1
00:00:00,050 --> 00:00:00,380
All right.

2
00:00:00,380 --> 00:00:04,400
And now I want to set up validation for job input.

3
00:00:04,400 --> 00:00:05,970
So what am I talking about?

4
00:00:05,990 --> 00:00:10,460
Well, let's take a look at our current controllers.

5
00:00:10,460 --> 00:00:16,910
We have one for get all jobs, create job, get single job, update job and delete job.

6
00:00:16,910 --> 00:00:22,370
And as you're looking at these controllers, which ones require req.body?

7
00:00:22,370 --> 00:00:25,790
So which ones are expecting some kind of value?

8
00:00:25,820 --> 00:00:31,760
Well, we have one for create job and we also have one for update job.

9
00:00:32,150 --> 00:00:32,689
Correct.

10
00:00:32,689 --> 00:00:35,630
So both of these are looking for req.body.

11
00:00:35,630 --> 00:00:38,510
And in there, of course, we'll have the Json value.

12
00:00:38,540 --> 00:00:45,860
So the cool thing is that we can set up, validate job input, essentially our validation and we can

13
00:00:45,860 --> 00:00:46,490
reuse it.

14
00:00:46,520 --> 00:00:49,430
We can use it for both requests.

15
00:00:49,430 --> 00:00:50,960
So let's try it out.

16
00:00:50,960 --> 00:00:56,870
And first we're going to navigate to, you know what, let's go to a server because I want to remove

17
00:00:56,870 --> 00:00:57,260
it.

18
00:00:57,880 --> 00:00:59,890
So we're not going to use validate test.

19
00:00:59,920 --> 00:01:02,710
Of course, that was just for demonstration purposes.

20
00:01:02,800 --> 00:01:05,400
And we also don't need this request.

21
00:01:05,410 --> 00:01:07,880
Just want to keep it nice and clean.

22
00:01:07,900 --> 00:01:10,360
And then when it comes to validation.

23
00:01:11,190 --> 00:01:14,490
I'm not going to leave it for your reference because you do have readmy.

24
00:01:15,480 --> 00:01:15,980
Same deal.

25
00:01:15,990 --> 00:01:23,880
I'm just going to remove it and effectively we right away want to export since we'll use it in our router.

26
00:01:24,530 --> 00:01:25,520
Let's come up with a name.

27
00:01:25,520 --> 00:01:28,730
I'm going to go with validate job input.

28
00:01:28,760 --> 00:01:35,210
Then remember, we have our function with validation errors and it's looking for the array.

29
00:01:35,240 --> 00:01:36,650
And this is what I'm talking about.

30
00:01:36,680 --> 00:01:42,920
Basically, once you have the setup in place, once you understand how express validate their works,

31
00:01:43,130 --> 00:01:50,500
essentially you'll just need to invoke this function and pass in which values you want to validate.

32
00:01:50,510 --> 00:01:54,720
And as far as the reference, well, we will use our schema.

33
00:01:54,740 --> 00:01:58,730
So in a schema we can clearly see what we're expecting.

34
00:01:58,850 --> 00:02:03,710
We'll go one by one and we'll decide, okay, what do we want from this property?

35
00:02:03,710 --> 00:02:08,360
Because of course, in order to create our instance, well, these are the values that we're looking

36
00:02:08,360 --> 00:02:08,740
for.

37
00:02:08,750 --> 00:02:09,259
Correct?

38
00:02:09,259 --> 00:02:15,650
So long story short, let's look for body, since that's where the values are going to be located.

39
00:02:15,650 --> 00:02:17,720
And what's the first property name?

40
00:02:17,720 --> 00:02:22,850
Well, company and effectively, I just want to have some kind of value.

41
00:02:22,850 --> 00:02:24,600
So I'm going to go with not empty.

42
00:02:24,600 --> 00:02:29,720
And then if there is an error, what is going to be my message?

43
00:02:29,730 --> 00:02:33,000
I'm just going to say company is required.

44
00:02:35,180 --> 00:02:35,850
Okay, good.

45
00:02:35,870 --> 00:02:42,350
Now we can copy and paste since it's going to be the same deal for position and job location.

46
00:02:42,380 --> 00:02:49,340
Again, if we take a look at the job model, notice position is simply a string and same goes for the

47
00:02:49,340 --> 00:02:50,300
job location.

48
00:02:50,300 --> 00:02:52,840
So these two are going to be a little bit different.

49
00:02:52,850 --> 00:02:58,160
But when it comes to job, location and position, I'm just checking for empty values.

50
00:02:58,190 --> 00:03:01,100
Now, can you add more validation here?

51
00:03:01,130 --> 00:03:01,820
Of course.

52
00:03:01,820 --> 00:03:07,280
So I'm just showing you the most basic one, but if you want to check for some different things, just

53
00:03:07,280 --> 00:03:11,620
visit the docs for Express Validator and you'll see all the options they provide.

54
00:03:11,630 --> 00:03:17,000
And of course, also please keep in mind that this is just our first validation apart from the test

55
00:03:17,000 --> 00:03:17,420
one.

56
00:03:17,420 --> 00:03:21,710
So as we're building the project, we'll cover some other options as well.

57
00:03:21,710 --> 00:03:23,500
So in here I just want to change it.

58
00:03:23,510 --> 00:03:29,690
So instead of company, this is going to be position and then the last one will be job location.

59
00:03:29,690 --> 00:03:34,940
Now please make sure that the name in here is the same.

60
00:03:34,940 --> 00:03:41,750
So yes, we want to go here in camel case since that's how we're going to be sending from the front

61
00:03:41,750 --> 00:03:43,760
end as well as the thunder client.

62
00:03:43,790 --> 00:03:45,880
Now, in here, of course we can change it.

63
00:03:45,890 --> 00:03:52,340
We can say job location is required and then we have job status and job type.

64
00:03:52,430 --> 00:03:57,770
And if we want to check for enums, we want to use is in.

65
00:03:57,800 --> 00:04:00,740
Now of course we can hardcode those values.

66
00:04:00,740 --> 00:04:02,270
We can hardcode the array.

67
00:04:02,270 --> 00:04:03,350
But remember.

68
00:04:04,060 --> 00:04:06,350
We set up the constants in the previous video.

69
00:04:06,370 --> 00:04:06,990
Correct.

70
00:04:07,000 --> 00:04:09,610
So let's utilize that.

71
00:04:09,760 --> 00:04:11,410
Let's go here with the body.

72
00:04:11,440 --> 00:04:13,180
Then what is the property?

73
00:04:13,180 --> 00:04:14,000
I'm checking.

74
00:04:14,020 --> 00:04:15,760
It's going to be a status.

75
00:04:15,940 --> 00:04:23,380
And then, like I said, we want to chain is in and instead of hard coding the array, well, let's

76
00:04:23,380 --> 00:04:26,020
try to get the job status one over here.

77
00:04:26,350 --> 00:04:27,670
Let me import.

78
00:04:27,670 --> 00:04:30,820
And now, of course, I want to go with object dot values.

79
00:04:30,820 --> 00:04:36,550
So remember, that's the built in method we have in the JavaScript, which is going to get only the

80
00:04:36,550 --> 00:04:36,940
values.

81
00:04:36,940 --> 00:04:39,790
So again, the result is going to be exactly the same.

82
00:04:39,820 --> 00:04:45,460
We'll get the array, but instead of hard coding, well, we're getting it from the constants, which

83
00:04:45,460 --> 00:04:48,310
means that these two will match always.

84
00:04:48,840 --> 00:04:53,850
So that way we don't need to worry about making some kind of typo.

85
00:04:53,850 --> 00:04:57,780
And then as far as the width message, let me think.

86
00:04:58,380 --> 00:05:02,370
Um, I think I'm going to go with invalid status.

87
00:05:03,360 --> 00:05:04,110
Value.

88
00:05:04,440 --> 00:05:05,370
Let's save it.

89
00:05:05,370 --> 00:05:09,000
And then, of course, we want to do the same thing for the job type.

90
00:05:09,240 --> 00:05:10,530
So let me scroll up.

91
00:05:10,530 --> 00:05:15,720
I don't think the auto import is going to work if I'm going to copy and paste.

92
00:05:16,940 --> 00:05:18,230
Then we want to change it.

93
00:05:19,400 --> 00:05:22,010
Then we want to copy in here.

94
00:05:22,220 --> 00:05:23,390
Let's go with job type.

95
00:05:25,770 --> 00:05:26,520
Job type.

96
00:05:26,520 --> 00:05:34,350
And then we're looking for different object and we'll say invalid type value.

97
00:05:34,380 --> 00:05:40,500
Now, once we have the validate job input in place, let's navigate to our router.

98
00:05:41,020 --> 00:05:44,080
So we're looking for routes our job router.

99
00:05:44,080 --> 00:05:50,140
And remember, when it comes to middleware, essentially we can stick it in front of the controller.

100
00:05:50,140 --> 00:05:50,710
Correct.

101
00:05:50,710 --> 00:05:56,880
And again, which two requests are going to look for the values in the Req.body?

102
00:05:56,890 --> 00:06:00,490
Well, the post one and the patch one.

103
00:06:00,490 --> 00:06:02,740
So create job and update job.

104
00:06:02,740 --> 00:06:08,590
And what's really awesome, once I have validate job input in place, of course I can reuse it.

105
00:06:08,590 --> 00:06:10,030
So this is what I'm talking about.

106
00:06:10,030 --> 00:06:14,170
Once you have the entire setup in place, it's not as bad as it looks.

107
00:06:14,200 --> 00:06:19,180
It's just that initial time when you need to build everything after that.

108
00:06:19,800 --> 00:06:25,920
You can set up the validation for multiple requests and it's actually much faster than you think.

109
00:06:25,920 --> 00:06:26,910
And trust me.

110
00:06:27,520 --> 00:06:34,270
It just saves you a bunch of mental energy because your controllers are way slimmer.

111
00:06:34,270 --> 00:06:37,720
So now let's navigate to a job router.

112
00:06:37,750 --> 00:06:40,180
In here, let's first look for the post one.

113
00:06:40,940 --> 00:06:44,870
And we're looking for validate job input.

114
00:06:45,380 --> 00:06:45,890
Yep.

115
00:06:46,040 --> 00:06:48,140
And we want to add the comma.

116
00:06:48,290 --> 00:06:52,940
And also we want to do the same thing, of course, for the patch one.

117
00:06:53,120 --> 00:06:57,590
And now we just need to go to our thunder client and test it out.

118
00:06:57,620 --> 00:07:04,700
Now in the Readme, you will find something very useful and this is effectively a Json code you want

119
00:07:04,700 --> 00:07:07,640
to use in two of those requests.

120
00:07:07,640 --> 00:07:12,830
So of course you can set it up from the scratch, but this is just going to be a nice starting point.

121
00:07:12,830 --> 00:07:18,230
And then of course, in order to test whether the validation works correctly, you can just change them

122
00:07:18,350 --> 00:07:21,250
again just so you can save a little bit of time.

123
00:07:21,260 --> 00:07:24,590
So first, let me navigate, I guess, to the Thunder client.

124
00:07:25,840 --> 00:07:28,660
Let me look for my great job.

125
00:07:28,660 --> 00:07:31,960
Let me send it here empty and in here.

126
00:07:31,960 --> 00:07:35,200
It's basically going to complain that, yep, job model.

127
00:07:35,440 --> 00:07:35,890
All right.

128
00:07:35,890 --> 00:07:37,870
Let me go back right now.

129
00:07:38,080 --> 00:07:42,160
So guess I'm looking for constants or in the job model.

130
00:07:42,160 --> 00:07:42,610
Yep.

131
00:07:43,660 --> 00:07:44,680
This is the thing.

132
00:07:44,680 --> 00:07:49,660
I mean, I just keep forgetting about those JS extensions.

133
00:07:49,660 --> 00:07:51,040
Let me try one more time.

134
00:07:51,040 --> 00:07:51,790
Great job.

135
00:07:51,790 --> 00:07:53,500
Let me send a notice.

136
00:07:53,500 --> 00:07:56,980
Companies required position is required and down the list.

137
00:07:57,250 --> 00:08:05,230
So at the moment, since I'm sending pretty much empty object, well I'll have a bunch of errors.

138
00:08:05,230 --> 00:08:05,770
Correct.

139
00:08:05,770 --> 00:08:07,930
So now let me copy and paste.

140
00:08:07,930 --> 00:08:11,470
So company is going to be coding addict, blah blah blah blah blah.

141
00:08:11,870 --> 00:08:13,870
Now let me send it and check it out.

142
00:08:13,870 --> 00:08:20,980
We successfully created a job and now it's going to be very easy to see whether our validator works.

143
00:08:20,980 --> 00:08:23,650
So, for example, I'm going to go with a remote.

144
00:08:23,650 --> 00:08:28,420
Remember, this is not a valid job type, so let's see whether everything is correct.

145
00:08:28,420 --> 00:08:34,120
And yep, I get back 400 and the value of course is invalid.

146
00:08:34,299 --> 00:08:39,700
Then let's also try to send without the company value.

147
00:08:40,090 --> 00:08:41,289
Let me save it over here.

148
00:08:41,620 --> 00:08:42,039
Yep.

149
00:08:42,039 --> 00:08:43,340
Company is required.

150
00:08:43,340 --> 00:08:46,280
So as you can see, our validator nicely works.

151
00:08:46,280 --> 00:08:49,250
Now let's go back to all jobs.

152
00:08:49,460 --> 00:08:51,500
Let me look for the last one.

153
00:08:51,500 --> 00:08:54,830
Since those are the values I'm going to reuse.

154
00:08:55,040 --> 00:08:58,340
Then we want to first navigate to edit job.

155
00:08:58,820 --> 00:09:03,890
I'm going to change the ID, so I want to make sure that I'm using the correct one.

156
00:09:04,250 --> 00:09:10,370
And again, remember, there is a validator, so if I'm going to send position DevOps, of course I'm

157
00:09:10,370 --> 00:09:12,380
going to get company is required.

158
00:09:12,470 --> 00:09:15,470
So now let's navigate back to create job.

159
00:09:16,930 --> 00:09:21,580
And let's just copy and paste and instead of the.

160
00:09:22,350 --> 00:09:25,920
Position now let me change it to front end.

161
00:09:26,840 --> 00:09:27,640
Let me send.

162
00:09:27,650 --> 00:09:29,090
Yep, everything's correct.

163
00:09:29,090 --> 00:09:30,800
And now position is correct.

164
00:09:30,890 --> 00:09:33,620
And of course you can test it out with more options.

165
00:09:33,620 --> 00:09:41,660
For example, you can send here without job location or you can change the job type value to something

166
00:09:41,660 --> 00:09:42,710
that's invalid.

167
00:09:42,710 --> 00:09:44,420
So this is totally up to you.

168
00:09:44,420 --> 00:09:46,440
I think in my case, I'm going to stop.

169
00:09:46,460 --> 00:09:53,390
Hopefully it's clear that our validate job input works as expected and now we can move on to the next

170
00:09:53,390 --> 00:09:53,930
step.

