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

Export the list of supported countries for isPostalCode, locales for isMobilePhone, etc... #815

Closed
dantman opened this issue Apr 14, 2018 · 6 comments · Fixed by #890
Closed

Comments

@dantman
Copy link

dantman commented Apr 14, 2018

I would like to use isPostalCode for form validation in a situation where the country code may be input by the user. I want to validate all the common countries (Canada, US, UK, etc...) that validator supports but if validator doesn't know how to validate a country's postal code I just want to consider all input valid.

The problem is that isPostalCode will throw a generic Error instance if the country code is one it does not understand. Which I cannot differentiate from a programming error without checking against the message (bad practice). The only thing I can do at the moment is hardcode a list of what countries are supported by isPostalCode in my code and check against that list before calling it to determine if it's safe to call. However this leaves open the possibility that validator will add support for another country's postal codes (I even found a database that seems to have more than what is in validator that validator might want to source some info from), which will make my list be out of date.

It would be nice if the list of countries supported by isPostalCode would be exported somewhere so I could import it and check my user inputted country codes so see if the postal code is safe to validate.

It would also work to add an option to consider postal codes from all unknown countries as valid. However I am also considering the possibility of doing something like required={isRequired && canValidatePostalCodeForCountry(countryCode)} to disable postal codes being required in forms when it's not known if the country has them.

@stevula
Copy link
Contributor

stevula commented Apr 26, 2018

I am facing the same issue. I have a list of supported countries which are not all supported by isPostalCode. It seems my only options are to check against the list from the README (not maintainable) or specify the path to src/lib/isPostalCode.js (brittle). It would be great if I could import directly from the package instead of hoping the path to that module doesn't change.

@chriso
Copy link
Collaborator

chriso commented May 3, 2018

I'd be happy to accept a PR that exports the list.

@profnandaa
Copy link
Member

@chriso - should this be now closed because of #830 ?

@chriso
Copy link
Collaborator

chriso commented Sep 1, 2018

@profnandaa I don't think the list of mobile phone locales is being exported currently.

@profnandaa
Copy link
Member

@chriso -- ooh, got it. Let me be on it.

@profnandaa
Copy link
Member

I even found a database that seems to have more than what is in validator that validator might want to source some info from - @dantman

^^Let's separate this into #722

andela-anandaa pushed a commit to profnandaa/validator.js that referenced this issue Sep 2, 2018
This commit exports supported locales for the following functions:
- isAlpha -> isAlphaLocales
- isAlphanumeric -> isAlphanumericLocales
- isMobilePhone -> isMobilePhoneLocales

fixes validatorjs#815
andela-anandaa pushed a commit to profnandaa/validator.js that referenced this issue Sep 2, 2018
This commit exports supported locales for the following functions:
- isAlpha -> isAlphaLocales
- isAlphanumeric -> isAlphanumericLocales
- isMobilePhone -> isMobilePhoneLocales

fixes validatorjs#815
andela-anandaa pushed a commit to profnandaa/validator.js that referenced this issue Sep 2, 2018
This commit exports the list of supported locales for `isFloat`
as `isFloatLocales`

Ref: validatorjs#815, validatorjs#891, follow up for validatorjs#890
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants