validatorjs/validator.js · error · Error

Invalid country code: '${countryCode}'

Error message

Invalid country code: '${countryCode}'

What it means

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.

Source

Thrown at src/lib/isVAT.js:139

  HN: str => /^(HN)?$/.test(str),
  MX: str => /^(MX)?\w{3,4}\d{6}\w{3}$/.test(str),
  NI: str => /^(NI)?\d{3}-\d{6}-\d{4}\w{1}$/.test(str),
  PA: str => /^(PA)?$/.test(str),
  PY: str => /^(PY)?\d{6,8}-\d{1}$/.test(str),
  PE: str => /^(PE)?\d{11}$/.test(str),
  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),
  UY: str => /^(UY)?\d{12}$/.test(str),
  VE: str => /^(VE)?[JGVE]{1}-(\d{9}|(\d{8}-\d{1}))$/.test(str),
};

export default function isVAT(str, countryCode) {
  assertString(str);
  assertString(countryCode);

  if (countryCode in vatMatchers) {
    return vatMatchers[countryCode](str);
  }
  throw new Error(`Invalid country code: '${countryCode}'`);
}

View on GitHub (pinned to a79ff980ab)

Solutions

  1. Use the exact uppercase ISO-style code listed in the README's isVAT section (e.g. 'DE', 'FR', 'GB').
  2. Verify the country is actually supported in your installed validator.js version by checking src/lib/isVAT.js vatMatchers keys.
  3. Pre-check the country code against a whitelist before calling isVAT and handle unsupported countries with a fallback (e.g. regex or external API).
  4. Trim/normalize the code (countryCode.trim().toUpperCase()) sourced from user input or config.

Example fix

// before
validator.isVAT(vat, country); // country = 'de' -> throws
// after
const code = (country || '').trim().toUpperCase();
if (validator.isVAT(vat, code)) {
  // valid
} else {
  // unsupported country or invalid VAT — handle explicitly
}
Defensive patterns

Strategy: validation

Validate before calling

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
function isValidVAT(str, countryCode) {
  if (typeof str !== 'string' || typeof countryCode !== 'string') return false;
  const code = countryCode.trim().toUpperCase();
  return SUPPORTED_VAT_COUNTRIES.includes(code) && validator.isVAT(str, code);
}

Type guard

function isSupportedVatCountry(countryCode) {
  return typeof countryCode === 'string' && SUPPORTED_VAT_COUNTRIES.includes(countryCode.trim().toUpperCase());
}

Try / catch

try {
  return validator.isVAT(str, countryCode);
} catch (e) {
  if (/^Invalid country code/.test(e.message)) {
    console.warn(`VAT validation unsupported for: ${countryCode}`);
    return null; // or fall back to a generic EU regex
  }
  throw e;
}

Prevention

When it happens

Trigger: 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'.

Common situations: 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.

Related errors


AI-assisted analysis of validatorjs/validator.js@a79ff980ab (2026-08-31). Data as JSON: /api/errors/9ff1b3fe21fe09b9. Report an issue: GitHub.