Skip to content

Type rules

mongoose-guard checks each value against its schema type without converting it. This page lists, for every built-in Mongoose type, exactly which JSON values pass. Any value can also be null, except a plain nested object. Custom SchemaTypes from plugins skip this check and go straight to Mongoose.

Schema typeAcceptsRejected examples
Stringany string42, true, {}
Numberfinite numbers"42", NaN, Infinity
Schema.Types.Doublefinite numbers"4.5"
Schema.Types.Int32integers from −2,147,483,648 to 2,147,483,6474.5, 2147483648
BigInta bigint, a safe integer number, or an integer string like "9007199254740993"4.2, "1.5"
Booleantrue, false"true", 0, 1
Datean ISO 8601 string ("2024-05-01", "2024-05-01T10:20:30Z", "2024-05-01T10:20:30.000+05:30") or a valid Date object"yesterday", 1714557630000, "05/01/2024"
Schema.Types.ObjectIda 24-character hex string, or an ObjectId instance"123", 42
Schema.Types.Decimal128a finite number, a decimal string like "10.50" or "1e3", or a Decimal128 instance"ten", true
Schema.Types.UUIDa UUID string like "123e4567-e89b-42d3-a456-426614174000""not-a-uuid"
Buffera string, or a Uint8Array / Buffer42
Schema.Types.Mixedanything, scanned for unsafe keysan object with a $ key at any depth
Schema shapeAcceptsThen checks
nested object { bio: String }a plain object (not null)each key against the nested fields
sub-schema addressSchemaa plain object or nulleach key against the sub-schema
array [String]an array or nullevery element against the element type
nested array [[Number]]an array of arraysevery element at every level
document array [addressSchema]an array or nullevery element must be a plain object, then is checked against the sub-schema
Map { type: Map, of: Number }a plain object or nullevery key for safety and every value against of
Map with no ofa plain object or nullevery key for safety; values are treated as Mixed

“Plain object” means a JSON object. Instances of classes, such as new Date() where an object is expected, are rejected.

null passes the type check for every field except a plain nested object. Whether it’s acceptable is then up to your schema: a required field rejects null with invalid_value and rule: "required".

JSON has no date or ObjectId type, so clients have to send them as strings. Accepting a well-formed string for those types isn’t casting, it’s the only way they can arrive. What mongoose-guard refuses is the loose stuff Mongoose also accepts, like a timestamp number for a Date or any 12-character string for an ObjectId.

If a field uses a SchemaType mongoose-guard doesn’t know, such as one from a plugin, the type check is skipped for that field. Mongoose still casts and validates it in the last step, and a failed cast comes back as invalid_type with Mongoose’s message.