validatorjs/validator.js · error · Error
Invalid locale '${locale}'
Error message
Invalid locale '${locale}' What it means
isTaxID(str, locale) validates a tax ID against locale-specific rules. The locale argument is looked up in the internal taxIdCheck map of supported locales; if the key is absent, the function throws this Error instead of returning false. It signals a programming mistake in the locale argument, not an invalid input string.
Source
Thrown at src/lib/isTaxID.js:1301
assertString(str);
// Copy TIN to avoid replacement if sanitized
let strcopy = str.slice(0);
if (locale in taxIdFormat) {
if (locale in sanitizeRegexes) {
strcopy = strcopy.replace(sanitizeRegexes[locale], '');
}
if (!taxIdFormat[locale].test(strcopy)) {
return false;
}
if (locale in taxIdCheck) {
return taxIdCheck[locale](strcopy);
}
// Fallthrough; not all locales have algorithmic checks
return true;
}
throw new Error(`Invalid locale '${locale}'`);
}
View on GitHub (pinned to a79ff980ab)
Solutions
- Check the locale against the supported list documented in the README (isTaxID section) and fix the spelling/format (e.g. 'en-US', 'de-AT').
- Log/inspect the actual locale value at the call site — undefined variables interpolate as 'undefined'.
- Guard the call: only invoke isTaxID when the locale is in your own whitelist of supported locales, otherwise skip or use a different validator.
- Pin/verify your validator.js version and confirm the locale exists in that version's src/lib/isTaxID.js taxIdCheck map.
Example fix
// before
validator.isTaxID(taxId, userLocale); // throws if userLocale is 'US'
// after
const SUPPORTED = ['en-US', 'de-AT', 'es-ES', 'fr-FR'];
if (SUPPORTED.includes(userLocale)) {
validator.isTaxID(taxId, userLocale);
} else {
throw new Error(`Unsupported tax ID locale: ${userLocale}`);
} Defensive patterns
Strategy: validation
Validate before calling
const SUPPORTED_TAX_LOCALES = ['en-US','en-CA','de-AT','de-DE','es-ES','fr-FR','gb-GB']; // per README isTaxID list
function isValidTaxIdFormat(str, locale) {
if (typeof str !== 'string' || typeof locale !== 'string') return false;
return SUPPORTED_TAX_LOCALES.includes(locale) && validator.isTaxID(str, locale);
} Type guard
function isSupportedTaxLocale(locale) {
return typeof locale === 'string' && SUPPORTED_TAX_LOCALES.includes(locale);
} Try / catch
try {
return validator.isTaxID(str, locale);
} catch (e) {
if (/^Invalid locale/.test(e.message)) {
console.warn(`Unsupported tax ID locale: ${locale}`);
return false;
}
throw e;
} Prevention
- Keep a whitelist constant of locales your app supports and intersect it with the library's documented isTaxID locale list.
- Never pass raw user/config values as locale without normalizing (trim, lowercase language, uppercase region, join with '-').
- On validator.js upgrades, diff the taxIdCheck keys in src/lib/isTaxID.js for removed/renamed locales.
- Add a unit test asserting your app's locale list only contains locales accepted by isTaxID.
When it happens
Trigger: Calling isTaxID(str, locale) with a locale string not in the supported set: misspelled names ('en_US' vs 'en-US'), unsupported countries ('us' lowercase, 'fr'), a variable that is undefined/null producing 'undefined'/'null' in the message, or a locale removed/renamed between validator versions.
Common situations: Hardcoding a locale from user config or an env var without checking support; passing a country code where a locale code is expected; upgrading validator.js and using a locale name that changed; dynamic locale selection from a dropdown not aligned with the library's list.
Related errors
- Invalid country code: '${countryCode}'
- Expected a string but received a ${input.constructor.name}
- Invalid locale '${locale}'
- Invalid locale '${locale}'
- Invalid locale '${options.locale}'
AI-assisted analysis of validatorjs/validator.js@a79ff980ab (2026-08-31).
Data as JSON: /api/errors/e03ddb8f661d07df.
Report an issue: GitHub.