0 1 00:00:00,690 --> 00:00:08,340 Now as we mentioned earlier, by using and incorporating Mongoose into our app it makes validation of 1 2 00:00:08,340 --> 00:00:10,660 data entry a lot easier. 2 3 00:00:10,980 --> 00:00:17,040 Instead of having to write our own assert statements all over the place and specifying specific things, 3 4 00:00:17,340 --> 00:00:23,490 we can actually use Mongoose's built in validation checks to make it a lot easier and a lot quicker to 4 5 00:00:23,490 --> 00:00:25,150 use. 5 6 00:00:25,170 --> 00:00:26,980 So I'm going to delete all of this part 6 7 00:00:27,010 --> 00:00:30,930 where we have the kiwi, orange, banana and the insertMany. 7 8 00:00:31,020 --> 00:00:35,380 Now if you want to keep it around for reference then just go ahead and comment it out. 8 9 00:00:35,550 --> 00:00:43,380 If you scroll to the top where we created our fruit schema, one of the things that you can do is instead 9 10 00:00:43,380 --> 00:00:48,990 of having the value of each of these key value pairs being simply just the data type 10 11 00:00:49,140 --> 00:00:53,710 because this is the only thing that is strictly required when you are creating the schema, 11 12 00:00:54,090 --> 00:00:57,990 instead you can actually add in some validation. 12 13 00:00:57,990 --> 00:01:05,290 So let's go ahead and delete that data type and instead I'm going to open up a set of curly braces. 13 14 00:01:05,490 --> 00:01:12,930 Now inside the curly braces I can add back that type which is going to be Number but I can also add 14 15 00:01:12,930 --> 00:01:14,000 some validation. 15 16 00:01:14,160 --> 00:01:21,210 So for example, what if when we're adding new data to our fruits collection the rating that we give to 16 17 00:01:21,210 --> 00:01:24,870 each fruit we want a limited between 1 and 10? 17 18 00:01:24,870 --> 00:01:29,950 So if somebody gives a rating of 99, then that shouldn't be valid right? 18 19 00:01:29,970 --> 00:01:33,650 So how would we do that using Mongoose? 19 20 00:01:33,660 --> 00:01:40,800 Well if we head over to the Mongoose documentation and you click on validation, then you can see that 20 21 00:01:40,980 --> 00:01:47,220 there's a whole page dedicated to explaining how you can use the built in validators that Mongoose has 21 22 00:01:47,580 --> 00:01:54,600 in order to make your data comply with a certain format and validate your data to keep it clean. 22 23 00:01:54,630 --> 00:02:00,000 Down here you can see that all schema types have the built in required validator. 23 24 00:02:00,000 --> 00:02:05,160 So you can make a particular property required. That is usually for example if you were going to implement 24 25 00:02:05,190 --> 00:02:08,160 an ID, then it probably should be required. 25 26 00:02:08,280 --> 00:02:11,610 But there's other things such as for the number data types, 26 27 00:02:11,610 --> 00:02:14,810 you can have a minimum and maximum validators. 27 28 00:02:14,910 --> 00:02:21,420 And if we click on that, it explains to us how you can use it. The minimum is going to be a number 28 29 00:02:21,810 --> 00:02:27,730 and it creates a validator that checks if the value is greater than or equal to the given minimum. 29 30 00:02:28,080 --> 00:02:30,910 And max does the same thing for the maximum. 30 31 00:02:31,350 --> 00:02:37,620 So that means over here for our rating, in order to keep all the new data that we enter into our database 31 32 00:02:37,890 --> 00:02:40,920 have a rating that's between 1 and 10 32 33 00:02:40,920 --> 00:02:47,180 then we can specify the min as 1 and the max as 10. 33 34 00:02:47,180 --> 00:02:54,230 So now if we hit save and let's just change this part to have a rating of, I don't kow, 34 34 35 00:02:54,260 --> 00:02:54,970 right? 35 36 00:02:55,010 --> 00:03:02,540 And let's uncomment the fruit.save to save it in there and comment out the person.save so 36 37 00:03:02,540 --> 00:03:10,910 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 38 00:03:11,090 --> 00:03:15,710 a error. And it tells us that there's a validation error 38 39 00:03:16,010 --> 00:03:18,940 and the problem is that fruit validation failed. 39 40 00:03:19,070 --> 00:03:27,510 The rating is 34 and it's more than the maximum allowed value which is 10. 40 41 00:03:27,700 --> 00:03:34,180 And when we look at the fruits that are being printed out, you can see that new fruit, the apple, didn't 41 42 00:03:34,180 --> 00:03:37,280 get added again because this is a fatal error. 42 43 00:03:37,420 --> 00:03:45,250 So it prevents data that doesn't match the validation to be inserted into our database thus keeping 43 44 00:03:45,250 --> 00:03:52,440 our database sanitized and clean and have all of the data in a format that we expect it to be. 44 45 00:03:54,810 --> 00:04:00,590 Now what if accidentally I added in a new document and I forgot to give it a name? 45 46 00:04:00,630 --> 00:04:06,630 So for example I was adding some peaches in there and peaches are lovely, so I'm going to give it a rating 46 47 00:04:06,630 --> 00:04:10,140 of 10. And let's just give it a review as well 47 48 00:04:10,140 --> 00:04:12,190 "Peaches are so yummy!". 48 49 00:04:13,060 --> 00:04:18,780 And let's hit save and try to save that fruit into our database, 49 50 00:04:18,780 --> 00:04:20,770 well nothing bad happens. 50 51 00:04:23,090 --> 00:04:30,030 If you check through our database you can see that that new thing got added right at the bottom here. 51 52 00:04:30,110 --> 00:04:31,190 It has a review, 52 53 00:04:31,220 --> 00:04:35,500 it has a rating but it doesn't have a name which is quite bad 53 54 00:04:35,510 --> 00:04:41,750 if we want to loop through our data and print out everything that has a name right? It doesn't work and 54 55 00:04:41,750 --> 00:04:44,960 it'll break our code later on down the line. 55 56 00:04:44,960 --> 00:04:52,730 So as a challenge, I want you to make the name field required and this means that you might have to look 56 57 00:04:52,730 --> 00:04:58,820 through the documentation on Mongoose. But you should make it such that when we try to add a new fruit 57 58 00:04:58,910 --> 00:05:04,820 that doesn't have a name then it will refuse to add that entry into our database. 58 59 00:05:04,820 --> 00:05:05,940 pause the video now 59 60 00:05:06,080 --> 00:05:07,210 and give that a go. 60 61 00:05:10,120 --> 00:05:18,380 Okay. So if we take a look at again the validation part of the Mongoose documentation and we look at 61 62 00:05:18,470 --> 00:05:20,270 the built in validators, 62 63 00:05:20,420 --> 00:05:27,890 it already specifies that there is a required validator. And you can see that in the example down here 63 64 00:05:27,930 --> 00:05:32,380 they've actually implemented it for us. For example for the bacon field 64 65 00:05:32,520 --> 00:05:38,760 they've opened up a set of curly braces and they've specified an object in here. And it says that all 65 66 00:05:38,760 --> 00:05:42,170 bacon fields have to have a value that is a number., 66 67 00:05:42,390 --> 00:05:44,890 so how many bacon have you got for your breakfast 67 68 00:05:44,910 --> 00:05:50,700 in this case. And then they have the required field in there which they've set to true. 68 69 00:05:50,700 --> 00:05:57,240 They've also given a message in the case when a piece of data is being added to the database without 69 70 00:05:57,450 --> 00:06:00,300 specifying a value for the bacon field. 70 71 00:06:00,300 --> 00:06:07,950 Let's try and replicate that in our code. Here in the schema again we're going to change the name part 71 72 00:06:08,280 --> 00:06:15,360 from string to a object and it's going to have a type of string. 72 73 00:06:15,960 --> 00:06:22,500 So we're going to keep that the same. But we're now going to add the required option and we're going 73 74 00:06:22,500 --> 00:06:24,420 to set that to true. 74 75 00:06:24,510 --> 00:06:30,300 So you can use one or you can use true, whichever one you find easier to understand. 75 76 00:06:30,450 --> 00:06:38,220 Now next we can optionally specify a message. So we can say when a new piece of data is being added to 76 77 00:06:38,220 --> 00:06:39,370 our fruits collection 77 78 00:06:39,450 --> 00:06:42,750 and they didn't specify a name, we should say that 78 79 00:06:45,840 --> 00:06:47,130 "Please check your data entry, 79 80 00:06:47,160 --> 00:06:49,910 no name specified!". 80 81 00:06:49,960 --> 00:06:55,810 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 82 00:06:55,810 --> 00:06:59,360 fruit with no name and try to save it to our database. 82 83 00:07:05,120 --> 00:07:11,960 You can see now we're getting a validation error and it tells us that fruit validation failed and the 83 84 00:07:11,960 --> 00:07:17,990 field that's causing a problem is name. And it's printing out that message that we specified, 84 85 00:07:17,990 --> 00:07:19,550 "Please check your data entry, 85 86 00:07:19,610 --> 00:07:20,990 no names specified!". 86 87 00:07:21,200 --> 00:07:22,890 That's pretty failsafe right? 87 88 00:07:23,150 --> 00:07:29,210 And if we check our data, you can see that we didn't get any new entries added to our data. 88 89 00:07:29,240 --> 00:07:35,090 We've only got apple, kiwi, orange, banana, and that peach that didn't have a name before we added in the 89 90 00:07:35,090 --> 00:07:36,170 validation. 90 91 00:07:36,170 --> 00:07:39,750 But this new one didn't get added. 91 92 00:07:39,830 --> 00:07:44,780 So there's a lot of other built in validations that you can check out on the Mongoose documentation 92 93 00:07:44,780 --> 00:07:51,020 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 94 00:07:51,020 --> 00:07:55,250 clean and validated against certain preset rules.