‫So, Mongoose offers us developers
‫very powerful ways of validating data
‫that's coming into our model.
‫And so, in this video you're gonna learn
‫all about data validation with Mongoose.
‫Now, what exactly does validation actually mean?
‫Well, validation is basically checking if the entered
‫values are in the right format for each field
‫in our document schema, and also that values
‫have actually been entered for all of the required fields.
‫Now, on the other hand, we also have sanitization,
‫which is to ensure that the inputted data is
‫basically clean, so that there is no malicious code
‫being injected into our database,
‫or into the application itself.
‫So, in that step we remove unwanted characters,
‫or even code, from the input data, all right?
‫And this is actually a crucial step, like,
‫a golden standard in back-end development.
‫To never, ever accept input data
‫coming from a user as it is.
‫So, we always need to sanitize that incoming data.
‫But, anyway, I will leave data sanitization
‫for the security section of the course,
‫so that in this lecture
‫we can focus entirely on data validation.
‫And we are doing this data validation
‫right here on the model.
‫And that, again, is because of the fat model
‫and thin controller philosophy,
‫which makes the model the perfect place
‫to perform validation, right?
‫And, in fact, Mongoose already comes with a bunch
‫of validation tools out of the box.
‫And so, let's now actually do some data validation here.
‫And actually, we already did.
‫So, when we used this require here, that is actually
‫already a built in data validator, all right?
‫So you see that we use a validator like required
‫right here in the schema type options.
‫And required is actually available for all the data types.
‫So, not just strings, but really everything.
‫Numbers, Booleans, dates, or really,
‫whatever type you're using.
‫Then we also have unique here, but please note,
‫that this is actually, technically, not a validator.
‫It will still produce an error
‫when we have a duplicate name,
‫but again, this is not really a validator, all right?
‫But, required is a validator,
‫and so I'm going to show you now another one
‫which is specifically just for strings,
‫which is called max length and also min length.
‫So, we have max length
‫and as the name says, we use this to specify
‫the maximum length that a string can have.
‫And if it's longer than that, well then,
‫it's going to produce an error, all right?
‫So, let's do that just like we did before with required,
‫where we specified an array, and then set the value.
‫So, let's say we want 40 as the maximum string length,
‫and then, we add the error after that.
‫So, basically, the error message that we get.
‫So, a tour name must have
‫less or equal then 40 characters.
‫Okay, so, that is max length,
‫and then we also have min length.
‫All right, and so let's set that one to 10,
‫so a tour name must have more or equal then 10 characters.
‫Okay, and so just like this we have these two validators
‫that are actually only available on strings, okay?
‫And, so let's now actually go ahead and try that.
‫So, I'm going to create a new tour here.
‫And, this is not a secret tour,
‫but then here the rest does not matter.
‫I will just call this one Test Tour,
‫and let's see how many characters we have,
‫so four plus one, plus four,
‫so we only have nine characters.
‫And so, we should now expect to actually get an error.
‫So, let's try that out.
‫And, indeed, a tour name must have
‫more or equal then 10 characters.
‫Great, but now what about updating tours?
‫So, let's get one of these here.
‫So, we have this test tour two here,
‫so let's try to update this one.
‫So, we go to our update tour route.
‫And now let's here, change the name to Test,
‫send the request, and, we get the same error.
‫Now, this only works because of a setting
‫that we set way back,
‫when we implemented this updating handler.
‫So, let me quickly show that to you in the tour controller.
‫Delete and update, and so it's because of this option
‫here runValidators, set to true
‫that the validators are run again.
‫If I set it now to false,
‫then Mongoose should happily accept this name.
‫And indeed, it does, all right.
‫So, let's set it back to something longer,
‫it doesn't matter,
‫okay, and if I set it back to true,
‫then it should not accept it,
‫and indeed, we get our error back, all right?
‫So, just wanted to quickly
‫show you the fact of that setting.
‫All right, and so, yeah, these are now working.
‫Let's try a couple of more validators here.
‫For example, now on numbers.
‫So, let's go here to the ratings average.
‫And we know that the rating must always
‫be between one and zero, and we know that a rating
‫must always be between one and five, okay?
‫And so, very similar to the min and max length,
‫on numbers we simply have min and max.
‫So, the minimum that we want is one,
‫and then again our error message.
‫So, rating must be above 1.0, let's say.
‫And then the max is five.
‫Must be below 5.0, okay,
‫and let's quickly test this one as well.
‫So, creating a new tour, let's call it the Test Tour Amazing
‫because it has an average rating of six.
‫Okay, so let's see what happens then,
‫and indeed, the rating must be below five.
‫Okay, and so, that again is not going to work,
‫and of course it cannot be zero, either.
‫So, it must be above one, and of course,
‫with four, it is gonna work, and indeed, here is our tour.
‫Next up, I want to restrict this difficulty value here
‫to only three difficulties.
‫So, easy, medium, and difficult.
‫And if the user puts in something else,
‫then it's not going to work.
‫So, let's try that out.
‫So, the validator that we use for that is called enum,
‫okay, and so, here we can pass an array
‫of the values, basically, that are allowed.
‫So, we have easy, we have medium, and we have difficult.
‫All right, now we also want to specify our error message
‫here, but right now that's not really possible, right?
‫I mean, if we added another argument here,
‫then that would not be for the error message,
‫it would of course be for yet another possible value, okay?
‫And so, the solution that we need to do here,
‫is to create yet another object here,
‫and then, actually specify that these here are the values.
‫And then, our message, all right?
‫And so, this is actually how it really works.
‫This notation here is really just a shorthand for
‫this complete object here, all right?
‫So, difficulty is either: easy, medium, or difficult.
‫Okay, and so, that is a very nice, very handy validator
‫that is automatically available on all strings, okay,
‫so, don't try to use this one here on numbers, for example.
‫This is only for strings.
‫And, by the way this min and max here
‫is actually not only for numbers,
‫but it's also gonna work with dates, all right?
‫Anyway, let's now finally test this out as well.
‫So, we need to change the name,
‫and in here let's put something else,
‫all right, and so indeed, we get our error.
‫If we now put easy, then it works.
‫Okay, and that's actually all I wanted to show you
‫here in this lecture,
‫but there are actually a bunch of other validators.
‫For example, on strings you have a match validator
‫in order to check if the input
‫matches a given regular expression.
‫But, I believe that these ones that I just showed you here
‫are the most important built in validators.
‫And, for a complete list of all of them,
‫of course, you can always check out the documentation.
‫Now we can also specify our own validators,
‫and so, that is exactly what we will do
‫right in the next video.