class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

How @IsPhoneNumber('US') actually works

Open redeemefy opened this issue 1 year ago • 1 comments

I was trying to... I'm trying to validate a phone number from the United States

...
@IsOptional()
@IsPhoneNumber('US')
readonly phone_number: string
...

The problem: The validation gets bypassed with a number from Colombia, South America or without +1 prefix.

...
  "phone_number": "+573166239876" // Colombia is bypassing validation
...
// or
  "phone_number": "3168769876"

If I try to pass a code that actually doesn't exit like 1119308765 it does return the validation message.

What I'm expecting out of @IsPhoneNumber('US') is that

  1. The +1 it's required since is the code for US.

  2. Also, it validates state codes +1(615)... for Tennessee or +1(787)... for Puerto Rico

  3. How does the decorator actually works?

  4. What are my options?

redeemefy avatar Jul 14 '22 19:07 redeemefy

If you take a look at the source code for this decorator, you can see that the package libphonenumber-js is used.

You can take a look at libphonenumber-js on npm

This project mostly uses external packages to perform the actual validation logic, the most notable of which being validator.js.

Another option could be to use IsMobilePhone instead, which uses validator.js.

braaar avatar Aug 09 '22 15:08 braaar

I think I now understand the actual issue here. This is the behaviour described in #1702

braaar avatar Oct 06 '22 05:10 braaar

Tracking in https://github.com/typestack/class-validator/issues/1702.

NoNameProvided avatar Nov 13 '22 14:11 NoNameProvided

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Dec 14 '22 00:12 github-actions[bot]