1
00:00:00,330 --> 00:00:04,770
All right, our user model is almost almost almost ready to go.

2
00:00:05,490 --> 00:00:11,460
The only thing is missing is the validation for email and like already mentioned in previous video,

3
00:00:11,460 --> 00:00:18,390
if you watched the other project, we want you to know what expression we use match property and we

4
00:00:18,390 --> 00:00:19,590
passed in the reject.

5
00:00:20,010 --> 00:00:22,020
However, there is another way how we can do that.

6
00:00:22,570 --> 00:00:28,950
You see, in the Mongoose, we can set up a validate property with two more properties.

7
00:00:28,950 --> 00:00:35,310
So essentially it's an object with two more properties, a validator property where we set up the function,

8
00:00:35,820 --> 00:00:40,140
which is going to be looking for the field and then whatever we return from this function, meaning

9
00:00:40,410 --> 00:00:41,670
either true or false.

10
00:00:42,090 --> 00:00:48,810
That means that either we validate or don't validate the particular property and then we have a message

11
00:00:49,440 --> 00:00:52,020
if, for example, there is an error.

12
00:00:52,500 --> 00:00:56,220
So at the moment in the user, what do we have, what we have?

13
00:00:56,310 --> 00:00:57,300
Please provide email.

14
00:00:57,370 --> 00:01:02,880
But once we set up a validator, we're going to say please provide a valid email of what makes sense.

15
00:01:02,880 --> 00:01:05,670
And if you want to find out more info about this one.

16
00:01:06,090 --> 00:01:07,940
Just go to your -- Knox.

17
00:01:07,980 --> 00:01:12,150
Search for custom validators and you'll see this example.

18
00:01:12,300 --> 00:01:14,610
I said Revalidate validator.

19
00:01:14,790 --> 00:01:15,690
That's the function.

20
00:01:16,410 --> 00:01:18,990
Please keep in mind that you're not invoking this function over here.

21
00:01:19,380 --> 00:01:22,620
You're just setting it up as a reference.

22
00:01:22,890 --> 00:01:30,780
And we also have a message because this function will be invoked when you're trying to create that user

23
00:01:31,080 --> 00:01:32,970
or job or whatever.

24
00:01:33,630 --> 00:01:39,570
And technically, yes, we can set up our own function, but then it's kind of the same like we were

25
00:01:39,570 --> 00:01:40,500
passing in that region.

26
00:01:41,190 --> 00:01:49,620
So another option can do we can get a validator package, which essentially is a very cool library that

27
00:01:49,620 --> 00:01:52,950
has all of those validations already in place.

28
00:01:53,430 --> 00:01:54,840
So let's start with this.

29
00:01:55,290 --> 00:01:59,580
I'm just going to go to a user and I'll add this object first.

30
00:02:00,120 --> 00:02:04,920
So let me go to my email and it's my preference to set it up above unique.

31
00:02:04,950 --> 00:02:09,539
Don't ask me why just a preference and I'm going to go with validate here.

32
00:02:09,539 --> 00:02:13,440
I have the object and then we're looking for validator.

33
00:02:13,680 --> 00:02:14,790
That's going to be my function.

34
00:02:15,180 --> 00:02:17,280
And then the second one will be the message.

35
00:02:17,640 --> 00:02:23,730
And as far as the message, like I said, please provide provide a valid email and then we want out

36
00:02:23,730 --> 00:02:26,970
the column so we don't have any syntax bugs.

37
00:02:27,360 --> 00:02:30,210
And now let's navigate and take a look at the package.

38
00:02:30,450 --> 00:02:36,180
So this is going to be a link and we'll have to install the library at some point as well.

39
00:02:36,540 --> 00:02:43,380
So let me navigate here or these are the docs where basically you can find all of the validators that

40
00:02:43,380 --> 00:02:44,190
they provide.

41
00:02:44,190 --> 00:02:47,250
And then in our case, we're looking for email.

42
00:02:47,730 --> 00:02:53,360
Now, why would you want to use the extension package instead of setting up the function yourself?

43
00:02:53,370 --> 00:02:55,410
Well, this is going to be more battle tested.

44
00:02:56,050 --> 00:02:59,130
It also gives you a bunch of other options as well.

45
00:02:59,370 --> 00:03:01,680
So those would be two reasons why.

46
00:03:01,950 --> 00:03:04,950
Going with external package kind of makes sense.

47
00:03:05,220 --> 00:03:08,220
So we're looking for this is email and what do we need to do?

48
00:03:08,250 --> 00:03:12,930
Well, we need to install first, then we need to import in your address and then we're just going to

49
00:03:12,930 --> 00:03:18,000
go with validator and email and we'll set it here where we have the validator.

50
00:03:18,270 --> 00:03:22,020
So let me stop server and I'm going to go with my command.

51
00:03:22,350 --> 00:03:29,340
So when I install the package, then I'll restart my entire project or npm start.

52
00:03:29,760 --> 00:03:33,990
Then we want to go to a user right below the Mongoose.

53
00:03:33,990 --> 00:03:39,150
So we're going to go with validator and that is going to be coming from the validator.

54
00:03:39,540 --> 00:03:43,710
Then let's keep on scrolling and we're looking for validator.

55
00:03:43,860 --> 00:03:46,090
And then is email.

56
00:03:46,470 --> 00:03:47,970
That's the function.

57
00:03:48,090 --> 00:03:49,980
Again, we pass in the reference.

58
00:03:50,190 --> 00:03:51,960
We don't invoke the functionality here.

59
00:03:52,200 --> 00:03:59,610
The function will be invoked and will automatically get access to whatever is passed in the email when

60
00:03:59,610 --> 00:04:01,920
we're trying to set up the user.

61
00:04:02,520 --> 00:04:10,920
And now that we are finally done with our initial setup for the user model, we can start setting up

62
00:04:10,920 --> 00:04:15,060
our controller and actually start creating the users.

