validatorjs/validator.js · error · Error

Invalid locale '${locale}'

Error message

Invalid locale '${locale}'

What it means

isIdentityCard(str, locale) validates national identity-card numbers using regexes/validators keyed by locale string (e.g. 'he-IL', 'zh-CN', 'ES'). An unsupported locale key throws this error so callers do not mistake a config error for an invalid card number.

Source

Thrown at src/lib/isIdentityCard.js:446

export default function isIdentityCard(str, locale) {
  assertString(str);
  if (locale in validators) {
    return validators[locale](str);
  } else if (locale === 'any') {
    for (const key in validators) {
      // https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
      // istanbul ignore else
      if (validators.hasOwnProperty(key)) {
        const validator = validators[key];
        if (validator(str)) {
          return true;
        }
      }
    }
    return false;
  }
  throw new Error(`Invalid locale '${locale}'`);
}

View on GitHub (pinned to a79ff980ab)

Solutions

  1. Use exact locale keys from the identityCard table in src/lib/isIdentityCard.js or the README
  2. Map ISO country codes to validator locale keys in a lookup table
  3. Guard with try/catch and treat unknown locale as 'unsupported' rather than 'invalid id'
  4. Verify the key exists in the version you installed (keys differ across versions)

Example fix

// before
validator.isIdentityCard(idNumber, 'IL'); // throws
// after
validator.isIdentityCard(idNumber, 'he-IL');
Defensive patterns

Strategy: validation

Validate before calling

const ID_LOCALES = ['ES','he-IL','ar-TN','zh-CN','zh-TW','th-TH','LK','ko-KR','PL','FA-IR','TR','NG','ID','MY','PK','fr-LU','DE','MX'];
if (!ID_LOCALES.includes(locale)) throw new TypeError(`Unsupported identity-card locale: ${locale}`);

Type guard

const isIdCardLocale = (loc) => typeof loc === 'string' && ID_LOCALES.includes(loc);

Try / catch

let ok;
try { ok = validator.isIdentityCard(id, locale); }
catch (e) { if (/^Invalid locale/.test(e.message)) { return { valid: null, reason: 'unsupported-locale' }; } throw e; }

Prevention

When it happens

Trigger: isIdentityCard(id, 'IL') instead of 'he-IL'; isIdentityCard(id) with locale explicitly passed as undefined; locale keys removed between validator.js versions.

Common situations: Guessing country codes instead of using the documented locale keys; dynamic country from user profile not mapped to library keys; upgrading validator.js and a locale key changes.

Related errors


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