FAQ: Model Testing Patterns - Path Validation II

This community-built FAQ covers the “Path Validation II” exercise from the lesson “Model Testing Patterns”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Testing for Web Development

FAQs on the exercise Path Validation II

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A little confused here:

This to me makes sense, I guess because the Schema calls the validator function ( I think?)

const **validator** = function(){

  return false

}

const DinosaurSchema = new Schema(



  {

    name: {type: String, required: true},

    count: {

      type: Number,

      // Define validate property here

      validate:**validator,**

    },

    risk: {type: String}

  }

);

But it appears you remove the call for the validator function. So how does it know to call it and pass 10 into it? What if you had min and max requirement for count, then how does that get handled by validator?

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const validator = function(value) {

  return value <= 10; 

};

const DinosaurSchema = new Schema(

  {

    name: {type: String, required: true},

    count: {

      type: Number,

      max: [10, 'Cannot hold more than 10 dinosaurs.']

    },

    risk: {type: String}

  }

);

module.exports = mongoose.model('Dinosaur', DinosaurSchema);

Also what’s with dino.validateSync(); ? Never expalined. Documentation says its manually testing validation, but how ? What’s happening.

In the code you listed below

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const validator = function(value) {

  return value <= 10; 

};

const DinosaurSchema = new Schema(

  {

    name: {type: String, required: true},

    count: {

      type: Number,

      max: [10, 'Cannot hold more than 10 dinosaurs.']

    },

    risk: {type: String}

  }

);

module.exports = mongoose.model('Dinosaur', DinosaurSchema);

This following section can actually be removed from the above code.

const validator = function(value) {

  return value <= 10; 

};

When you changed over from using validate to using the built in max validator that section was no longer needed.

I know they didn’t really explain the whole validate:validator but basically validator is a function that takes the input from the path in this case count and uses it as the value for the function. I hope this helped.

Also

dino.validateSync()

runs all the validation tests that are present in the schema. This could be functions you made like validator using the validate validator keyword like this

const DinosaurSchema = new Schema(

  {

    name: {type: String, required: true},

    count: {

      type: Number,

      validate: validator

    },

    risk: {type: String}

  }

);

or by using a built-in validator such max

const DinosaurSchema = new Schema(

  {

    name: {type: String, required: true},

    count: {

      type: Number,

      max: [10, 'Cannot hold more than 10 dinosaurs.']

    },

    risk: {type: String}

  }

);

If I may add to what @alangervin3193293788 already explained, I went digging in the Mongoose documentation which explains max as such:

max: Number, creates a validator that checks if the value is less than or equal to the given maximum.

So yeah, no need for that external validator anymore :wink: