1
00:00:00,230 --> 00:00:07,250
And once we're done with our initial setup, your first question is probably, Hey, buddy, are you

2
00:00:07,250 --> 00:00:07,910
okay?

3
00:00:08,060 --> 00:00:16,280
You told us that once we set up the validation layer, our controllers are going to be leaner and instead

4
00:00:16,280 --> 00:00:21,380
I just have this giant massive code sitting on top of it.

5
00:00:21,410 --> 00:00:28,220
It's definitely not a easier or a more maintainable way.

6
00:00:28,610 --> 00:00:29,540
And you're correct.

7
00:00:29,540 --> 00:00:33,260
But keep in mind that we can move them to a separate file.

8
00:00:33,440 --> 00:00:36,520
So essentially we can set up this as a middleware.

9
00:00:36,530 --> 00:00:41,190
And again, the cool thing is that we can take it from project to project.

10
00:00:41,210 --> 00:00:47,270
So essentially we will reuse this code in this project in all of the requests.

11
00:00:47,270 --> 00:00:52,490
And if you want, you can do the same thing in your other projects as well.

12
00:00:52,490 --> 00:00:54,320
So let's get to work.

13
00:00:54,350 --> 00:00:58,560
We first want to create a validation middleware.

14
00:00:58,580 --> 00:01:04,470
JS In the middleware folder, so we already have one for errors.

15
00:01:04,680 --> 00:01:07,030
And now let's do the same thing over here.

16
00:01:07,050 --> 00:01:08,760
So let's come up with the name.

17
00:01:09,120 --> 00:01:11,130
In my case, validation.

18
00:01:11,990 --> 00:01:12,520
Middleware.

19
00:01:12,530 --> 00:01:16,110
For some reason, middleware spelling is just horrible for me.

20
00:01:16,130 --> 00:01:17,300
I don't know why.

21
00:01:17,330 --> 00:01:19,520
Then let's grab the same imports.

22
00:01:19,520 --> 00:01:23,900
So again, we're obviously not going to keep this in server.

23
00:01:24,520 --> 00:01:26,140
So I just want to scroll up.

24
00:01:26,320 --> 00:01:29,830
I want to grab these ones and essentially we can remove it.

25
00:01:29,860 --> 00:01:30,700
Okay, good.

26
00:01:30,730 --> 00:01:33,260
Then we want to grab our bad request.

27
00:01:33,280 --> 00:01:35,500
Remember, that's the.

28
00:01:36,300 --> 00:01:36,960
Our class.

29
00:01:36,960 --> 00:01:41,190
We set up a few videos ago since again, we're not going to hard code.

30
00:01:41,190 --> 00:01:47,090
We will actually use our class and effectively in here we want to create two things.

31
00:01:47,100 --> 00:01:53,880
We want to create, validate test where we'll pass in the values we want to validate.

32
00:01:53,880 --> 00:01:56,790
And second one is going to be a function.

33
00:01:57,450 --> 00:02:01,230
Which takes care of the error response.

34
00:02:01,230 --> 00:02:06,150
And I guess let's start with the function that returns the errors.

35
00:02:06,890 --> 00:02:08,300
We want to come up with the name.

36
00:02:08,300 --> 00:02:10,160
In my case, I'm going to go with with.

37
00:02:10,860 --> 00:02:13,200
Validation errors.

38
00:02:13,200 --> 00:02:17,040
And in here we're going to be looking for that array.

39
00:02:17,130 --> 00:02:18,900
So this one over here.

40
00:02:20,240 --> 00:02:22,190
And in our case, of course, we only have the body.

41
00:02:22,190 --> 00:02:28,040
But normally there's going to be more values and I'm going to give it a validate.

42
00:02:29,600 --> 00:02:31,070
Values name.

43
00:02:31,070 --> 00:02:33,290
So that's my parameter.

44
00:02:33,320 --> 00:02:36,060
Now, what do I want to return from this function?

45
00:02:36,080 --> 00:02:40,670
Well, since there's going to be two things, there's going to be validate values, just like we have

46
00:02:40,670 --> 00:02:47,630
right now, and also the middleware in Express if we want to group them together, we essentially set

47
00:02:47,630 --> 00:02:48,830
up an array.

48
00:02:48,860 --> 00:02:50,920
Again, this is coming from Express.

49
00:02:50,930 --> 00:02:56,270
If you have multiple middlewares, effectively you just group them in such manner.

50
00:02:56,270 --> 00:02:59,330
So this is not express validator syntax.

51
00:02:59,330 --> 00:03:02,570
We're just returning from this function in such a way.

52
00:03:02,570 --> 00:03:08,690
So first we want to pass in validate values again, something we're going to set up in a second.

53
00:03:08,690 --> 00:03:13,040
And the second one is going to be our error response.

54
00:03:13,040 --> 00:03:17,540
So let's navigate back over here and notice this function effectively.

55
00:03:17,540 --> 00:03:24,620
We can cut it out because of course we will pass here the validation errors function.

56
00:03:24,620 --> 00:03:25,940
So let me copy and paste.

57
00:03:25,940 --> 00:03:30,960
So this does not change still from this validate values.

58
00:03:30,960 --> 00:03:37,980
We'll have access to the errors, but what we want to change over here is how we handle the response.

59
00:03:37,980 --> 00:03:40,620
Simply I want to go with throw new.

60
00:03:41,330 --> 00:03:47,200
And we're going to go with bad request error and we'll pass in the error messages.

61
00:03:47,210 --> 00:03:54,170
So essentially, this property over here and what's missing is the validate test.

62
00:03:54,170 --> 00:04:00,290
So pretty much for every controller, we are going to invoke this function.

63
00:04:00,810 --> 00:04:04,140
And we'll pass in the values we want to validate.

64
00:04:04,350 --> 00:04:06,810
And then what we're going to get back.

65
00:04:07,580 --> 00:04:10,580
Is two of these middlewares sitting here.

66
00:04:10,580 --> 00:04:12,770
And of course we'll set it up in the controller.

67
00:04:12,770 --> 00:04:14,450
So what am I talking about over here?

68
00:04:14,450 --> 00:04:21,200
Well, since at the moment we only have this for test, we're just going to go with export, then const,

69
00:04:21,410 --> 00:04:24,860
then validate and let's call it test.

70
00:04:24,860 --> 00:04:31,700
And as far as the name later we'll use validate, login, validate, register and hopefully you see

71
00:04:31,700 --> 00:04:32,750
where I'm going with this.

72
00:04:32,800 --> 00:04:36,590
Then we want to invoke with validate errors.

73
00:04:36,590 --> 00:04:43,700
And in here, this is where I want to set up again the angle brackets and we want to set up the values.

74
00:04:43,820 --> 00:04:47,840
So essentially I want to cut it out so the logic doesn't change.

75
00:04:47,840 --> 00:04:50,210
I'm just setting it up over here.

76
00:04:50,940 --> 00:04:52,410
As a middleware.

77
00:04:52,410 --> 00:04:57,390
So pretty much for every controller, this is what we're going to create right now.

78
00:04:57,390 --> 00:05:00,420
So this code we won't touch anymore.

79
00:05:00,540 --> 00:05:05,580
Again, this is something we can reuse from project to project, but then for every controller, I'm

80
00:05:05,580 --> 00:05:11,430
going to say, Hey, I'm looking for such and such value and the length should be this, or some other

81
00:05:11,430 --> 00:05:12,750
property should be that.

82
00:05:12,750 --> 00:05:21,630
And as a result, every time I have a new controller, I can quickly and easily set up a validation

83
00:05:21,630 --> 00:05:24,160
for all of the values that are coming in.

84
00:05:24,180 --> 00:05:26,130
Now in here, let's change it around.

85
00:05:26,130 --> 00:05:30,200
I'm going to say is length is equal to min three.

86
00:05:30,210 --> 00:05:33,390
So that's going to be the minimum and Max is going to be 50.

87
00:05:33,420 --> 00:05:40,290
Again, this is just for testing purposes and we're going to go with name must be let's say.

88
00:05:40,980 --> 00:05:41,730
Between.

89
00:05:42,470 --> 00:05:43,760
And three.

90
00:05:44,670 --> 00:05:45,480
50.

91
00:05:46,330 --> 00:05:48,100
And let's write characters.

92
00:05:49,080 --> 00:05:49,620
Long.

93
00:05:50,220 --> 00:05:52,170
Then let's remove this And.

94
00:05:52,800 --> 00:05:57,240
We get a bunch of methods that we can chain again over here.

95
00:05:57,390 --> 00:06:01,020
For example, we can also use trim, which removes whitespace.

96
00:06:01,020 --> 00:06:03,480
So super, super useful library.

97
00:06:03,510 --> 00:06:11,580
Now we want to go back to a server and instead of setting up everything manually, we want to import.

98
00:06:11,610 --> 00:06:15,210
Now, please keep in mind that of course this is going to be temporary.

99
00:06:15,780 --> 00:06:21,030
I'm going to remove all of this at the end of the video, but for now we want to go validate.

100
00:06:21,980 --> 00:06:23,090
And best.

101
00:06:24,940 --> 00:06:29,500
Let's scroll down and notice where we have our request.

102
00:06:29,530 --> 00:06:30,520
We want to reference it.

103
00:06:30,530 --> 00:06:30,880
That's it.

104
00:06:30,880 --> 00:06:33,010
That's all we want to do over here.

105
00:06:33,010 --> 00:06:36,790
And if I go back to my new request and if I send over here.

106
00:06:37,470 --> 00:06:39,090
Everything should be fine.

107
00:06:39,210 --> 00:06:41,490
But it complains that.

108
00:06:41,580 --> 00:06:42,270
Yep.

109
00:06:42,300 --> 00:06:47,850
Validation error because probably I forgot to add JS over here.

110
00:06:48,060 --> 00:06:48,300
Nope.

111
00:06:48,300 --> 00:06:49,740
That one is over here.

112
00:06:49,740 --> 00:06:52,410
Cannot find module custom errors.

113
00:06:52,410 --> 00:06:53,490
Oh, over here.

114
00:06:53,850 --> 00:06:54,360
Yep.

115
00:06:54,360 --> 00:06:54,960
My bad.

116
00:06:54,990 --> 00:06:57,390
Notice the JS is missing again.

117
00:06:57,390 --> 00:07:01,140
Something I already warned you about that I'll probably forget that.

118
00:07:01,170 --> 00:07:02,250
Let me try one more time.

119
00:07:02,250 --> 00:07:04,290
Let's send notice in here I have.

120
00:07:04,290 --> 00:07:05,310
Hello, Peter.

121
00:07:05,310 --> 00:07:08,850
Because this past both of my checks.

122
00:07:08,850 --> 00:07:13,770
But if I'm going to send an empty one, check it out.

123
00:07:13,890 --> 00:07:15,240
I'll have name is required.

124
00:07:15,240 --> 00:07:17,610
Name must be three and 50 long.

125
00:07:17,610 --> 00:07:22,680
So notice essentially we're getting the list of all the errors.

126
00:07:22,680 --> 00:07:25,350
And if, let's say I'm going to pass in.

127
00:07:26,360 --> 00:07:28,770
First two characters.

128
00:07:28,790 --> 00:07:30,890
Yeah, I passed the first check.

129
00:07:30,920 --> 00:07:36,410
It's not empty, but I still have this one in place, so hopefully it's clear again.

130
00:07:37,090 --> 00:07:44,200
We set up this middleware and essentially for every request, we're just going to create a new instance.

131
00:07:44,200 --> 00:07:48,070
And in here we'll just set up our conditions.

132
00:07:48,070 --> 00:07:49,870
So this one we won't touch.

133
00:07:49,870 --> 00:07:50,470
That's it.

134
00:07:50,500 --> 00:07:51,340
It works.

135
00:07:51,340 --> 00:07:57,670
And pretty much for every request, we'll right away set up what we're looking for and what are the

136
00:07:57,670 --> 00:07:58,090
rules.

137
00:07:58,090 --> 00:08:06,550
And again, as a result, our controllers are going to be very lean because if the request gets all

138
00:08:06,550 --> 00:08:13,210
the way to controller, we already know that it passed all of the tests that we set up over here in

139
00:08:13,210 --> 00:08:15,220
the validation middleware.

