Validation

Before we get into the specifics of validation syntax, please keep the following rules in mind:

  • Validation is defined in the SchemaType
  • Validation is middleware. Mongoose registers validation as a pre('save') hook on every schema by default.
  • You can disable automatic validation before save by setting the validateBeforeSave option
  • You can manually run validation using doc.validate(callback) or doc.validateSync()
  • You can manually mark a field as invalid (causing validation to fail) by using doc.invalidate(...)
  • Validators are not run on undefined values. The only exception is the required validator.
  • Validation is asynchronously recursive; when you call Model#save, sub-document validation is executed as well. If an error occurs, your Model#save callback receives it
  • Validation is customizable
  1. const schema = new Schema({
  2. name: {
  3. type: String,
  4. required: true
  5. }
  6. });
  7. const Cat = db.model('Cat', schema);
  8. // This cat has no name :(
  9. const cat = new Cat();
  10. cat.save(function(error) {
  11. assert.equal(error.errors['name'].message,
  12. 'Path `name` is required.');
  13. error = cat.validateSync();
  14. assert.equal(error.errors['name'].message,
  15. 'Path `name` is required.');
  16. });

Built-in Validators

Mongoose has several built-in validators.

Each of the validator links above provide more information about how to enable them and customize their error messages.

  1. const breakfastSchema = new Schema({
  2. eggs: {
  3. type: Number,
  4. min: [6, 'Too few eggs'],
  5. max: 12
  6. },
  7. bacon: {
  8. type: Number,
  9. required: [true, 'Why no bacon?']
  10. },
  11. drink: {
  12. type: String,
  13. enum: ['Coffee', 'Tea'],
  14. required: function() {
  15. return this.bacon > 3;
  16. }
  17. }
  18. });
  19. const Breakfast = db.model('Breakfast', breakfastSchema);
  20. const badBreakfast = new Breakfast({
  21. eggs: 2,
  22. bacon: 0,
  23. drink: 'Milk'
  24. });
  25. let error = badBreakfast.validateSync();
  26. assert.equal(error.errors['eggs'].message,
  27. 'Too few eggs');
  28. assert.ok(!error.errors['bacon']);
  29. assert.equal(error.errors['drink'].message,
  30. '`Milk` is not a valid enum value for path `drink`.');
  31. badBreakfast.bacon = 5;
  32. badBreakfast.drink = null;
  33. error = badBreakfast.validateSync();
  34. assert.equal(error.errors['drink'].message, 'Path `drink` is required.');
  35. badBreakfast.bacon = null;
  36. error = badBreakfast.validateSync();
  37. assert.equal(error.errors['bacon'].message, 'Why no bacon?');

Custom Error Messages

You can configure the error message for individual validators in your schema. There are two equivalent ways to set the validator error message:

  • Array syntax: min: [6, 'Must be at least 6, got {VALUE}']
  • Object syntax: enum: { values: ['Coffee', 'Tea'], message: '{VALUE} is not supported' }

Mongoose also supports rudimentary templating for error messages. Mongoose replaces {VALUE} with the value being validated.

  1. const breakfastSchema = new Schema({
  2. eggs: {
  3. type: Number,
  4. min: [6, 'Must be at least 6, got {VALUE}'],
  5. max: 12
  6. },
  7. drink: {
  8. type: String,
  9. enum: {
  10. values: ['Coffee', 'Tea'],
  11. message: '{VALUE} is not supported'
  12. }
  13. }
  14. });
  15. const Breakfast = db.model('Breakfast', breakfastSchema);
  16. const badBreakfast = new Breakfast({
  17. eggs: 2,
  18. drink: 'Milk'
  19. });
  20. let error = badBreakfast.validateSync();
  21. assert.equal(error.errors['eggs'].message,
  22. 'Must be at least 6, got 2');
  23. assert.equal(error.errors['drink'].message, 'Milk is not supported');

The unique Option is Not a Validator

A common gotcha for beginners is that the unique option for schemas is not a validator. It’s a convenient helper for building MongoDB unique indexes. See the FAQ for more information.

  1. const uniqueUsernameSchema = new Schema({
  2. username: {
  3. type: String,
  4. unique: true
  5. }
  6. });
  7. const U1 = db.model('U1', uniqueUsernameSchema);
  8. const U2 = db.model('U2', uniqueUsernameSchema);
  9. const dup = [{ username: 'Val' }, { username: 'Val' }];
  10. U1.create(dup, err => {
  11. // Race condition! This may save successfully, depending on whether
  12. // MongoDB built the index before writing the 2 docs.
  13. });
  14. // You need to wait for Mongoose to finish building the `unique`
  15. // index before writing. You only need to build indexes once for
  16. // a given collection, so you normally don't need to do this
  17. // in production. But, if you drop the database between tests,
  18. // you will need to use `init()` to wait for the index build to finish.
  19. U2.init().
  20. then(() => U2.create(dup)).
  21. catch(error => {
  22. // Will error, but will *not* be a mongoose validation error, it will be
  23. // a duplicate key error.
  24. // See: https://masteringjs.io/tutorials/mongoose/e11000-duplicate-key
  25. assert.ok(error);
  26. assert.ok(!error.errors);
  27. assert.ok(error.message.indexOf('duplicate key error') !== -1);
  28. });

Custom Validators

If the built-in validators aren’t enough, you can define custom validators to suit your needs.

Custom validation is declared by passing a validation function. You can find detailed instructions on how to do this in the SchemaType#validate() API docs.

  1. const userSchema = new Schema({
  2. phone: {
  3. type: String,
  4. validate: {
  5. validator: function(v) {
  6. return /\d{3}-\d{3}-\d{4}/.test(v);
  7. },
  8. message: props => `${props.value} is not a valid phone number!`
  9. },
  10. required: [true, 'User phone number required']
  11. }
  12. });
  13. const User = db.model('user', userSchema);
  14. const user = new User();
  15. let error;
  16. user.phone = '555.0123';
  17. error = user.validateSync();
  18. assert.equal(error.errors['phone'].message,
  19. '555.0123 is not a valid phone number!');
  20. user.phone = '';
  21. error = user.validateSync();
  22. assert.equal(error.errors['phone'].message,
  23. 'User phone number required');
  24. user.phone = '201-555-0123';
  25. // Validation succeeds! Phone number is defined
  26. // and fits `DDD-DDD-DDDD`
  27. error = user.validateSync();
  28. assert.equal(error, null);

Async Custom Validators

Custom validators can also be asynchronous. If your validator function returns a promise (like an async function), mongoose will wait for that promise to settle. If the returned promise rejects, or fulfills with the value false, Mongoose will consider that a validation error.

  1. const userSchema = new Schema({
  2. name: {
  3. type: String,
  4. // You can also make a validator async by returning a promise.
  5. validate: () => Promise.reject(new Error('Oops!'))
  6. },
  7. email: {
  8. type: String,
  9. // There are two ways for an promise-based async validator to fail:
  10. // 1) If the promise rejects, Mongoose assumes the validator failed with the given error.
  11. // 2) If the promise resolves to `false`, Mongoose assumes the validator failed and creates an error with the given `message`.
  12. validate: {
  13. validator: () => Promise.resolve(false),
  14. message: 'Email validation failed'
  15. }
  16. }
  17. });
  18. const User = db.model('User', userSchema);
  19. const user = new User();
  20. user.email = 'test@test.co';
  21. user.name = 'test';
  22. user.validate().catch(error => {
  23. assert.ok(error);
  24. assert.equal(error.errors['name'].message, 'Oops!');
  25. assert.equal(error.errors['email'].message, 'Email validation failed');
  26. });

Validation Errors

Errors returned after failed validation contain an errors object whose values are ValidatorError objects. Each ValidatorError has kind, path, value, and message properties. A ValidatorError also may have a reason property. If an error was thrown in the validator, this property will contain the error that was thrown.

  1. const toySchema = new Schema({
  2. color: String,
  3. name: String
  4. });
  5. const validator = function(value) {
  6. return /red|white|gold/i.test(value);
  7. };
  8. toySchema.path('color').validate(validator,
  9. 'Color `{VALUE}` not valid', 'Invalid color');
  10. toySchema.path('name').validate(function(v) {
  11. if (v !== 'Turbo Man') {
  12. throw new Error('Need to get a Turbo Man for Christmas');
  13. }
  14. return true;
  15. }, 'Name `{VALUE}` is not valid');
  16. const Toy = db.model('Toy', toySchema);
  17. const toy = new Toy({ color: 'Green', name: 'Power Ranger' });
  18. toy.save(function(err) {
  19. // `err` is a ValidationError object
  20. // `err.errors.color` is a ValidatorError object
  21. assert.equal(err.errors.color.message, 'Color `Green` not valid');
  22. assert.equal(err.errors.color.kind, 'Invalid color');
  23. assert.equal(err.errors.color.path, 'color');
  24. assert.equal(err.errors.color.value, 'Green');
  25. // This is new in mongoose 5. If your validator throws an exception,
  26. // mongoose will use that message. If your validator returns `false`,
  27. // mongoose will use the 'Name `Power Ranger` is not valid' message.
  28. assert.equal(err.errors.name.message,
  29. 'Need to get a Turbo Man for Christmas');
  30. assert.equal(err.errors.name.value, 'Power Ranger');
  31. // If your validator threw an error, the `reason` property will contain
  32. // the original error thrown, including the original stack trace.
  33. assert.equal(err.errors.name.reason.message,
  34. 'Need to get a Turbo Man for Christmas');
  35. assert.equal(err.name, 'ValidationError');
  36. });

Cast Errors

Before running validators, Mongoose attempts to coerce values to the correct type. This process is called casting the document. If casting fails for a given path, the error.errors object will contain a CastError object.

Casting runs before validation, and validation does not run if casting fails. That means your custom validators may assume v is null, undefined, or an instance of the type specified in your schema.

  1. const vehicleSchema = new mongoose.Schema({
  2. numWheels: { type: Number, max: 18 }
  3. });
  4. const Vehicle = db.model('Vehicle', vehicleSchema);
  5. const doc = new Vehicle({ numWheels: 'not a number' });
  6. const err = doc.validateSync();
  7. err.errors['numWheels'].name; // 'CastError'
  8. // 'Cast to Number failed for value "not a number" at path "numWheels"'
  9. err.errors['numWheels'].message;

Required Validators On Nested Objects

Defining validators on nested objects in mongoose is tricky, because nested objects are not fully fledged paths.

  1. let personSchema = new Schema({
  2. name: {
  3. first: String,
  4. last: String
  5. }
  6. });
  7. assert.throws(function() {
  8. // This throws an error, because 'name' isn't a full fledged path
  9. personSchema.path('name').required(true);
  10. }, /Cannot.*'required'/);
  11. // To make a nested object required, use a single nested schema
  12. const nameSchema = new Schema({
  13. first: String,
  14. last: String
  15. });
  16. personSchema = new Schema({
  17. name: {
  18. type: nameSchema,
  19. required: true
  20. }
  21. });
  22. const Person = db.model('Person', personSchema);
  23. const person = new Person();
  24. const error = person.validateSync();
  25. assert.ok(error.errors['name']);

Update Validators

In the above examples, you learned about document validation. Mongoose also supports validation for update(), updateOne(), updateMany(), and findOneAndUpdate() operations. Update validators are off by default - you need to specify the runValidators option.

To turn on update validators, set the runValidators option for update(), updateOne(), updateMany(), or findOneAndUpdate(). Be careful: update validators are off by default because they have several caveats.

  1. const toySchema = new Schema({
  2. color: String,
  3. name: String
  4. });
  5. const Toy = db.model('Toys', toySchema);
  6. Toy.schema.path('color').validate(function(value) {
  7. return /red|green|blue/i.test(value);
  8. }, 'Invalid color');
  9. const opts = { runValidators: true };
  10. Toy.updateOne({}, { color: 'not a color' }, opts, function(err) {
  11. assert.equal(err.errors.color.message,
  12. 'Invalid color');
  13. });

Update Validators and this

There are a couple of key differences between update validators and document validators. In the color validation function above, this refers to the document being validated when using document validation. However, when running update validators, the document being updated may not be in the server’s memory, so by default the value of this is not defined.

  1. const toySchema = new Schema({
  2. color: String,
  3. name: String
  4. });
  5. toySchema.path('color').validate(function(value) {
  6. // When running in `validate()` or `validateSync()`, the
  7. // validator can access the document using `this`.
  8. // Does **not** work with update validators.
  9. if (this.name.toLowerCase().indexOf('red') !== -1) {
  10. return value !== 'red';
  11. }
  12. return true;
  13. });
  14. const Toy = db.model('ActionFigure', toySchema);
  15. const toy = new Toy({ color: 'red', name: 'Red Power Ranger' });
  16. const error = toy.validateSync();
  17. assert.ok(error.errors['color']);
  18. const update = { color: 'red', name: 'Red Power Ranger' };
  19. const opts = { runValidators: true };
  20. Toy.updateOne({}, update, opts, function(error) {
  21. // The update validator throws an error:
  22. // "TypeError: Cannot read property 'toLowerCase' of undefined",
  23. // because `this` is **not** the document being updated when using
  24. // update validators
  25. assert.ok(error);
  26. });

The context option

The context option lets you set the value of this in update validators to the underlying query.

  1. toySchema.path('color').validate(function(value) {
  2. // When running update validators, `this` refers to the query object.
  3. if (this.getUpdate().$set.name.toLowerCase().indexOf('red') !== -1) {
  4. return value === 'red';
  5. }
  6. return true;
  7. });
  8. const Toy = db.model('Figure', toySchema);
  9. const update = { color: 'blue', name: 'Red Power Ranger' };
  10. // Note the context option
  11. const opts = { runValidators: true, context: 'query' };
  12. Toy.updateOne({}, update, opts, function(error) {
  13. assert.ok(error.errors['color']);
  14. });

Update Validators Only Run On Updated Paths

The other key difference is that update validators only run on the paths specified in the update. For instance, in the below example, because ‘name’ is not specified in the update operation, update validation will succeed.

When using update validators, required validators only fail when you try to explicitly $unset the key.

  1. const kittenSchema = new Schema({
  2. name: { type: String, required: true },
  3. age: Number
  4. });
  5. const Kitten = db.model('Kitten', kittenSchema);
  6. const update = { color: 'blue' };
  7. const opts = { runValidators: true };
  8. Kitten.updateOne({}, update, opts, function() {
  9. // Operation succeeds despite the fact that 'name' is not specified
  10. });
  11. const unset = { $unset: { name: 1 } };
  12. Kitten.updateOne({}, unset, opts, function(err) {
  13. // Operation fails because 'name' is required
  14. assert.ok(err);
  15. assert.ok(err.errors['name']);
  16. });

Update Validators Only Run For Some Operations

One final detail worth noting: update validators only run on the following update operators:

  • $set
  • $unset
  • $push (>= 4.8.0)
  • $addToSet (>= 4.8.0)
  • $pull (>= 4.12.0)
  • $pullAll (>= 4.12.0)

For instance, the below update will succeed, regardless of the value of number, because update validators ignore $inc.

Also, $push, $addToSet, $pull, and $pullAll validation does not run any validation on the array itself, only individual elements of the array.

  1. const testSchema = new Schema({
  2. number: { type: Number, max: 0 },
  3. arr: [{ message: { type: String, maxlength: 10 } }]
  4. });
  5. // Update validators won't check this, so you can still `$push` 2 elements
  6. // onto the array, so long as they don't have a `message` that's too long.
  7. testSchema.path('arr').validate(function(v) {
  8. return v.length < 2;
  9. });
  10. const Test = db.model('Test', testSchema);
  11. let update = { $inc: { number: 1 } };
  12. const opts = { runValidators: true };
  13. Test.updateOne({}, update, opts, function() {
  14. // There will never be a validation error here
  15. update = { $push: [{ message: 'hello' }, { message: 'world' }] };
  16. Test.updateOne({}, update, opts, function(error) {
  17. // This will never error either even though the array will have at
  18. // least 2 elements.
  19. });
  20. });