According to the Department of Dino Zoo Control (DDZC), the zoo isn’t safe with more than 10 of any one type of Dinosaur.

At this point your Dinosaur model has name, count, and risk properties and you have some server tests written. One of those tests expects a warning message when adding more than 10 dinosaurs, but you receive the following error message:

AssertionError: expected '' to equal 'Cannot hold more than 10 dinosaurs.'

This error occurred because the Dinosaur model has no validation! You can ensure the safety of the zoo by adding a validator function.

In this exercise you’ll be using a custom validator function. It receives the value to validate as its first argument. It returns a Boolean, which is false if the value fails validation. Avoid arrow notation () =>. Using function() notation preserves the proper binding of this. Here’s the syntax:

// Define validator validate = function (value) { ... } // Add validator to Schema const DinosaurSchema = new Schema({ count: { type: Number, validate: [validator, 'custom err msg'] } });

Since validation is a model-level concern, you’ll need to test at the model layer. You can test validation like this:

  1. Create an instance of a model with validators and execute the validations with the validateSync method. Any validation errors will be stored in [instance].errors.[path], like dino.errors.count.
  2. Make assertions on [instance].errors.[path] and its properties.

For more information on validators visit the Validation section of the Mongoose guide: http://mongoosejs.com/docs/validation.html.



Using TDD, ensure that a dinosaur cannot have a count of more than 10. The count and risk paths have already been defined for you.

In dinosaur-test.js under it('is invalid with 11' construct a new instance of the Dinosaur model with

  • name 'T-rex'
  • count 11
  • risk 'High'

and store it in a variable dino.


Call dino.validateSync() after that.


If there are any errors, they will be stored in dino.errors.

  • Use assert.ok to confirm that dino.errors exists
  • Pass the custom error message 'model should have validation error' as a second argument to assert.ok

Run npm test and see the error message

AssertionError: model should have validation error: expected undefined to be truthy

The errors object is undefined, which means no error is thrown! In dinosaur.js add

validate: validator

to the count property. You’ll define the validator function soon.


Run npm test and see the error message

ReferenceError: validator is not defined

Above the DinosaurSchema declaration in dinosaur.js, define a function named validator with function(args) {} notation.


Run npm test and see the AssertionError message reappear. The validator function needs to return false to raise a validation error.


Make validator return false.


Run npm test and see green!

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?