{"record":{"id":"162a59a4bd00f468","repo":"validatorjs/validator.js","slug":"invalid-locale-locale","errorCode":null,"errorMessage":"Invalid locale '${locale}'","messagePattern":"Invalid locale '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/isAlpha.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 alpha) {\n    return alpha[locale].test(str);\n  }\n  throw new Error(`Invalid locale '${locale}'`);\n}\n\nexport const locales = Object.keys(alpha);\n","sourceCodeStart":5,"sourceCodeEnd":27,"githubUrl":"https://github.com/validatorjs/validator.js/blob/a79ff980ab14257e795332989e497bdff3218e87/src/lib/isAlpha.js#L5-L27","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","solutions":["Use a key from validator.locales (exported by the module) instead of a guessed code","Normalize your locale (e.g. 'en' -> 'en-US') with a small mapping before calling","Check membership: if (!locales.includes(locale)) fallback to 'en-US'","Log/print the actual runtime value of the locale variable to find undefined/empty values"],"exampleFix":"// before\nvalidator.isAlpha(name, 'en'); // throws\n// after\nvalidator.isAlpha(name, 'en-US'); // or verify with locales.includes(locale)","handlingStrategy":"validation","validationCode":"import validator, { locales as alphaLocales } from 'validator';\nconst localeOk = (loc) => typeof loc === 'string' && alphaLocales.includes(loc);\nif (!localeOk(locale)) locale = 'en-US';","typeGuard":"const isValidAlphaLocale = (loc) => typeof loc === 'string' && validator.locales.includes(loc);","tryCatchPattern":"let ok;\ntry { ok = validator.isAlpha(str, locale); }\ncatch (e) { if (/^Invalid locale/.test(e.message)) { ok = validator.isAlpha(str, 'en-US'); } else { throw e; } }","preventionTips":["Pick locales only from validator.locales, never hand-written codes","Centralize one locale-mapping helper for the whole app","Default to 'en-US' for unset/unknown locales","Snapshot-test your locale mapping against validator.locales on upgrades"],"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"}