-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
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.