{"record":{"id":"7a2ff9d8e160b2dc","repo":"Automattic/mongoose","slug":"invalid-validator-received-typeof-arg-arg","errorCode":null,"errorMessage":"Invalid validator. Received (${typeof arg}) ${arg}. See https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.validate()","messagePattern":"Invalid validator\\. Received \\((.+?)\\) (.+?)\\. See https://mongoosejs\\.com/docs/api/schematype\\.html#SchemaType\\.prototype\\.validate\\(\\)","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schemaType.js","lineNumber":1065,"sourceCode":"      properties = { message: message, type: type, validator: obj };\n    }\n\n    this.validators.push(properties);\n    return this;\n  }\n\n  let i;\n  let length;\n  let arg;\n\n  for (i = 0, length = arguments.length; i < length; i++) {\n    arg = arguments[i];\n    if (!utils.isPOJO(arg)) {\n      const msg = 'Invalid validator. Received (' + typeof arg + ') '\n        + arg\n        + '. See https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.validate()';\n\n      throw new MongooseError(msg);\n    }\n    this.validate(arg.validator, arg);\n  }\n\n  return this;\n};\n\n/**\n * Adds a required validator to this SchemaType. The validator gets added\n * to the front of this SchemaType's validators array using `unshift()`.\n *\n * #### Example:\n *\n *     const s = new Schema({ born: { type: Date, required: true })\n *\n *     // or with custom error message\n *\n *     const s = new Schema({ born: { type: Date, required: '{PATH} is required!' })","sourceCodeStart":1047,"sourceCodeEnd":1083,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schemaType.js#L1047-L1083","documentation":"Thrown by the base SchemaType.prototype.validate(). The method accepts either a function/RegExp as the first argument, or plain-object validator descriptors such as { validator: fn, message: '...' }. If an argument is neither a function, a RegExp, nor a POJO (e.g. a string, number, array, or class instance), Mongoose rejects it with this error instead of registering a broken validator.","triggerScenarios":"Calling schema.path('email').validate('isEmail') with a validator.js rule name as a string; passing an array of validators path.validate([fn1, fn2]); passing a wrapped/class instance, null, or undefined as the validator; calling validate(messageString) with only a message.","commonSituations":"Integrating validator.js by passing the rule name string instead of the function; refactoring old code that stored validators in arrays or Maps; copy-pasting a message string as the first argument.","solutions":["Pass the validator function directly: path.validate(fn) or path.validate(fn, 'message')","Or pass a plain-object descriptor: path.validate({ validator: fn, message: 'Invalid value', type: 'custom' })","For validator.js, import the function (e.g. import isEmail from 'validator/lib/isEmail') and pass the function itself","If passing multiple validators, pass multiple POJO arguments, not an array: path.validate({validator: a}, {validator: b})"],"exampleFix":"// before\nschema.path('email').validate('isEmail');\n// after\nimport { isEmail } from 'validator';\nschema.path('email').validate({ validator: isEmail, message: 'Invalid email address' });","handlingStrategy":"validation","validationCode":"const assertValidator = (v) => {\n  const ok = typeof v === 'function' ||\n    v instanceof RegExp ||\n    (v !== null && typeof v === 'object' && !Array.isArray(v) && utils.isPOJO(v));\n  if (!ok) throw new TypeError(`Invalid validator: ${typeof v} ${v}`);\n};\nassertValidator(input);\npath.validate(input);","typeGuard":"function isValidatorInput(v) {\n  return typeof v === 'function' ||\n    v instanceof RegExp ||\n    (Object.getPrototypeOf(v) === Object.prototype || Object.getPrototypeOf(v) === null);\n}","tryCatchPattern":"try { path.validate(arg); } catch (err) { if (/Invalid validator/.test(err.message)) throw new Error(`Bad validator for path ${path.path}`); throw err; }","preventionTips":["Standardize on the object form { validator, message } across the codebase","Never pass validator.js rule-name strings — import the functions","Add a unit test that registers every validator at schema-build time so bad arguments fail in CI, not at runtime"],"tags":["mongoose","schema","custom-validator","validate"],"backgroundTag":"invalid-validator-argument","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}