Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing Undefined To Country Code Causes Unexpected Behaviour - Non-Strict Mode Unclear #415

Open
conatus opened this issue Nov 23, 2020 · 0 comments

Comments

@conatus
Copy link

conatus commented Nov 23, 2020

Hey there!

Thanks for making this!

I'm calling this library with the following.

.phone(undefined, false, "A valid phone number is required")

This code is valid, because the type signature is as follows https://github.com/abhisekp/yup-phone/blob/master/src/yup-phone.ts#L16 - the first parameter is not required.

However, the library defaults to India in this case running the following:

const phoneNumber = phoneUtil.parseAndKeepRawInput(value, 'IN');

When we do the following:

const regionCodeFromPhoneNumber = phoneUtil.getRegionCodeForNumber(
    phoneNumber
);

Therefore we always get IN back in this situation, because the phone number of an unknown validity has been parsed as belonging to this region.

If the intended behaviour is that when we are not in strict mode, then the phone number's region is derived from the phone number itself then validated, then it will always be validated with the region specified or India.

So it is never really a "unstrict" test for any valid global phone number. It always needs a region.

Google's guide suggests that isValidNumberForRegion should be used sparing https://github.com/google/libphonenumber/blob/master/FAQ.md#when-should-i-use-isvalidnumberforregion

Maybe when we aren't in strict mode then isValidNumber() could be used?

Maybe I am missing something here?

In a PHP version of this library, one can pass empty strings or null when the region is unknown when parsing.

giggsey/libphonenumber-for-php#85

This however is not valid code:

const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();

const value = '202-456-1414';
const phoneNumber = phoneUtil.parse(value);

In the other Javascript library port of libphonenumber, this is valid code:

const parsePhoneNumber = require('libphonenumber-js');

const phoneNumber = parsePhoneNumber('+1 202-456-1414');

phoneNumber.isValid();

Hope this isn't confusing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant