Automattic/mongoose · error · MongooseError
Invalid validator. Received (${typeof arg}) ${arg}. See http
Error message
Invalid validator. Received (${typeof arg}) ${arg}. See https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.validate() What it means
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.
Source
Thrown at lib/schemaType.js:1065
properties = { message: message, type: type, validator: obj };
}
this.validators.push(properties);
return this;
}
let i;
let length;
let arg;
for (i = 0, length = arguments.length; i < length; i++) {
arg = arguments[i];
if (!utils.isPOJO(arg)) {
const msg = 'Invalid validator. Received (' + typeof arg + ') '
+ arg
+ '. See https://mongoosejs.com/docs/api/schematype.html#SchemaType.prototype.validate()';
throw new MongooseError(msg);
}
this.validate(arg.validator, arg);
}
return this;
};
/**
* Adds a required validator to this SchemaType. The validator gets added
* to the front of this SchemaType's validators array using `unshift()`.
*
* #### Example:
*
* const s = new Schema({ born: { type: Date, required: true })
*
* // or with custom error message
*
* const s = new Schema({ born: { type: Date, required: '{PATH} is required!' })View on GitHub (pinned to 49cdab0136)
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})
Example fix
// before
schema.path('email').validate('isEmail');
// after
import { isEmail } from 'validator';
schema.path('email').validate({ validator: isEmail, message: 'Invalid email address' }); Defensive patterns
Strategy: validation
Validate before calling
const assertValidator = (v) => {
const ok = typeof v === 'function' ||
v instanceof RegExp ||
(v !== null && typeof v === 'object' && !Array.isArray(v) && utils.isPOJO(v));
if (!ok) throw new TypeError(`Invalid validator: ${typeof v} ${v}`);
};
assertValidator(input);
path.validate(input); Type guard
function isValidatorInput(v) {
return typeof v === 'function' ||
v instanceof RegExp ||
(Object.getPrototypeOf(v) === Object.prototype || Object.getPrototypeOf(v) === null);
} Try / catch
try { path.validate(arg); } catch (err) { if (/Invalid validator/.test(err.message)) throw new Error(`Bad validator for path ${path.path}`); throw err; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Aggregate `near()` must be called with non-nullish argument
- Invalid sort() argument. Must be a string or object.
- If thenExpr or elseExpr is string, it must be either $$DESCE
- Field `${path}` is not in schema and strict mode is set to t
- Path "${path}" is not in schema, strict mode is `true`, and
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/7a2ff9d8e160b2dc.
Report an issue: GitHub.