typestack / class-validator

Decorator-based property validation for classes.
MIT License
10.89k stars 786 forks source link

Mobile phone number validation #366

Closed Demogorrgon closed 5 years ago

Demogorrgon commented 5 years ago

Hello ! Is there a way to validate any mobile phone number without explicitly setting regions? I just want to check, that string is a mobile phone number.

vlapo commented 5 years ago

Did you try this?

@IsMobilePhone("any")

From docs:

@IsMobilePhone(locale: string) | Checks if the string is a mobile phone number.
Demogorrgon commented 5 years ago

No, i didn't, but I will, thanks

чт, 13 июн. 2019 г., 23:42 Vlad Poluch notifications@github.com:

Did you try this?

@IsMobilePhone("any")

From docs:

@IsMobilePhone(locale: string) | Checks if the string is a mobile phone number.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/typestack/class-validator/issues/366?email_source=notifications&email_token=AF3DW6Z7QJ3KO53BVMXUE6TP2KWNDA5CNFSM4HXZMKX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXU7FPQ#issuecomment-501871294, or mute the thread https://github.com/notifications/unsubscribe-auth/AF3DW6YNJKKNUDO7WKTL65LP2KWNDANCNFSM4HXZMKXQ .

Demogorrgon commented 5 years ago

So, IsMobilePhone("any") do not works properly It says, that '3' is not ok, '33' is not ok, but '333' is ok But looks like it works good, if i enter '+' symbol as the first string symbol

btw, i try to check mobile phone with schema using this structure: phoneNumber: [{ type: 'isMobilePhone', constraints: ['any'] }],

Maybe, i'm doing something wrong ?

vlapo commented 5 years ago

class-validator use https://www.npmjs.com/package/google-libphonenumber to check phone numbers. Maybe check there what format is supported. But I think lead + is international standard. This is class-validator internal code:

const phoneNum = phoneUtil.parseAndKeepRawInput(value, region);
const result = phoneUtil.isValidNumber(phoneNum);

I think your schema structure is ok.

vlapo commented 5 years ago

@oreshkoandrei Did you resolve your problem?

Demogorrgon commented 5 years ago

yeah, sorry for the delay

lock[bot] commented 5 years ago

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