question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fix: `@IsPhoneNumber` decorator does not match E.164 format

See original GitHub issue

Description

Hi, I use the class-validator library to validate request data on a Node.js app. I have problems with validating phone numbers with intl. prefix (use ‘ZZ’ as a region)!

Example

class PhoneNumberWithIssue {
  @IsNotEmpty({ message: 'ERROR_EMPTY_PHONE_NUMBER_FIELD' })
  @IsPhoneNumber('ZZ', { message: 'ERROR_INVALID_PHONE_NUMBER' })
  phoneNumber!: string;
}

const invalidPhoneNumber = new PhoneNumberWithIssue();
invalidPhoneNumber.phoneNumber = "+380981111111++++++++++=====++++=+";
validate(invalidPhoneNumber).then(errors => {
    if (errors.length > 0) {
        console.log("validation failed. errors: ", errors);
    } else {
        console.log("validation succeed"); // Validation is succeeded, but the phone number is not valid!
    }
});

Dependencies

  • "class-validator": "^0.10.0"

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
kamronbatmancommented, Jan 22, 2022

For anyone reading this like I am and wondering why this hasn’t been fixed, here is a quick workaround: is-e164-phone-number.decorator.ts

import { registerDecorator } from 'class-validator';
import { parsePhoneNumberFromString } from 'libphonenumber-js';
import { ValidationOptions } from 'class-validator/types/decorator/ValidationOptions';

export function IsE164PhoneNumber(
  validationOptions?: ValidationOptions,
): PropertyDecorator {
  return function (object: object, propertyName: string) {
    registerDecorator({
      name: 'isE164PhoneNumber',
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      validator: {
        validate(value: any) {
          const phoneNumber = parsePhoneNumberFromString(value);
          return phoneNumber.number == value && phoneNumber.isValid();
        },
      },
    });
  };
}

Usage:

export class UserDto {
  @IsE164PhoneNumber()
  phoneNumber: string;
}
1reaction
NoNameProvidedcommented, Aug 4, 2020

The related PR has been closed as out-of-date because the libary used to parse phone numbers has changed. This issue needs to be re-tested with the new lib.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IsPhoneNumber() npm class validator how to add multiple ...
So solution is: parse phone number; if it's valid check country code; if it's valid pass to next built-in decorator. So I've created ......
Read more >
Javascript E164 Phone Number Validation - Kevin Chisholm
I this article you will learn how to set up a JavaScript E164 phone number validation system for your forms. E.164 is the...
Read more >
Phone numbers in JavaScript using E.164, libphonenumber ...
The E.164 standard might be great for storage, but terrible for two things. First, virtually no one would type or read out their...
Read more >
Computer Programming using Python
Python variables do not need explicit declaration to reserve memory space. ... If values of two operands are not equal, then condition becomes...
Read more >
libphonenumber-js - npm
Since libphonenumber-js is not a dialing library (we're not Android phone operaing ... number: string — The phone number in E.164 format.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found