class-validator
class-validator copied to clipboard
How @IsPhoneNumber('US') actually works
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
-
The
+1
it's required since is the code forUS
. -
Also, it validates state codes
+1(615)...
for Tennessee or+1(787)...
for Puerto Rico -
How does the decorator actually works?
-
What are my options?
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.
I think I now understand the actual issue here. This is the behaviour described in #1702
Tracking in https://github.com/typestack/class-validator/issues/1702.
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.