Skip to content

feat(isPassportNumber): add support for any #2570

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Validator | Description
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{ no_symbols: false }` it also has `locale` as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).<br/><br/>`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fr-FR', 'fr-CA', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`.
**isOctal(str)** | check if the string is a valid octal number.
**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.<br/><br/>`countryCode` is one of `['AM', 'AR', 'AT', 'AU', 'AZ', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE', 'IN', 'IR', 'ID', 'IS', 'IT', 'JM', 'JP', 'KR', 'KZ', 'LI', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'NZ', 'PH', 'PK', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', 'TR', 'UA', 'US', 'ZA']`. Locale list is `validator.passportNumberLocales`.
**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.<br/><br/>`countryCode` is one of `['AM', 'AR', 'AT', 'AU', 'AZ', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE', 'IN', 'IR', 'ID', 'IS', 'IT', 'JM', 'JP', 'KR', 'KZ', 'LI', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'NZ', 'PH', 'PK', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', 'TR', 'UA', 'US', 'ZA']` OR `'any'`. If 'any' is used, function will check if any of the locales match. Locale list is `validator.passportNumberLocales`.
**isPort(str)** | check if the string is a valid port number.
**isPostalCode(str, locale)** | check if the string is a postal code.<br/><br/>`locale` is one of `['AD', 'AT', 'AU', 'AZ', 'BA', 'BD', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CO', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MG', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PK', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM']` OR `'any'`. If 'any' is used, function will check if any of the locales match. Locale list is `validator.isPostalCodeLocales`.
**isRFC3339(str)** | check if the string is a valid [RFC 3339][RFC 3339] date.
Expand Down
12 changes: 12 additions & 0 deletions src/lib/isPassportNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ export default function isPassportNumber(str, countryCode) {
/** Remove All Whitespaces, Convert to UPPERCASE */
const normalizedStr = str.replace(/\s/g, '').toUpperCase();

if (countryCode === 'any') {
for (const key in passportRegexByCountryCode) {
if (passportRegexByCountryCode.hasOwnProperty(key)) {
const regex = passportRegexByCountryCode[key];
if (regex.test(normalizedStr)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the if statement here is not needed: regex.test returns a boolean.
Why not just directly return that value, instead of manually checking if it is "true-ish" and then returning true?

Copy link
Author

@ilyichv ilyichv Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning directly the result won't work since it breaks the loop and you want to check if at least one element matches.
I tried to follow the code style of isPostalCode. A better-looking version could be:

if (countryCode === 'any') {
    return Object.values(passportRegexByCountryCode).some(regex => regex.test(normalizedStr));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, of course, you're right – I overlooked that we are in a for loop, my bad!

I do like your new version though :-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code an then noticed that you're testing node 6 as well and of course it fails. If you agree we can keep the "dirty" version and move on!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about the CI testing against Node 6 (which IMHO is useless that it still is done, seeing that it was EOL'ed almost 6 years ago :-()

so yeah, all good to go back with your original version then

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a bit weird! I reverted the latest version anyway :)

return true;
}
}
}
return false;
}

return (countryCode.toUpperCase() in passportRegexByCountryCode) &&
passportRegexByCountryCode[countryCode].test(normalizedStr);
}
88 changes: 88 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3641,6 +3641,94 @@ describe('Validators', () => {
'Z12345678',
],
});

test({
validator: 'isPassportNumber',
args: ['any'],
valid: [
'AB1234567',
'ABC123456',
'A1234567',
'A1234567',
'A12345678',
'AB123456',
'123456789',
'AB123456',
'AB1234567',
'AB123456',
'A1234567',
'G12345678',
'EA1234567',
'A123456',
'12345678',
'C12345678',
'123456789',
'123456789',
'K1234567',
'AB1234567',
'AB123456',
'AB1234567',
'12AB12345',
'123456789',
'AB1234567',
'123456789',
'A-1234567',
'A1234567',
'A12345678',
'A1234567',
'AB1234567',
'A1234567',
'AB1234567',
'S12345678',
'M12345678',
'A1234567',
'A12345',
'AB123456',
'AB123456',
'AB1234567',
'AB123456',
'1234567',
'AB1234567',
'A12345678',
'12345678901',
'AB1234567',
'LA123456',
'LD123456',
'LF123456',
'LH123456',
'EA123456',
'EP123456',
'N123456',
'A123456',
'A1234567A',
'AB123456',
'AB1234567',
'AB1234567',
'AB1234567',
'A123456',
'12345678',
'123456789',
'123456789',
'12345678',
'PA1234567',
'A1234567',
'A123456',
'AB1234567',
'A12345678',
'AB123456',
'123456789',
'A12345678',
'T12345678',
'A12345678',
'M12345678',
'D12345678',
],
invalid: [
'12345',
'ABCDEFGH!',
'A1B2C3D4E5F',
],
});
});

it('should validate decimal numbers', () => {
Expand Down