{"record":{"id":"8de9105c211f0c81","repo":"validatorjs/validator.js","slug":"invalid-locale-locale-8de910","errorCode":null,"errorMessage":"Invalid locale '${locale}'","messagePattern":"Invalid locale '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/isAlphanumeric.js","lineNumber":23,"sourceCode":"  assertString(_str);\n\n  let str = _str;\n  const { ignore } = options;\n\n  if (ignore) {\n    if (ignore instanceof RegExp) {\n      str = str.replace(ignore, '');\n    } else if (typeof ignore === 'string') {\n      str = str.replace(new RegExp(`[${ignore.replace(/[-[\\]{}()*+?.,\\\\^$|#\\\\s]/g, '\\\\$&')}]`, 'g'), ''); // escape regex for ignore\n    } else {\n      throw new Error('ignore should be instance of a String or RegExp');\n    }\n  }\n\n  if (locale in alphanumeric) {\n    return alphanumeric[locale].test(str);\n  }\n  throw new Error(`Invalid locale '${locale}'`);\n}\n\nexport const locales = Object.keys(alphanumeric);\n","sourceCodeStart":5,"sourceCodeEnd":27,"githubUrl":"https://github.com/validatorjs/validator.js/blob/a79ff980ab14257e795332989e497bdff3218e87/src/lib/isAlphanumeric.js#L5-L27","documentation":"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.","triggerScenarios":"isAlphanumeric('foo123', 'en') instead of 'en-US'; empty-string or undefined locale interpolated into the message; a locale key removed in a library upgrade.","commonSituations":"Hard-coded two-letter codes from i18n config; dynamic locale from HTTP Accept-Language passed raw; environment-specific config leaving the variable unset.","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"],"exampleFix":"// before\nconst loc = req.acceptsLanguages()[0]; // e.g. 'en'\nvalidator.isAlphanumeric(input, loc); // throws\n// after\nconst map = { en: 'en-US', ar: 'ar-EG' };\nvalidator.isAlphanumeric(input, map[loc.split('-')[0]] || 'en-US');","handlingStrategy":"validation","validationCode":"import { locales as alnumLocales } from 'validator';\nif (!alnumLocales.includes(locale)) locale = 'en-US';","typeGuard":"const isValidAlphanumericLocale = (loc) => typeof loc === 'string' && alnumLocales.includes(loc);","tryCatchPattern":"let ok;\ntry { ok = validator.isAlphanumeric(str, locale); }\ncatch (e) { if (/^Invalid locale/.test(e.message)) { ok = validator.isAlphanumeric(str, 'en-US'); } else { throw e; } }","preventionTips":["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"],"tags":["validation","locale","options"],"backgroundTag":"invalid-locale","analyzedSha":"a79ff980ab14257e795332989e497bdff3218e87","analyzedAt":"2026-08-31T21:42:31.416Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}