{"record":{"id":"9ff1b3fe21fe09b9","repo":"validatorjs/validator.js","slug":"invalid-country-code-countrycode","errorCode":null,"errorMessage":"Invalid country code: '${countryCode}'","messagePattern":"Invalid country code: '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/isVAT.js","lineNumber":139,"sourceCode":"  HN: str => /^(HN)?$/.test(str),\r\n  MX: str => /^(MX)?\\w{3,4}\\d{6}\\w{3}$/.test(str),\r\n  NI: str => /^(NI)?\\d{3}-\\d{6}-\\d{4}\\w{1}$/.test(str),\r\n  PA: str => /^(PA)?$/.test(str),\r\n  PY: str => /^(PY)?\\d{6,8}-\\d{1}$/.test(str),\r\n  PE: str => /^(PE)?\\d{11}$/.test(str),\r\n  DO: str => /^(DO)?(\\d{11}|(\\d{3}-\\d{7}-\\d{1})|[145]{1}\\d{8}|([145]{1})-\\d{2}-\\d{5}-\\d{1})$/.test(str),\r\n  UY: str => /^(UY)?\\d{12}$/.test(str),\r\n  VE: str => /^(VE)?[JGVE]{1}-(\\d{9}|(\\d{8}-\\d{1}))$/.test(str),\r\n};\r\n\r\nexport default function isVAT(str, countryCode) {\r\n  assertString(str);\r\n  assertString(countryCode);\r\n\r\n  if (countryCode in vatMatchers) {\r\n    return vatMatchers[countryCode](str);\r\n  }\r\n  throw new Error(`Invalid country code: '${countryCode}'`);\r\n}\r\n","sourceCodeStart":121,"sourceCodeEnd":141,"githubUrl":"https://github.com/validatorjs/validator.js/blob/a79ff980ab14257e795332989e497bdff3218e87/src/lib/isVAT.js#L121-L141","documentation":"isVAT(str, countryCode) validates a VAT number for a specific country using per-country matcher functions in the vatMatchers map. When countryCode is not a key of that map, the library throws this Error because it has no rules for that country. It indicates an unsupported or malformed country-code argument, not an invalid VAT string.","triggerScenarios":"Calling isVAT(str, countryCode) with a code outside vatMatchers: lowercase codes ('de' instead of 'DE'), non-EU/unsupported countries ('US', 'CH'), whitespace or BOM in the code, or an undefined/null variable rendered as 'undefined'/'null'.","commonSituations":"Assuming isVAT supports all EU/EEA countries or worldwide codes when only a subset has matchers; taking country codes from user input or an i18n config in the wrong case; building a form where the country list is broader than the validator's support; version differences in supported countries.","solutions":["Use the exact uppercase ISO-style code listed in the README's isVAT section (e.g. 'DE', 'FR', 'GB').","Verify the country is actually supported in your installed validator.js version by checking src/lib/isVAT.js vatMatchers keys.","Pre-check the country code against a whitelist before calling isVAT and handle unsupported countries with a fallback (e.g. regex or external API).","Trim/normalize the code (countryCode.trim().toUpperCase()) sourced from user input or config."],"exampleFix":"// before\nvalidator.isVAT(vat, country); // country = 'de' -> throws\n// after\nconst code = (country || '').trim().toUpperCase();\nif (validator.isVAT(vat, code)) {\n  // valid\n} else {\n  // unsupported country or invalid VAT — handle explicitly\n}","handlingStrategy":"validation","validationCode":"const SUPPORTED_VAT_COUNTRIES = ['AT','BE','BG','CY','CZ','DE','DK','EE','ES','FI','FR','GB','GR','HR','HU','IE','IT','LT','LU','LV','MT','NL','PL','PT','RO','SE','SI','SK']; // per README isVAT list\nfunction isValidVAT(str, countryCode) {\n  if (typeof str !== 'string' || typeof countryCode !== 'string') return false;\n  const code = countryCode.trim().toUpperCase();\n  return SUPPORTED_VAT_COUNTRIES.includes(code) && validator.isVAT(str, code);\n}","typeGuard":"function isSupportedVatCountry(countryCode) {\n  return typeof countryCode === 'string' && SUPPORTED_VAT_COUNTRIES.includes(countryCode.trim().toUpperCase());\n}","tryCatchPattern":"try {\n  return validator.isVAT(str, countryCode);\n} catch (e) {\n  if (/^Invalid country code/.test(e.message)) {\n    console.warn(`VAT validation unsupported for: ${countryCode}`);\n    return null; // or fall back to a generic EU regex\n  }\n  throw e;\n}","preventionTips":["Normalize country codes with .trim().toUpperCase() immediately after receiving them from users or config.","Derive your country dropdown options from the library's supported list, not from a full ISO country list.","Handle unsupported countries (e.g. non-EU) with an explicit fallback path instead of calling isVAT blindly.","When upgrading validator.js, re-check the vatMatchers keys for changes in supported countries."],"tags":["validator","vat","country-code","argument-validation"],"backgroundTag":"unsupported-country-code","analyzedSha":"a79ff980ab14257e795332989e497bdff3218e87","analyzedAt":"2026-08-31T21:42:31.416Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}