{"record":{"id":"001a610289d3ac78","repo":"validatorjs/validator.js","slug":"invalid-locale-locale-001a61","errorCode":null,"errorMessage":"Invalid locale '${locale}'","messagePattern":"Invalid locale '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/isMobilePhone.js","lineNumber":212,"sourceCode":"      }\n      return false;\n    });\n  } else if (locale in phones) {\n    return phones[locale].test(str);\n    // alias falsey locale as 'any'\n  } else if (!locale || locale === 'any') {\n    for (const key in phones) {\n      // istanbul ignore else\n      if (phones.hasOwnProperty(key)) {\n        const phone = phones[key];\n        if (phone.test(str)) {\n          return true;\n        }\n      }\n    }\n    return false;\n  }\n  throw new Error(`Invalid locale '${locale}'`);\n}\n\nexport const locales = Object.keys(phones);\n","sourceCodeStart":194,"sourceCodeEnd":216,"githubUrl":"https://github.com/validatorjs/validator.js/blob/a79ff980ab14257e795332989e497bdff3218e87/src/lib/isMobilePhone.js#L194-L216","documentation":"isMobilePhone(str, locale) validates phone numbers against per-locale patterns (e.g. 'en-US', 'zh-CN') or an array of such locales. A locale string not present in the phones table throws this error so misconfiguration is not silently reported as an invalid phone.","triggerScenarios":"isMobilePhone('+14155552671', 'US') instead of 'en-US'; passing undefined locale; passing an array containing one bad key (older versions throw for unsupported entries depending on code path).","commonSituations":"Using ISO country codes from a billing database directly; locale from user settings in an unsupported region; version upgrade adding/removing keys.","solutions":["Use exact keys from the module's exported locales list (export const locales = Object.keys(phones))","Normalize the user's country to a supported validator locale key","Check membership before calling: if (locales.includes(loc)) ... else fallback","When the message shows 'undefined', find where the locale variable is lost"],"exampleFix":"// before\nvalidator.isMobilePhone(phone, user.country); // e.g. 'US' -> throws\n// after\nconst key = { US: 'en-US', GB: 'en-GB', FR: 'fr-FR' }[user.country];\nvalidator.isMobilePhone(phone, key || 'en-US');","handlingStrategy":"validation","validationCode":"import { locales as phoneLocales } from 'validator';\nif (!phoneLocales.includes(locale)) locale = 'en-US';\nvalidator.isMobilePhone(phone, locale);","typeGuard":"const isValidPhoneLocale = (loc) => typeof loc === 'string' && phoneLocales.includes(loc);","tryCatchPattern":"let ok;\ntry { ok = validator.isMobilePhone(phone, locale); }\ncatch (e) { if (/^Invalid locale/.test(e.message)) { ok = validator.isMobilePhone(phone, 'en-US'); } else { throw e; } }","preventionTips":["Use the exported locales list as the single source of truth","Translate ISO country codes to validator keys (US -> en-US)","Validate arrays of locales element-by-element before passing","Log the exact locale value when the error message shows 'undefined'"],"tags":["validation","locale","phone"],"backgroundTag":"invalid-locale","analyzedSha":"a79ff980ab14257e795332989e497bdff3218e87","analyzedAt":"2026-08-31T21:42:31.416Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}