{"record":{"id":"4820210086524920","repo":"validatorjs/validator.js","slug":"invalid-locale-locale-482021","errorCode":null,"errorMessage":"Invalid locale '${locale}'","messagePattern":"Invalid locale '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/isPostalCode.js","lineNumber":104,"sourceCode":"\nexport default function isPostalCode(str, locale) {\n  assertString(str);\n  if (locale in patterns) {\n    return patterns[locale].test(str);\n  } else if (locale === 'any') {\n    for (const key in patterns) {\n      // https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes\n      // istanbul ignore else\n      if (patterns.hasOwnProperty(key)) {\n        const pattern = patterns[key];\n        if (pattern.test(str)) {\n          return true;\n        }\n      }\n    }\n    return false;\n  }\n  throw new Error(`Invalid locale '${locale}'`);\n}\n","sourceCodeStart":86,"sourceCodeEnd":106,"githubUrl":"https://github.com/validatorjs/validator.js/blob/a79ff980ab14257e795332989e497bdff3218e87/src/lib/isPostalCode.js#L86-L106","documentation":"isPostalCode(str, locale) validates postal codes against locale-specific regexes (e.g. 'US', 'GB', 'FR', or 'any'). An unsupported locale throws this error instead of returning false, because a missing locale entry is a caller configuration problem.","triggerScenarios":"isPostalCode('94105', 'usa') (must be 'US'), isPostalCode(zip, country) where country is 'USA' or lowercase, locale variable undefined.","commonSituations":"Feeding full country names or ISO3 codes from shipping databases into the validator; case mismatches; new country added in a newer validator.js than installed.","solutions":["Use the exact ISO-ish keys the table expects ('US', 'GB', 'PT', ...) or 'any'","Upper-case and trim the country code before calling","Validate the key with the module's exported locales list first","Upgrade validator.js if the needed locale was added in a newer version"],"exampleFix":"// before\nvalidator.isPostalCode(zip, order.country); // 'usa' -> throws\n// after\nvalidator.isPostalCode(zip, (order.country || '').toUpperCase() === 'USA' ? 'US' : order.country);","handlingStrategy":"validation","validationCode":"import { locales as postalLocales } from 'validator';\nconst norm = String(country || '').trim().toUpperCase();\nconst locale = postalLocales.includes(norm) ? norm : 'any';\nvalidator.isPostalCode(zip, locale);","typeGuard":"const isValidPostalLocale = (loc) => typeof loc === 'string' && postalLocales.includes(loc);","tryCatchPattern":"let ok;\ntry { ok = validator.isPostalCode(zip, locale); }\ncatch (e) { if (/^Invalid locale/.test(e.message)) { ok = validator.isPostalCode(zip, 'any'); } else { throw e; } }","preventionTips":["Normalize country codes to uppercase two-letter form before calling","Use 'any' when the country is unknown rather than guessing a key","Keep a country->locale mapping tested against validator's locales list","Upgrade validator.js when a needed country key is missing"],"tags":["validation","locale","postal-code"],"backgroundTag":"invalid-locale","analyzedSha":"a79ff980ab14257e795332989e497bdff3218e87","analyzedAt":"2026-08-31T21:42:31.416Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}