validatorjs/validator.js · error · Error

Invalid locale '${locale}'

Error message

Invalid locale '${locale}'

What it means

isLicensePlate(str, locale) checks vehicle license plates against locale-specific regexes (e.g. 'de-DE', 'pt-PT', 'any' to accept any supported plate). Unknown locale keys throw this error to distinguish configuration mistakes from invalid plates.

Source

Thrown at src/lib/isLicensePlate.js:39

    /^[A-HJ-PR-UW-Z]{3} ?[\d]{2}[A-HJ-PR-UW-Z1-9]$|(^[A-ZÅÄÖ ]{2,7}$)/.test(str.trim()),
  'en-PK': str => /(^[A-Z]{2}((\s|-){0,1})[0-9]{3,4}((\s|-)[0-9]{2}){0,1}$)|(^[A-Z]{3}((\s|-){0,1})[0-9]{3,4}((\s|-)[0-9]{2}){0,1}$)|(^[A-Z]{4}((\s|-){0,1})[0-9]{3,4}((\s|-)[0-9]{2}){0,1}$)|(^[A-Z]((\s|-){0,1})[0-9]{4}((\s|-)[0-9]{2}){0,1}$)/.test(str.trim()),
};

export default function isLicensePlate(str, locale) {
  assertString(str);
  if (locale in validators) {
    return validators[locale](str);
  } else if (locale === 'any') {
    for (const key in validators) {
      /* eslint guard-for-in: 0 */
      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 documented keys like 'de-DE', 'pt-PT', or 'any'
  2. Translate ISO country codes to validator keys before calling
  3. Catch the error and return an 'unknown locale' business error instead of 'invalid plate'
  4. Check src/lib/isLicensePlate.js for the exact key set in your installed version

Example fix

// before
validator.isLicensePlate(plate, 'DE'); // throws
// after
validator.isLicensePlate(plate, 'de-DE');
Defensive patterns

Strategy: validation

Validate before calling

const PLATE_LOCALES = ['de-DE','de-LI','pt-PT','fi-FI','sv-SE','sq-AL','cs-CZ','es-AR','br-PR','EC','any'];
if (!PLATE_LOCALES.includes(locale)) throw new TypeError(`Unsupported license-plate locale: ${locale}`);

Type guard

const isPlateLocale = (loc) => typeof loc === 'string' && PLATE_LOCALES.includes(loc);

Try / catch

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

Prevention

When it happens

Trigger: isLicensePlate('B-AB 1234', 'DE') instead of 'de-DE'; locale passed as undefined from an unbound variable; using a country name string instead of the locale key.

Common situations: Mapping vehicle-registration data (ISO country) directly to the validator without translation; stale locale key after a validator.js upgrade; typo in a hard-coded key.

Related errors


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