validatorjs/validator.js · error · Error
ignore should be instance of a String or RegExp
Error message
ignore should be instance of a String or RegExp
What it means
isAlpha(str, locale, ignore) accepts an optional `ignore` option that must be a String or RegExp listing characters to strip before validation. The library throws this error when `ignore` is truthy but of any other type (number, array, object, boolean), because it cannot safely remove the ignored characters.
Source
Thrown at src/lib/isAlpha.js:16
import assertString from './util/assertString';
import { alpha } from './alpha';
export default function isAlpha(_str, locale = 'en-US', options = {}) {
assertString(_str);
let str = _str;
const { ignore } = options;
if (ignore) {
if (ignore instanceof RegExp) {
str = str.replace(ignore, '');
} else if (typeof ignore === 'string') {
str = str.replace(new RegExp(`[${ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for ignore
} else {
throw new Error('ignore should be instance of a String or RegExp');
}
}
if (locale in alpha) {
return alpha[locale].test(str);
}
throw new Error(`Invalid locale '${locale}'`);
}
export const locales = Object.keys(alpha);
View on GitHub (pinned to a79ff980ab)
Solutions
- Convert the ignore value to a string or RegExp before calling: isAlpha(str, 'en-US', String(ignore))
- If the value is an array of characters, join it: ignore.join('')
- Guard with typeof before calling and skip or coerce invalid values
- Update call sites after a version upgrade where the options signature changed
Example fix
// before
validator.isAlpha(userInput, 'en-US', 123);
// after
const ignore = typeof allowed === 'string' ? allowed : (Array.isArray(allowed) ? allowed.join('') : '');
validator.isAlpha(userInput, 'en-US', ignore); Defensive patterns
Strategy: type-guard
Validate before calling
function isIgnorable(v) { return v == null || typeof v === 'string' || v instanceof RegExp; }
if (!isIgnorable(ignore)) throw new TypeError('ignore must be a string or RegExp'); Type guard
const isIgnoreOption = (v) => v == null || typeof v === 'string' || v instanceof RegExp;
Try / catch
let result;
try { result = validator.isAlpha(str, locale, ignore); }
catch (e) { if (/ignore should be/.test(e.message)) { result = validator.isAlpha(str, locale); } else { throw e; } } Prevention
- Never pass arrays as ignore; join characters into a string
- Coerce config-sourced options with String() before use
- Use real RegExp objects created in the same JS realm
- Add a unit test asserting the option type at your API boundary
When it happens
Trigger: Calling isAlpha('abc', 'en-US', 123), isAlpha(str, locale, ['a','b']), or passing a regex-like object that is not a real RegExp instance (e.g. from another realm or a plain object {source:'x'}).
Common situations: Passing options collected from JSON config/CLI args where numbers or arrays were not coerced to strings; passing a regex-like object from a different iframe/VM context; typos where the ignore value ends up undefined-shaped but non-string (e.g. an enum value).
Related errors
- ignore should be instance of a String or RegExp
- Invalid locale '${locale}'
- Invalid locale '${locale}'
- ${provider} is not a valid credit card provider.
- Invalid locale '${options.locale}'
AI-assisted analysis of validatorjs/validator.js@a79ff980ab (2026-08-31).
Data as JSON: /api/errors/4b325a546f7575ba.
Report an issue: GitHub.