{"record":{"id":"4b325a546f7575ba","repo":"validatorjs/validator.js","slug":"ignore-should-be-instance-of-a-string-or-regexp","errorCode":null,"errorMessage":"ignore should be instance of a String or RegExp","messagePattern":"ignore should be instance of a String or RegExp","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/isAlpha.js","lineNumber":16,"sourceCode":"import assertString from './util/assertString';\nimport { alpha } from './alpha';\n\nexport default function isAlpha(_str, locale = 'en-US', options = {}) {\n  assertString(_str);\n\n  let str = _str;\n  const { ignore } = options;\n\n  if (ignore) {\n    if (ignore instanceof RegExp) {\n      str = str.replace(ignore, '');\n    } else if (typeof ignore === 'string') {\n      str = str.replace(new RegExp(`[${ignore.replace(/[-[\\]{}()*+?.,\\\\^$|#\\\\s]/g, '\\\\$&')}]`, 'g'), ''); // escape regex for ignore\n    } else {\n      throw new Error('ignore should be instance of a String or RegExp');\n    }\n  }\n\n  if (locale in alpha) {\n    return alpha[locale].test(str);\n  }\n  throw new Error(`Invalid locale '${locale}'`);\n}\n\nexport const locales = Object.keys(alpha);\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/validatorjs/validator.js/blob/a79ff980ab14257e795332989e497bdff3218e87/src/lib/isAlpha.js#L1-L27","documentation":"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.","triggerScenarios":"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'}).","commonSituations":"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).","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"],"exampleFix":"// before\nvalidator.isAlpha(userInput, 'en-US', 123);\n// after\nconst ignore = typeof allowed === 'string' ? allowed : (Array.isArray(allowed) ? allowed.join('') : '');\nvalidator.isAlpha(userInput, 'en-US', ignore);","handlingStrategy":"type-guard","validationCode":"function isIgnorable(v) { return v == null || typeof v === 'string' || v instanceof RegExp; }\nif (!isIgnorable(ignore)) throw new TypeError('ignore must be a string or RegExp');","typeGuard":"const isIgnoreOption = (v) => v == null || typeof v === 'string' || v instanceof RegExp;","tryCatchPattern":"let result;\ntry { result = validator.isAlpha(str, locale, ignore); }\ncatch (e) { if (/ignore should be/.test(e.message)) { result = validator.isAlpha(str, locale); } else { throw e; } }","preventionTips":["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"],"tags":["validation","options","type-error"],"backgroundTag":"invalid-option-type","analyzedSha":"a79ff980ab14257e795332989e497bdff3218e87","analyzedAt":"2026-08-31T21:42:31.416Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}