validatorjs/validator.js · error · Error

Invalid locale '${locale}'

Error message

Invalid locale '${locale}'

What it means

isAlpha(str, locale) validates alphabetic strings against a fixed table of locale regexes (en-US, ar, de, etc.). If the locale string is not a key in that table, the library throws instead of silently returning false, so a misconfigured locale is surfaced immediately.

Source

Thrown at src/lib/isAlpha.js:23

  assertString(_str);

  let str = _str;
  const { ignore } = options;

  if (ignore) {
    if (ignore instanceof RegExp) {
      str = str.replace(ignore, '');
    } else if (typeof ignore === 'string') {
      str = str.replace(new RegExp(`[${ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for ignore
    } else {
      throw new Error('ignore should be instance of a String or RegExp');
    }
  }

  if (locale in alpha) {
    return alpha[locale].test(str);
  }
  throw new Error(`Invalid locale '${locale}'`);
}

export const locales = Object.keys(alpha);

View on GitHub (pinned to a79ff980ab)

Solutions

  1. Use a key from validator.locales (exported by the module) instead of a guessed code
  2. Normalize your locale (e.g. 'en' -> 'en-US') with a small mapping before calling
  3. Check membership: if (!locales.includes(locale)) fallback to 'en-US'
  4. Log/print the actual runtime value of the locale variable to find undefined/empty values

Example fix

// before
validator.isAlpha(name, 'en'); // throws
// after
validator.isAlpha(name, 'en-US'); // or verify with locales.includes(locale)
Defensive patterns

Strategy: validation

Validate before calling

import validator, { locales as alphaLocales } from 'validator';
const localeOk = (loc) => typeof loc === 'string' && alphaLocales.includes(loc);
if (!localeOk(locale)) locale = 'en-US';

Type guard

const isValidAlphaLocale = (loc) => typeof loc === 'string' && validator.locales.includes(loc);

Try / catch

let ok;
try { ok = validator.isAlpha(str, locale); }
catch (e) { if (/^Invalid locale/.test(e.message)) { ok = validator.isAlpha(str, 'en-US'); } else { throw e; } }

Prevention

When it happens

Trigger: isAlpha('hello', 'en') (correct key is 'en-US'), isAlpha(str, ''), isAlpha(str, 'fr-FR-XYZ'), or passing a dynamic/undefined locale variable that interpolates as 'undefined'.

Common situations: Using two-letter language codes instead of the locale keys the library ships (e.g. 'en' vs 'en-US'); reading the locale from user config or environment where it is unset; library upgrade adding/removing locales so a previously valid key disappears.

Related errors


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