Each error has a .path, describing the full path of the property that failed validation, and a .message describing the error.
errors[0].path //=> 'address.street'errors[0].message //=> 'address.street is required.'
Custom error messages
You can override the default error messages by passing an object to Schema#message().
const post = new Schema({ title: { required: true }})post.message({ required: (path) => `${path} can not be empty.`})const [error] = post.validate({})assert(error.message = 'title can not be empty.')
It is also possible to define messages for individual properties:
const post = new Schema({ title: { required: true, message: 'Title is required.' }})
And for individual validators:
const post = new Schema({ title: { type: String, required: true, message: { type: 'Title must be a string.', required: 'Title is required.' } }})
Nesting
Objects and arrays can be nested as deep as you want:
In this example, the pet.type property will be interpreted as a type rule, and the validations will not work as intended. To work around this we could use the slightly more verbose properties rule:
In this case the type property of pets.properties will be interpreted as a nested property, and the validations will work as intended.
Custom validators
Custom validators can be defined by passing an object with named validators to .use:
const hexColor = val => /^#[0-9a-fA-F]$/.test(val)const car = new Schema({ color: { type: String, use: { hexColor } }})
Define a custom error message for the validator:
car.message({ hexColor: path => `${path} must be a valid color.`})
Custom types
Pass a constructor to .type to validate against a custom type:
class Car {}const user = new Schema({ car: { type: Car }})
Chainable API
If you want to avoid constructing large objects, you can add paths to a schema by using the chainable API:
const user = new Schema()user .path('username').type(String).required() .path('address.zip').type(String).required()
Array elements can be defined by using $ as a placeholder for indices:
const user = new Schema()user.path('pets.$').type(String)
This is equivalent to writing
const user = new Schema({ pets: [{ type: String }]})
Typecasting
Values can be automatically typecast before validation.
To enable typecasting, pass an options object to the Schema constructor with typecast set to true.
const user = new Schema(definition, { typecast: true })
You can override this setting by passing an option to .validate().
user.validate(obj, { typecast: false })
To typecast custom types, you can register a typecaster:
class Car {}const user = new Schema({ car: { type: Car }})user.typecaster({ Car: (val) => new Car(val)})
Property stripping
By default, all values not defined in the schema will be stripped from the object.
Set .strip = false on the options object to disable this behavior. This will likely be changed in a future version.
Strict mode
When strict mode is enabled, properties that are not defined in the schema will trigger a validation error. Set .strict = true on the options object to enable strict mode.
A property instance gets returned whenever you call schema.path().
Properties are also created internally when an object is passed to the Schema constructor.
Validate using named functions from the given object.
Error messages can be defined by providing an object with
named error messages/generators to schema.message()
The message generator receives the value being validated,
the object it belongs to and any additional arguments.
Parameters
fnsObject object with named validation functions to call
Examples
const schema = new Schema()const prop = schema.path('some.path')schema.message({ binary: (path, ctx) => `${path} must be binary.`, bits: (path, ctx, bits) => `${path} must be ${bits}-bit`})prop.use({ binary: (val, ctx) => /^[01]+$/i.test(val), bits: [(val, ctx, bits) => val.length == bits, 32]})