Now as we mentioned earlier, by using and incorporating Mongoose into our app it makes validation of 1
data entry a lot easier. 2
Instead of having to write our own assert statements all over the place and specifying specific things, 3
we can actually use Mongoose's built in validation checks to make it a lot easier and a lot quicker to 4
use. 5
So I'm going to delete all of this part 6
where we have the kiwi, orange, banana and the insertMany. 7
Now if you want to keep it around for reference then just go ahead and comment it out. 8
If you scroll to the top where we created our fruit schema, one of the things that you can do is instead 9
of having the value of each of these key value pairs being simply just the data type 10
because this is the only thing that is strictly required when you are creating the schema, 11
instead you can actually add in some validation. 12
So let's go ahead and delete that data type and instead I'm going to open up a set of curly braces. 13
Now inside the curly braces I can add back that type which is going to be Number but I can also add 14
some validation. 15
So for example, what if when we're adding new data to our fruits collection the rating that we give to 16
each fruit we want a limited between 1 and 10? 17
So if somebody gives a rating of 99, then that shouldn't be valid right? 18
So how would we do that using Mongoose? 19
Well if we head over to the Mongoose documentation and you click on validation, then you can see that 20
there's a whole page dedicated to explaining how you can use the built in validators that Mongoose has 21
in order to make your data comply with a certain format and validate your data to keep it clean. 22
Down here you can see that all schema types have the built in required validator. 23
So you can make a particular property required. That is usually for example if you were going to implement 24
an ID, then it probably should be required. 25
But there's other things such as for the number data types, 26
you can have a minimum and maximum validators. 27
And if we click on that, it explains to us how you can use it. The minimum is going to be a number 28
and it creates a validator that checks if the value is greater than or equal to the given minimum. 29
And max does the same thing for the maximum. 30
So that means over here for our rating, in order to keep all the new data that we enter into our database 31
have a rating that's between 1 and 10 32
then we can specify the min as 1 and the max as 10. 33
So now if we hit save and let's just change this part to have a rating of, I don't kow, 34 34
right? 35
And let's uncomment the fruit.save to save it in there and comment out the person.save so 36
that we don't save any new persons, then if we go back and run on node app.js, you can see we get 37
a error. And it tells us that there's a validation error 38
and the problem is that fruit validation failed. 39
The rating is 34 and it's more than the maximum allowed value which is 10. 40
And when we look at the fruits that are being printed out, you can see that new fruit, the apple, didn't 41
get added again because this is a fatal error. 42
So it prevents data that doesn't match the validation to be inserted into our database thus keeping 43
our database sanitized and clean and have all of the data in a format that we expect it to be. 44
Now what if accidentally I added in a new document and I forgot to give it a name? 45
So for example I was adding some peaches in there and peaches are lovely, so I'm going to give it a rating 46
of 10. And let's just give it a review as well 47
"Peaches are so yummy!". 48
And let's hit save and try to save that fruit into our database, 49
well nothing bad happens. 50
If you check through our database you can see that that new thing got added right at the bottom here. 51
It has a review, 52
it has a rating but it doesn't have a name which is quite bad 53
if we want to loop through our data and print out everything that has a name right? It doesn't work and 54
it'll break our code later on down the line. 55
So as a challenge, I want you to make the name field required and this means that you might have to look 56
through the documentation on Mongoose. But you should make it such that when we try to add a new fruit 57
that doesn't have a name then it will refuse to add that entry into our database. 58
pause the video now 59
and give that a go. 60
Okay. So if we take a look at again the validation part of the Mongoose documentation and we look at 61
the built in validators, 62
it already specifies that there is a required validator. And you can see that in the example down here 63
they've actually implemented it for us. For example for the bacon field 64
they've opened up a set of curly braces and they've specified an object in here. And it says that all 65
bacon fields have to have a value that is a number., 66
so how many bacon have you got for your breakfast 67
in this case. And then they have the required field in there which they've set to true. 68
They've also given a message in the case when a piece of data is being added to the database without 69
specifying a value for the bacon field. 70
Let's try and replicate that in our code. Here in the schema again we're going to change the name part 71
from string to a object and it's going to have a type of string. 72
So we're going to keep that the same. But we're now going to add the required option and we're going 73
to set that to true. 74
So you can use one or you can use true, whichever one you find easier to understand. 75
Now next we can optionally specify a message. So we can say when a new piece of data is being added to 76
our fruits collection 77
and they didn't specify a name, we should say that 78
"Please check your data entry, 79
no name specified!". 80
So now let's hit save. And let's try to do the same thing that we pulled last time which is adding a 81
fruit with no name and try to save it to our database. 82
You can see now we're getting a validation error and it tells us that fruit validation failed and the 83
field that's causing a problem is name. And it's printing out that message that we specified, 84
"Please check your data entry, 85
no names specified!". 86
That's pretty failsafe right? 87
And if we check our data, you can see that we didn't get any new entries added to our data. 88
We've only got apple, kiwi, orange, banana, and that peach that didn't have a name before we added in the 89
validation. 90
But this new one didn't get added. 91
So there's a lot of other built in validations that you can check out on the Mongoose documentation 92
page. But that's just a quick intro into how you might use it and how easy it can be to keep your data 93
clean and validated against certain preset rules.