All language subtitles for 003 Data Validation with Mongoose_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 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 2 00:00:08,340 --> 00:00:10,660 data entry a lot easier. 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, 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 5 00:00:23,490 --> 00:00:25,150 use. 6 00:00:25,170 --> 00:00:26,980 So I'm going to delete all of this part 7 00:00:27,010 --> 00:00:30,930 where we have the kiwi, orange, banana and the insertMany. 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. 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 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 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, 12 00:00:54,090 --> 00:00:57,990 instead you can actually add in some validation. 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. 14 00:01:05,489 --> 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 15 00:01:12,930 --> 00:01:14,000 some validation. 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 17 00:01:21,210 --> 00:01:24,870 each fruit we want a limited between 1 and 10? 18 00:01:24,870 --> 00:01:29,950 So if somebody gives a rating of 99, then that shouldn't be valid right? 19 00:01:29,970 --> 00:01:33,650 So how would we do that using Mongoose? 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 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 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. 23 00:01:54,630 --> 00:02:00,000 Down here you can see that all schema types have the built in required validator. 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 25 00:02:05,190 --> 00:02:08,160 an ID, then it probably should be required. 26 00:02:08,280 --> 00:02:11,610 But there's other things such as for the number data types, 27 00:02:11,610 --> 00:02:14,810 you can have a minimum and maximum validators. 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 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. 30 00:02:28,080 --> 00:02:30,910 And max does the same thing for the maximum. 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 32 00:02:37,890 --> 00:02:40,920 have a rating that's between 1 and 10 33 00:02:40,920 --> 00:02:47,180 then we can specify the min as 1 and the max as 10. 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 35 00:02:54,260 --> 00:02:54,970 right? 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 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 38 00:03:11,090 --> 00:03:15,710 a error. And it tells us that there's a validation error 39 00:03:16,010 --> 00:03:18,940 and the problem is that fruit validation failed. 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. 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 42 00:03:34,180 --> 00:03:37,280 get added again because this is a fatal error. 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 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. 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? 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 47 00:04:06,630 --> 00:04:10,140 of 10. And let's just give it a review as well 48 00:04:10,140 --> 00:04:12,190 "Peaches are so yummy!". 49 00:04:13,060 --> 00:04:18,779 And let's hit save and try to save that fruit into our database, 50 00:04:18,779 --> 00:04:20,769 well nothing bad happens. 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. 52 00:04:30,110 --> 00:04:31,190 It has a review, 53 00:04:31,220 --> 00:04:35,500 it has a rating but it doesn't have a name which is quite bad 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 55 00:04:41,750 --> 00:04:44,960 it'll break our code later on down the line. 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 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 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. 59 00:05:04,820 --> 00:05:05,940 pause the video now 60 00:05:06,080 --> 00:05:07,210 and give that a go. 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 62 00:05:18,470 --> 00:05:20,270 the built in validators, 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 64 00:05:27,930 --> 00:05:32,380 they've actually implemented it for us. For example for the bacon field 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 66 00:05:38,760 --> 00:05:42,170 bacon fields have to have a value that is a number., 67 00:05:42,390 --> 00:05:44,890 so how many bacon have you got for your breakfast 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. 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 70 00:05:57,450 --> 00:06:00,300 specifying a value for the bacon field. 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 72 00:06:08,280 --> 00:06:15,360 from string to a object and it's going to have a type of string. 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 74 00:06:22,500 --> 00:06:24,420 to set that to true. 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. 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 77 00:06:38,220 --> 00:06:39,370 our fruits collection 78 00:06:39,450 --> 00:06:42,750 and they didn't specify a name, we should say that 79 00:06:45,840 --> 00:06:47,130 "Please check your data entry, 80 00:06:47,160 --> 00:06:49,910 no name specified!". 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 82 00:06:55,810 --> 00:06:59,360 fruit with no name and try to save it to our database. 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 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, 85 00:07:17,990 --> 00:07:19,550 "Please check your data entry, 86 00:07:19,610 --> 00:07:20,990 no names specified!". 87 00:07:21,200 --> 00:07:22,890 That's pretty failsafe right? 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. 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 90 00:07:35,090 --> 00:07:36,170 validation. 91 00:07:36,170 --> 00:07:39,750 But this new one didn't get added. 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 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 94 00:07:51,020 --> 00:07:55,250 clean and validated against certain preset rules. 10076

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.