validatorjs/validator.js · error · Error

Invalid locale '${options.locale}'

Error message

Invalid locale '${options.locale}'

What it means

isDecimal(str, options) validates decimal numbers using locale-specific formats; options.locale must be a key in the decimal locales table (default 'en-US'). If the provided locale is not in that table the function throws instead of returning false.

Source

Thrown at src/lib/isDecimal.js:25

  const regExp = new RegExp(`^[-+]?([0-9]+)?(\\${decimal[options.locale]}[0-9]{${options.decimal_digits}})${options.force_decimal ? '' : '?'}$`);
  return regExp;
}

const default_decimal_options = {
  force_decimal: false,
  decimal_digits: '1,',
  locale: 'en-US',
};

const blacklist = ['', '-', '+'];

export default function isDecimal(str, options) {
  assertString(str);
  options = merge(options, default_decimal_options);
  if (options.locale in decimal) {
    return !includes(blacklist, str.replace(/ /g, '')) && decimalRegExp(options).test(str);
  }
  throw new Error(`Invalid locale '${options.locale}'`);
}

View on GitHub (pinned to a79ff980ab)

Solutions

  1. Use a supported key such as 'en-US' (the default) or another key from the decimal locales table in src/lib/isDecimal.js
  2. Ensure the locale field is actually present in the options object (not clobbered by a merge with undefined)
  3. Wrap the call and fall back to default options on invalid locale
  4. Print the resolved options.locale to detect undefined values

Example fix

// before
validator.isDecimal(price, { locale: 'en' }); // throws
// after
validator.isDecimal(price, { locale: 'en-US' });
Defensive patterns

Strategy: validation

Validate before calling

const DECIMAL_LOCALES = ['en-US','en-ZA','en-AU','en-HK','en-IN','fr-FR','de-DE','pt-BR','es-ES','ar-AE','ar-EG','ar-JO','ar-KW','ar-LB','ar-SA','ar-TN','bg-BG','cs-CZ','da-DK','el-GR','en-CA','en-GB','en-IE','en-KE','en-MT','en-NG','en-NZ','en-PK','en-RW','en-SG','en-TZ','en-UG','zh-CN','zh-HK','fa-IR','gu-IN','hi-IN','id-ID','it-IT','kk-KZ','ko-KR','ja-JP','ms-MY','nb-NO','nl-NL','pl-PL','ru-RU','sl-SI','sr-RS','sv-SE','tk-TM','tr-TR','uk-UA','vi-VN'];
const locale = DECIMAL_LOCALES.includes(opts.locale) ? opts.locale : 'en-US';
validator.isDecimal(str, { ...opts, locale });

Type guard

const isValidDecimalLocale = (loc) => typeof loc === 'string' && DECIMAL_LOCALES.includes(loc);

Try / catch

let ok;
try { ok = validator.isDecimal(str, options); }
catch (e) { if (/^Invalid locale/.test(e.message)) { ok = validator.isDecimal(str, { locale: 'en-US', decimal_digits: '1,', force_decimal: false }); } else { throw e; } }

Prevention

When it happens

Trigger: isDecimal('12.34', { locale: 'en' }), isDecimal(x, { locale: undefined }) after merging options that overwrote the default, or passing locale names like 'pt' rather than keys such as 'pt-BR'.

Common situations: Passing an options object missing locale so a later merge leaves undefined; reusing Intl locale tags ('en-GB' is valid but 'en' is not); version changes to the supported locale set.

Related errors


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