validatorjs/validator.js · error · Error
Invalid locale '${locale}'
Error message
Invalid locale '${locale}' What it means
isAlphanumeric(str, locale) tests against per-locale regexes keyed exactly like the alpha table (e.g. 'en-US', 'ar-SA'). An unknown locale key throws this error rather than returning false, to fail fast on misconfiguration.
Source
Thrown at src/lib/isAlphanumeric.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 alphanumeric) {
return alphanumeric[locale].test(str);
}
throw new Error(`Invalid locale '${locale}'`);
}
export const locales = Object.keys(alphanumeric);
View on GitHub (pinned to a79ff980ab)
Solutions
- Pick the locale from the module's exported locales list
- Map application locales to validator locale keys before calling
- Default to 'en-US' when the configured locale is unsupported
- Inspect the runtime value when the message shows 'undefined' or empty
Example fix
// before
const loc = req.acceptsLanguages()[0]; // e.g. 'en'
validator.isAlphanumeric(input, loc); // throws
// after
const map = { en: 'en-US', ar: 'ar-EG' };
validator.isAlphanumeric(input, map[loc.split('-')[0]] || 'en-US'); Defensive patterns
Strategy: validation
Validate before calling
import { locales as alnumLocales } from 'validator';
if (!alnumLocales.includes(locale)) locale = 'en-US'; Type guard
const isValidAlphanumericLocale = (loc) => typeof loc === 'string' && alnumLocales.includes(loc);
Try / catch
let ok;
try { ok = validator.isAlphanumeric(str, locale); }
catch (e) { if (/^Invalid locale/.test(e.message)) { ok = validator.isAlphanumeric(str, 'en-US'); } else { throw e; } } Prevention
- Map i18n/Accept-Language locales to validator keys in one place
- Never pass raw user/config locale strings to the validator
- Fall back to a known-good key when the mapping misses
- Re-verify keys after every validator.js upgrade
When it happens
Trigger: isAlphanumeric('foo123', 'en') instead of 'en-US'; empty-string or undefined locale interpolated into the message; a locale key removed in a library upgrade.
Common situations: Hard-coded two-letter codes from i18n config; dynamic locale from HTTP Accept-Language passed raw; environment-specific config leaving the variable unset.
Related errors
- Invalid locale '${locale}'
- Invalid locale '${options.locale}'
- ignore should be instance of a String or RegExp
- ignore should be instance of a String or RegExp
- ${provider} is not a valid credit card provider.
AI-assisted analysis of validatorjs/validator.js@a79ff980ab (2026-08-31).
Data as JSON: /api/errors/8de9105c211f0c81.
Report an issue: GitHub.