WEBVTT 0 00:00.690 --> 00:08.340 Now as we mentioned earlier, by using and incorporating Mongoose into our app it makes validation of 1 00:08.340 --> 00:10.660 data entry a lot easier. 2 00:10.980 --> 00:17.040 Instead of having to write our own assert statements all over the place and specifying specific things, 3 00:17.340 --> 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 00:23.490 --> 00:25.150 use. 5 00:25.170 --> 00:26.980 So I'm going to delete all of this part 6 00:27.010 --> 00:30.930 where we have the kiwi, orange, banana and the insertMany. 7 00:31.020 --> 00:35.380 Now if you want to keep it around for reference then just go ahead and comment it out. 8 00:35.550 --> 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 00:43.380 --> 00:48.990 of having the value of each of these key value pairs being simply just the data type 10 00:49.140 --> 00:53.710 because this is the only thing that is strictly required when you are creating the schema, 11 00:54.090 --> 00:57.990 instead you can actually add in some validation. 12 00:57.990 --> 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 01:05.490 --> 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 01:12.930 --> 01:14.000 some validation. 15 01:14.160 --> 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 01:21.210 --> 01:24.870 each fruit we want a limited between 1 and 10? 17 01:24.870 --> 01:29.950 So if somebody gives a rating of 99, then that shouldn't be valid right? 18 01:29.970 --> 01:33.650 So how would we do that using Mongoose? 19 01:33.660 --> 01:40.800 Well if we head over to the Mongoose documentation and you click on validation, then you can see that 20 01:40.980 --> 01:47.220 there's a whole page dedicated to explaining how you can use the built in validators that Mongoose has 21 01:47.580 --> 01:54.600 in order to make your data comply with a certain format and validate your data to keep it clean. 22 01:54.630 --> 02:00.000 Down here you can see that all schema types have the built in required validator. 23 02:00.000 --> 02:05.160 So you can make a particular property required. That is usually for example if you were going to implement 24 02:05.190 --> 02:08.160 an ID, then it probably should be required. 25 02:08.280 --> 02:11.610 But there's other things such as for the number data types, 26 02:11.610 --> 02:14.810 you can have a minimum and maximum validators. 27 02:14.910 --> 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 02:21.810 --> 02:27.730 and it creates a validator that checks if the value is greater than or equal to the given minimum. 29 02:28.080 --> 02:30.910 And max does the same thing for the maximum. 30 02:31.350 --> 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 02:37.890 --> 02:40.920 have a rating that's between 1 and 10 32 02:40.920 --> 02:47.180 then we can specify the min as 1 and the max as 10. 33 02:47.180 --> 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 02:54.260 --> 02:54.970 right? 35 02:55.010 --> 03:02.540 And let's uncomment the fruit.save to save it in there and comment out the person.save so 36 03:02.540 --> 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 03:11.090 --> 03:15.710 a error. And it tells us that there's a validation error 38 03:16.010 --> 03:18.940 and the problem is that fruit validation failed. 39 03:19.070 --> 03:27.510 The rating is 34 and it's more than the maximum allowed value which is 10. 40 03:27.700 --> 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 03:34.180 --> 03:37.280 get added again because this is a fatal error. 42 03:37.420 --> 03:45.250 So it prevents data that doesn't match the validation to be inserted into our database thus keeping 43 03:45.250 --> 03:52.440 our database sanitized and clean and have all of the data in a format that we expect it to be. 44 03:54.810 --> 04:00.590 Now what if accidentally I added in a new document and I forgot to give it a name? 45 04:00.630 --> 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 04:06.630 --> 04:10.140 of 10. And let's just give it a review as well 47 04:10.140 --> 04:12.190 "Peaches are so yummy!". 48 04:13.060 --> 04:18.780 And let's hit save and try to save that fruit into our database, 49 04:18.780 --> 04:20.770 well nothing bad happens. 50 04:23.090 --> 04:30.030 If you check through our database you can see that that new thing got added right at the bottom here. 51 04:30.110 --> 04:31.190 It has a review, 52 04:31.220 --> 04:35.500 it has a rating but it doesn't have a name which is quite bad 53 04:35.510 --> 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 04:41.750 --> 04:44.960 it'll break our code later on down the line. 55 04:44.960 --> 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 04:52.730 --> 04:58.820 through the documentation on Mongoose. But you should make it such that when we try to add a new fruit 57 04:58.910 --> 05:04.820 that doesn't have a name then it will refuse to add that entry into our database. 58 05:04.820 --> 05:05.940 pause the video now 59 05:06.080 --> 05:07.210 and give that a go. 60 05:10.120 --> 05:18.380 Okay. So if we take a look at again the validation part of the Mongoose documentation and we look at 61 05:18.470 --> 05:20.270 the built in validators, 62 05:20.420 --> 05:27.890 it already specifies that there is a required validator. And you can see that in the example down here 63 05:27.930 --> 05:32.380 they've actually implemented it for us. For example for the bacon field 64 05:32.520 --> 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 05:38.760 --> 05:42.170 bacon fields have to have a value that is a number., 66 05:42.390 --> 05:44.890 so how many bacon have you got for your breakfast 67 05:44.910 --> 05:50.700 in this case. And then they have the required field in there which they've set to true. 68 05:50.700 --> 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 05:57.450 --> 06:00.300 specifying a value for the bacon field. 70 06:00.300 --> 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 06:08.280 --> 06:15.360 from string to a object and it's going to have a type of string. 72 06:15.960 --> 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 06:22.500 --> 06:24.420 to set that to true. 74 06:24.510 --> 06:30.300 So you can use one or you can use true, whichever one you find easier to understand. 75 06:30.450 --> 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 06:38.220 --> 06:39.370 our fruits collection 77 06:39.450 --> 06:42.750 and they didn't specify a name, we should say that 78 06:45.840 --> 06:47.130 "Please check your data entry, 79 06:47.160 --> 06:49.910 no name specified!". 80 06:49.960 --> 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 06:55.810 --> 06:59.360 fruit with no name and try to save it to our database. 82 07:05.120 --> 07:11.960 You can see now we're getting a validation error and it tells us that fruit validation failed and the 83 07:11.960 --> 07:17.990 field that's causing a problem is name. And it's printing out that message that we specified, 84 07:17.990 --> 07:19.550 "Please check your data entry, 85 07:19.610 --> 07:20.990 no names specified!". 86 07:21.200 --> 07:22.890 That's pretty failsafe right? 87 07:23.150 --> 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 07:29.240 --> 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 07:35.090 --> 07:36.170 validation. 90 07:36.170 --> 07:39.750 But this new one didn't get added. 91 07:39.830 --> 07:44.780 So there's a lot of other built in validations that you can check out on the Mongoose documentation 92 07:44.780 --> 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 07:51.020 --> 07:55.250 clean and validated against certain preset rules.