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

  1. Check the locale against the supported list documented in the README (isTaxID section) and fix the spelling/format (e.g. 'en-US', 'de-AT').
  2. Log/inspect the actual locale value at the call site — undefined variables interpolate as 'undefined'.
  3. Guard the call: only invoke isTaxID when the locale is in your own whitelist of supported locales, otherwise skip or use a different validator.
  4. 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

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


AI-assisted analysis of validatorjs/validator.js@a79ff980ab (2026-08-31). Data as JSON: /api/errors/e03ddb8f661d07df. Report an issue: GitHub.