1
00:00:00,440 --> 00:00:06,680
And as far as the validation layer, we're going to use package by the name of Express Validator.

2
00:00:06,680 --> 00:00:10,280
Just keep in mind, yes, there are other packages as well.

3
00:00:10,280 --> 00:00:17,330
For example, you can use Joy, but in my opinion, Express Validator is super, super straightforward

4
00:00:17,330 --> 00:00:22,790
and therefore we're going to stick with this one and we'll start with another test route.

5
00:00:22,790 --> 00:00:27,710
So essentially, I just want you to understand the main concepts in isolation.

6
00:00:27,710 --> 00:00:32,750
And once that's the case, then of course we'll apply it to rest of the requests.

7
00:00:32,750 --> 00:00:39,410
So first of all, let's navigate to server and let's scroll up.

8
00:00:40,100 --> 00:00:47,040
So at the moment I have this post route to the home page where essentially we're just logging req.body.

9
00:00:47,060 --> 00:00:51,110
I believe we use this one to test out the express Json.

10
00:00:51,140 --> 00:00:53,090
Let's refactor it a little bit.

11
00:00:53,270 --> 00:00:55,370
I'm going to change the URL.

12
00:00:55,400 --> 00:01:02,770
I'm going to go with API version one because we already have the URL variable in the Thunder client.

13
00:01:02,780 --> 00:01:03,380
Correct.

14
00:01:03,560 --> 00:01:05,480
And I'm just going to go with the test.

15
00:01:05,780 --> 00:01:08,030
That's going to be my complete URL.

16
00:01:08,120 --> 00:01:12,860
For now, let's just grab the name since I know that I'm going to send that.

17
00:01:12,890 --> 00:01:18,920
So I'm going to go with name and I want to destructure it from req dot body.

18
00:01:18,950 --> 00:01:26,990
Then as far as the response, simply let's send back Res.json and instead of all this, we're just going

19
00:01:26,990 --> 00:01:30,830
to go with template string and we're going to go with Hello.

20
00:01:31,610 --> 00:01:33,140
And let's access the name.

21
00:01:36,120 --> 00:01:38,730
We're going to navigate to Thunder Client.

22
00:01:39,640 --> 00:01:41,050
And let's set up a new one again.

23
00:01:41,050 --> 00:01:43,120
This is just going to be a test one.

24
00:01:43,890 --> 00:01:45,510
So we can access the URL?

25
00:01:45,540 --> 00:01:46,260
Of course.

26
00:01:47,090 --> 00:01:48,210
Let's close the curlies.

27
00:01:48,230 --> 00:01:50,780
Then we're going to go with Test Body.

28
00:01:52,030 --> 00:01:53,410
Then let's provide name.

29
00:01:54,800 --> 00:01:57,830
And I'm going to go with Peter because why not?

30
00:01:57,860 --> 00:01:58,760
Let's send it.

31
00:01:58,760 --> 00:02:03,260
And it complains that we cannot find the root.

32
00:02:03,290 --> 00:02:04,910
And this is a good use case.

33
00:02:05,060 --> 00:02:08,539
So we go here with get an API version one.

34
00:02:08,810 --> 00:02:10,509
So why we have the error?

35
00:02:10,520 --> 00:02:12,020
Well, because this is a post road.

36
00:02:12,350 --> 00:02:16,970
And again, this is a good demonstration for the Morgan.

37
00:02:17,180 --> 00:02:17,860
You're right away.

38
00:02:17,870 --> 00:02:18,440
See that?

39
00:02:18,440 --> 00:02:21,630
Hey, listen, you're using the wrong method.

40
00:02:21,650 --> 00:02:22,980
So let's go here with post.

41
00:02:23,000 --> 00:02:23,630
Let's send it.

42
00:02:23,630 --> 00:02:24,680
And now, of course I have.

43
00:02:24,680 --> 00:02:25,550
Hello, Peter.

44
00:02:25,730 --> 00:02:29,120
Now, it's not going to be surprised if I go with Empty.

45
00:02:29,870 --> 00:02:30,530
String.

46
00:02:30,710 --> 00:02:37,220
Then of course, in my response I'll have this same result and this is what we want to fix with Express

47
00:02:37,220 --> 00:02:38,200
Validator.

48
00:02:38,210 --> 00:02:40,130
I want to set up a validation.

49
00:02:40,220 --> 00:02:44,210
If the name is not present, I actually want to send back 404.

50
00:02:44,240 --> 00:02:46,310
I don't want to send back 200.

51
00:02:46,310 --> 00:02:47,600
And again, just bear with me.

52
00:02:47,600 --> 00:02:52,580
We'll set up everything in a server and then once we have everything.

53
00:02:53,190 --> 00:03:00,060
In place, then we'll move it to a separate middleware and again apply to rest of the routes.

54
00:03:00,420 --> 00:03:02,190
First of all, let's import.

55
00:03:02,830 --> 00:03:04,350
We're looking for two things.

56
00:03:04,360 --> 00:03:08,980
We want to grab the body and the validation.

57
00:03:09,750 --> 00:03:10,680
Result.

58
00:03:10,890 --> 00:03:13,590
And both of these things are coming from.

59
00:03:14,840 --> 00:03:17,460
The Express validator package.

60
00:03:17,480 --> 00:03:22,640
Now, as far as the body, remember, so there are multiple places where we can check for values.

61
00:03:22,670 --> 00:03:24,400
For example, params.

62
00:03:24,410 --> 00:03:29,660
And therefore, since I know that I'm going to be checking for values in a body, I want to get the.

63
00:03:30,200 --> 00:03:33,530
Body export from express validator.

64
00:03:33,530 --> 00:03:38,550
And as far as the validation result, you'll see it in action in second.

65
00:03:38,570 --> 00:03:39,680
So let's scroll down.

66
00:03:39,680 --> 00:03:42,170
I think I can close the console for now.

67
00:03:42,200 --> 00:03:51,560
And here in between the controller and the URL, we want to set up express Validator as our middleware.

68
00:03:51,830 --> 00:03:57,530
So Express Validator is going to be sitting on top of our controller and if.

69
00:03:58,540 --> 00:04:02,300
The value is not present or it doesn't match our condition.

70
00:04:02,320 --> 00:04:07,360
Then we'll right away spit back the 400 error.

71
00:04:07,570 --> 00:04:10,390
And in order to set it up, we want to go here with the brackets.

72
00:04:10,750 --> 00:04:13,330
Then we need to invoke body.

73
00:04:13,360 --> 00:04:17,890
So essentially this is where we're looking for the value and the way it works.

74
00:04:17,890 --> 00:04:19,450
We just pass here the string.

75
00:04:19,480 --> 00:04:27,280
Now, of course, this string matches whatever we're expecting in the req.body.

76
00:04:27,310 --> 00:04:33,040
So since I'm expecting that there's going to be a name, that's why I type here, string of name.

77
00:04:33,250 --> 00:04:39,520
Later, when we check for lastname, when we check for job status and all that, of course we'll use

78
00:04:39,520 --> 00:04:46,360
the same string value and then we can nicely chain the methods that are provided and one of them is

79
00:04:46,360 --> 00:04:46,990
not empty.

80
00:04:47,020 --> 00:04:49,000
So essentially I'm saying, hey, listen.

81
00:04:49,950 --> 00:04:51,810
There has to be some kind of value in here.

82
00:04:51,810 --> 00:04:58,110
And what's also really cool, we can chain for specific errors, specific messages.

83
00:04:58,110 --> 00:04:59,390
So what am I talking about?

84
00:04:59,400 --> 00:05:05,280
Well, we can set up one main one, but also, for example, for not empty, I can say, hey, listen,

85
00:05:05,280 --> 00:05:12,930
name is required, but then when we'll add one to check the length, I can say, you know what the length

86
00:05:12,930 --> 00:05:17,670
should be between, let's say three and 50 and you'll see it in action in a second.

87
00:05:17,670 --> 00:05:22,140
So first, like I said, we can just keep chaining these methods.

88
00:05:22,140 --> 00:05:24,270
And the one we want to use is with message.

89
00:05:24,270 --> 00:05:28,170
So essentially this is going to be the error message we send back.

90
00:05:28,170 --> 00:05:32,130
So here we're going to go with name is required.

91
00:05:32,220 --> 00:05:38,550
And once we have this code in place, we actually want to add a function which will check for those

92
00:05:38,550 --> 00:05:39,090
errors.

93
00:05:39,090 --> 00:05:42,840
So essentially, yes, we'll have to middleware functions.

94
00:05:42,840 --> 00:05:45,630
So one is going to be where we'll set up our roles.

95
00:05:45,630 --> 00:05:49,050
And the second one is where we'll check for errors.

96
00:05:49,050 --> 00:05:50,860
If they exist.

97
00:05:50,860 --> 00:05:57,760
And if that's the case, then of course we'll send back the 404 or whatever error we want to send back.

98
00:05:57,760 --> 00:06:03,700
So in this middleware function, we want to access three things req res and the next.

99
00:06:04,280 --> 00:06:10,100
So when it comes to next, if everything is successful, we want to pass it to the next middleware.

100
00:06:10,130 --> 00:06:14,930
Otherwise, the request is going to stop over here in this middleware function.

101
00:06:14,930 --> 00:06:16,700
So next is a special one.

102
00:06:16,700 --> 00:06:22,430
And yes, of course, in this case, the next middleware technically is our controller, so that's the

103
00:06:22,430 --> 00:06:23,120
end of the chain.

104
00:06:23,120 --> 00:06:29,210
But normally if you set up next, that just passes on to the next middleware and that's something always

105
00:06:29,300 --> 00:06:35,410
you want to add because otherwise, again, this request is just going to stop over here in this function.

106
00:06:35,420 --> 00:06:40,430
Now, of course, we will invoke next based on the condition whether we have errors.

107
00:06:40,430 --> 00:06:45,680
But in general, try not to forget about the next and essentially.

108
00:06:46,580 --> 00:06:48,580
We want to set up a variable.

109
00:06:48,590 --> 00:06:50,120
I'm going to go over here errors.

110
00:06:50,120 --> 00:06:56,540
And this is where we want to invoke validation result and we're looking for the request.

111
00:06:57,310 --> 00:07:00,880
So essentially we pass in the incoming request.

112
00:07:00,880 --> 00:07:07,070
And since we already have this logic in place, it checks for that name in the body.

113
00:07:07,090 --> 00:07:08,530
Hopefully that is clear.

114
00:07:08,530 --> 00:07:12,160
And now, of course, I just want to log it just so we can see what's happening.

115
00:07:12,550 --> 00:07:16,090
And then we're going to discuss what methods we can use.

116
00:07:16,120 --> 00:07:18,540
So first, let's navigate to the new request.

117
00:07:18,550 --> 00:07:20,080
Let's send it one more time.

118
00:07:20,080 --> 00:07:22,930
So at the moment, yeah, the response is still the same.

119
00:07:22,930 --> 00:07:25,450
What I'm interested is the console.

120
00:07:25,450 --> 00:07:32,020
So notice over here I have this errors property and in here each error is represented with this object.

121
00:07:32,050 --> 00:07:34,060
Now what we're looking for is the message.

122
00:07:34,060 --> 00:07:37,100
So that's the one that we're going to send back.

123
00:07:37,120 --> 00:07:43,870
And what's really awesome, this instance has a few methods that we can right away use.

124
00:07:43,900 --> 00:07:47,800
For example, one of them is is an empty.

125
00:07:47,800 --> 00:07:49,390
So invoke it over here.

126
00:07:49,510 --> 00:07:52,270
We send it one more time and check it out.

127
00:07:52,270 --> 00:07:53,770
Basically, this is false.

128
00:07:53,800 --> 00:07:54,910
Now, what does that mean?

129
00:07:54,910 --> 00:07:57,080
Well, it means that we have the errors.

130
00:07:57,080 --> 00:08:00,260
So if is empty is true, then we're good to go.

131
00:08:00,290 --> 00:08:05,030
Then we can pass it on to the next middleware in our case controller.

132
00:08:05,030 --> 00:08:09,920
But if it's false, if it's not empty, that means we have errors.

133
00:08:09,920 --> 00:08:13,370
And of course, in that case we want to send back the error response.

134
00:08:13,400 --> 00:08:15,700
So let's set up the condition.

135
00:08:15,710 --> 00:08:21,830
I'll say if is empty is false, basically if we have the errors.

136
00:08:22,400 --> 00:08:26,060
Let's send back some kind of error response.

137
00:08:26,060 --> 00:08:31,640
Now, in my case, I'm going to hardcode the 400, but in the upcoming videos, when we set up the proper

138
00:08:31,640 --> 00:08:37,190
middleware, we'll actually invoke one of the classes we set up previously.

139
00:08:37,370 --> 00:08:39,110
Then we can also.

140
00:08:39,789 --> 00:08:41,169
Set up messages.

141
00:08:41,169 --> 00:08:45,130
So let me go here with some kind of variable error messages.

142
00:08:45,790 --> 00:08:51,100
And let me just make sure that the spelling is correct and that one is equal to errors.

143
00:08:51,220 --> 00:08:59,620
Again, this instance has this array method, which essentially turns it into an array and we can map

144
00:08:59,620 --> 00:09:00,520
over it.

145
00:09:00,760 --> 00:09:05,500
And what I want is, again, just to return that message property.

146
00:09:05,530 --> 00:09:09,520
Now, if you want to have different setup, of course you can change it around.

147
00:09:09,520 --> 00:09:13,420
But in my case, I'm just looking for that message property.

148
00:09:13,570 --> 00:09:22,090
So this one over here and again, this is built in this right away just turns it into an array and then

149
00:09:22,090 --> 00:09:26,590
we can iterate over and then when it comes to the response.

150
00:09:27,570 --> 00:09:30,240
Let's go here with return res dot.

151
00:09:30,420 --> 00:09:32,160
Again, we're hardcoding for now.

152
00:09:32,820 --> 00:09:35,100
We'll fix that in the middleware.

153
00:09:35,250 --> 00:09:36,930
Then let's go json.

154
00:09:36,930 --> 00:09:43,890
And since potentially I can have multiple error messages, I'm going to go with errors property and

155
00:09:43,890 --> 00:09:47,190
I'll set it equal to my error messages.

156
00:09:47,340 --> 00:09:55,080
So now if we go over here and if I send this one notice, I'll get back the errors and it's going to

157
00:09:55,080 --> 00:09:57,660
say name is required.

158
00:09:58,110 --> 00:10:01,950
And keep in mind, of course we can add more conditions.

159
00:10:01,950 --> 00:10:09,810
So let's say I add here, Peter, but I can go back over here where I have the angle brackets and I

160
00:10:09,810 --> 00:10:12,330
can just keep chaining my conditions.

161
00:10:12,330 --> 00:10:18,690
So for example, I can go with, you know, what is length should be, I don't know, let me go with

162
00:10:18,690 --> 00:10:22,950
something ridiculous and I'm going to go with 50.

163
00:10:23,040 --> 00:10:29,390
So we'll pass here the object and we'll say min is equal to 50.

164
00:10:29,410 --> 00:10:31,960
Again, something will change in a second.

165
00:10:32,620 --> 00:10:35,080
But just for testing purposes.

166
00:10:35,350 --> 00:10:38,710
And then remember, we can still change the message.

167
00:10:38,710 --> 00:10:40,180
So I'm going to copy and paste.

168
00:10:40,210 --> 00:10:45,520
Now we have with message and in here we'll just write something else.

169
00:10:45,520 --> 00:10:49,510
So I'll say name must be at least.

170
00:10:50,200 --> 00:10:51,310
And 50.

171
00:10:51,340 --> 00:10:57,040
And I'm not going to type characters because we'll set it up later and let's navigate back to the request.

172
00:10:57,040 --> 00:11:02,290
So technically we have the beta, but now we have a different error.

173
00:11:02,380 --> 00:11:11,230
And with this in place we have successfully added the express validator middleware to our test request.

