1
00:00:00,080 --> 00:00:00,410
All right.

2
00:00:00,410 --> 00:00:07,070
And once we have a clear understanding how we can set up our own custom validation, let's kick it up

3
00:00:07,070 --> 00:00:10,460
a notch and let's make it more complex.

4
00:00:10,460 --> 00:00:16,020
And in the process, we will clean out our controllers even more.

5
00:00:16,040 --> 00:00:17,510
So what am I talking about?

6
00:00:17,510 --> 00:00:24,920
Well, let's navigate to a job controller first and notice how in all of the routes where we're looking

7
00:00:24,920 --> 00:00:31,880
for that ID parameter, we're also checking whether the actual job exists.

8
00:00:32,659 --> 00:00:39,830
And essentially, we can just move this functionality again to our custom validate function.

9
00:00:40,400 --> 00:00:46,610
And effectively, we'll not only say a few lines of code, but we'll have that functionality in one

10
00:00:46,610 --> 00:00:47,170
place.

11
00:00:47,180 --> 00:00:48,780
So let's try it out.

12
00:00:48,800 --> 00:00:55,400
First, what we need to understand is that in order for this functionality to work, our custom function

13
00:00:55,400 --> 00:00:57,290
is going to be async.

14
00:00:57,680 --> 00:00:59,210
Now, why is that so important?

15
00:00:59,210 --> 00:01:04,989
Because remember, with asynchronous functions, they return a promise.

16
00:01:05,010 --> 00:01:11,190
So essentially, if you have a functionality that's synchronous, yes, this is going to be the approach

17
00:01:11,190 --> 00:01:12,300
you're going to take.

18
00:01:12,420 --> 00:01:19,980
You'll either return true or false, but if you want to set up a synchronous functionality, then the

19
00:01:19,980 --> 00:01:21,780
approach is going to be a little bit different.

20
00:01:21,780 --> 00:01:26,280
Basically, we will right away need to throw the errors.

21
00:01:26,280 --> 00:01:27,590
So what am I talking about?

22
00:01:27,600 --> 00:01:33,540
Well, first of all, let's make this async since we will work with the database to check whether a

23
00:01:33,540 --> 00:01:38,520
job exists, then I want to actually wrap this into a curly bracket.

24
00:01:38,520 --> 00:01:45,300
So instead of implicit return, something we used in the previous video, now we'll go with explicit

25
00:01:45,300 --> 00:01:49,410
return and we'll throw our own errors instead of returning.

26
00:01:49,440 --> 00:01:55,230
True or false, since at the moment custom is already async, we need to use this different approach

27
00:01:55,260 --> 00:02:02,970
First, let's just make sure that our is valid works and I'm going to start by creating a new variable.

28
00:02:02,970 --> 00:02:05,610
I'm just going to say is valid ID.

29
00:02:07,120 --> 00:02:10,030
And that one is going to be equal to this functionality.

30
00:02:10,449 --> 00:02:11,870
So let me cut it out.

31
00:02:11,890 --> 00:02:13,420
Let's set it up over here.

32
00:02:13,450 --> 00:02:16,270
Now, this one is either true or false.

33
00:02:16,270 --> 00:02:20,400
So that, of course, doesn't change what we're getting back from This is valid.

34
00:02:20,440 --> 00:02:27,610
But now we want to set up a condition right here in our custom one, and we're going to go is valid

35
00:02:27,640 --> 00:02:28,150
ID.

36
00:02:29,120 --> 00:02:32,590
And then if is valid, is false.

37
00:02:32,600 --> 00:02:36,910
Essentially, if it's not a valid ID, we want to throw new.

38
00:02:36,920 --> 00:02:40,040
And again, we're looking for our own custom request.

39
00:02:40,550 --> 00:02:41,810
Let's take a look.

40
00:02:41,840 --> 00:02:42,500
Yep.

41
00:02:42,500 --> 00:02:48,200
We imported everything is fine and then we just need to provide the value and that one is going to be

42
00:02:48,200 --> 00:02:48,560
the same.

43
00:02:48,560 --> 00:02:51,020
So we're not going to use this message anymore.

44
00:02:51,020 --> 00:02:53,540
So essentially we're throwing our own errors.

45
00:02:53,540 --> 00:02:59,780
And again, we need to do that because async function is not going to return true or false?

46
00:02:59,780 --> 00:03:02,690
So we kind of need to do this manually, I should say.

47
00:03:03,580 --> 00:03:05,940
So let me take this out.

48
00:03:05,950 --> 00:03:08,200
So I'll use the same message.

49
00:03:08,200 --> 00:03:10,240
So that's the is valid one.

50
00:03:10,240 --> 00:03:13,510
And I don't think there's a need to test it one more time.

51
00:03:14,200 --> 00:03:17,920
So this will be testing our job one anyway.

52
00:03:18,130 --> 00:03:18,580
All right.

53
00:03:18,580 --> 00:03:27,460
So again, we can successfully check for valid MongoDB, but how we can check whether the job exists

54
00:03:27,490 --> 00:03:28,660
in our database?

55
00:03:28,660 --> 00:03:33,190
Well, let's navigate back to job controller and let's take a look at get job.

56
00:03:33,740 --> 00:03:36,680
So in order to get the specific job, here's what we do.

57
00:03:36,710 --> 00:03:40,580
We go here with weight, then job find by ID.

58
00:03:40,730 --> 00:03:46,310
We pass in the ID, and if it doesn't exist, well, then we throw the error.

59
00:03:46,340 --> 00:03:46,960
Correct.

60
00:03:46,970 --> 00:03:49,700
So this one will stay the same in the get job.

61
00:03:49,700 --> 00:03:56,200
But once we have the validation in place, we'll be able to remove the errors.

62
00:03:56,210 --> 00:03:58,430
So let's try it out over here.

63
00:03:58,430 --> 00:04:01,760
First, I'm just going to copy these two lines of code.

64
00:04:01,760 --> 00:04:03,770
Sorry, I don't want to cut it out.

65
00:04:03,980 --> 00:04:05,060
We're just going to copy.

66
00:04:05,060 --> 00:04:06,830
Let's navigate back over here.

67
00:04:07,590 --> 00:04:08,670
Let's paste it.

68
00:04:08,820 --> 00:04:12,790
We don't have the job model, so that's the first thing we want to import.

69
00:04:12,810 --> 00:04:18,899
So I'm going to go here with import job from and then let's go.

70
00:04:19,700 --> 00:04:20,660
One level up.

71
00:04:21,110 --> 00:04:24,860
We're looking for models and more specifically, a job model.

72
00:04:24,890 --> 00:04:27,380
Now, we also want to change some code around here.

73
00:04:27,410 --> 00:04:28,670
Notice this is an ID.

74
00:04:28,820 --> 00:04:35,450
Well, at the moment ID is actually in the value parameter, so we're still looking for the specific

75
00:04:35,450 --> 00:04:35,870
job.

76
00:04:35,870 --> 00:04:40,130
And if the job doesn't exist, we want to get the not found error.

77
00:04:40,160 --> 00:04:45,770
So of course we also want to import the not found custom error.

78
00:04:46,670 --> 00:04:49,290
And we want to change this one to a value.

79
00:04:49,310 --> 00:04:52,760
So let's navigate up and grab that not found error.

80
00:04:52,910 --> 00:04:58,760
So right after the bad request one, I also want to get the not found.

81
00:04:58,850 --> 00:05:08,030
Let's save and we're going to navigate back to a job controller and one by one, let's remove some lines

82
00:05:08,030 --> 00:05:12,350
of code from the controllers so this one stays the same.

83
00:05:13,010 --> 00:05:15,830
We're still grabbing the correct job and all that.

84
00:05:16,160 --> 00:05:20,740
Then when it comes to the update job, we don't need this line of code anymore.

85
00:05:21,530 --> 00:05:22,970
So this one we won't use.

86
00:05:22,970 --> 00:05:26,270
And also same goes for remove job.

87
00:05:26,450 --> 00:05:30,950
So now essentially we know that by the time we get to the controller.

88
00:05:32,190 --> 00:05:33,420
Everything should be correct.

89
00:05:33,420 --> 00:05:40,170
If the job doesn't exist in our database, then the validation layer is already going to check it and

90
00:05:40,170 --> 00:05:47,730
essentially send back the error response so we can move up over here and actually remove this import

91
00:05:47,730 --> 00:05:48,450
as well.

92
00:05:48,450 --> 00:05:54,270
And again, our main goal is to keep our controllers as lean as possible.

93
00:05:55,120 --> 00:05:58,810
And now let's navigate to get single job.

94
00:05:58,840 --> 00:05:59,920
Let's test it out.

95
00:05:59,920 --> 00:06:04,150
So now we have this invalid MongoDB so that one still works.

96
00:06:04,150 --> 00:06:05,800
Let me make sure that.

97
00:06:06,830 --> 00:06:11,300
I have some kind of value over here, and I think I lost it, so.

98
00:06:12,530 --> 00:06:13,790
Let me take this one.

99
00:06:14,920 --> 00:06:16,750
Where we have get single job.

100
00:06:18,270 --> 00:06:19,170
Let's send.

101
00:06:19,200 --> 00:06:22,790
So if the ID is correct, then of course we get the proper one.

102
00:06:22,800 --> 00:06:25,340
We already tested the invalid one.

103
00:06:25,350 --> 00:06:30,840
So now let's just change around and actually keep the syntax but provide the wrong value.

104
00:06:30,840 --> 00:06:32,910
And technically everything works.

105
00:06:32,910 --> 00:06:35,970
We get the message, but notice the status code.

106
00:06:36,270 --> 00:06:39,540
Essentially we're getting back 400.

107
00:06:39,570 --> 00:06:39,990
Why?

108
00:06:39,990 --> 00:06:46,140
Well, because if we take a look at our validation middleware, we'll see that for pretty much all of

109
00:06:46,140 --> 00:06:50,760
the errors we are throwing 400, which is the case for the most part.

110
00:06:50,760 --> 00:06:57,240
But in this case, since we're looking for specific job, we actually want to go with 404 and therefore

111
00:06:57,240 --> 00:07:00,710
we'll need to do a bit more acrobatics over here.

112
00:07:00,720 --> 00:07:05,610
So we're only going to run this functionality if we have some errors.

113
00:07:05,730 --> 00:07:10,830
And essentially the way I'm going to check it, I'm going to look for the first item in this array that

114
00:07:10,830 --> 00:07:16,050
I'm getting back, and I'm just going to make sure that it starts with no job.

115
00:07:16,080 --> 00:07:21,160
Because remember, when it comes to a message over here, I go with no job.

116
00:07:21,160 --> 00:07:21,640
And.

117
00:07:22,300 --> 00:07:23,110
Rest of the stuff.

118
00:07:23,110 --> 00:07:27,570
So if that's the case, I'll actually throw not found error.

119
00:07:27,580 --> 00:07:31,800
So let's go here and let's type error messages.

120
00:07:31,810 --> 00:07:37,450
We know that we're going to get back an array and we're just looking for the first item since in this

121
00:07:37,450 --> 00:07:39,670
case they're not going to be multiple messages.

122
00:07:39,670 --> 00:07:41,500
There's only going to be one.

123
00:07:41,500 --> 00:07:44,080
So let's go with start with.

124
00:07:44,320 --> 00:07:49,420
That's the JavaScript string method and I'm just going to go with no job.

125
00:07:49,420 --> 00:07:52,210
So if that's the case, we want to throw.

126
00:07:52,930 --> 00:07:53,950
Another error.

127
00:07:53,950 --> 00:07:57,020
And this is going to be equal to not found error.

128
00:07:57,040 --> 00:08:00,610
And essentially, let's pass in the same error messages.

129
00:08:00,610 --> 00:08:03,550
So now let's try it out where we have single job.

130
00:08:03,580 --> 00:08:06,700
Again, I'm keeping all of this the same.

131
00:08:06,700 --> 00:08:16,060
Let's send and now we actually have 404 and with this in place, we have the setup to not only check

132
00:08:16,060 --> 00:08:25,540
whether the MongoDB ID is valid, but also we right away check whether the job actually exists in our

133
00:08:25,540 --> 00:08:26,350
database.

134
00:08:26,590 --> 00:08:29,710
And before I let you go, I want to mention two more things.

135
00:08:29,710 --> 00:08:33,730
First, we can refactor our controllers even more.

136
00:08:33,940 --> 00:08:42,159
So when we look at the get job update job as well as delete job, we can see that we always start by

137
00:08:42,159 --> 00:08:47,980
destructuring that ID, But if you want to save on some lines of code, of course you can simply go

138
00:08:47,980 --> 00:08:52,790
here with req, dot params dot and then the id.

139
00:08:53,180 --> 00:09:00,860
So of course in that case we don't need this line of code anymore so we can simplify and shorten our

140
00:09:00,860 --> 00:09:02,240
controllers even more.

141
00:09:02,870 --> 00:09:07,280
Of course we just need to repeat the same deal for rest of them.

142
00:09:07,280 --> 00:09:10,670
So in this case I'll just copy and paste because it's going to be faster.

143
00:09:10,790 --> 00:09:12,530
So let's grab it over here.

144
00:09:12,530 --> 00:09:14,840
We don't need params anymore.

145
00:09:15,680 --> 00:09:16,760
Let's remove.

146
00:09:16,790 --> 00:09:18,470
And the same goes.

147
00:09:19,280 --> 00:09:20,780
For deleting the job.

148
00:09:21,050 --> 00:09:23,450
We don't want to destructure it again.

149
00:09:23,450 --> 00:09:25,040
This is totally a preference.

150
00:09:25,310 --> 00:09:29,540
I actually prefer to destructure it, but I definitely want to showcase this.

151
00:09:29,810 --> 00:09:40,850
And lastly, just remember that technically in here we can throw a straight up JavaScript error because

152
00:09:40,880 --> 00:09:47,540
again, we're setting up those custom errors actually in the.

153
00:09:48,590 --> 00:09:50,670
With validation errors.

154
00:09:50,690 --> 00:09:55,090
So here I'm just checking for no job and then I go with not found error.

155
00:09:55,130 --> 00:10:03,650
So you can change these ones, both of them to a straight up JavaScript error and everything is still

156
00:10:03,650 --> 00:10:04,560
going to work.

157
00:10:04,580 --> 00:10:11,930
So it doesn't really matter whether we go here with our own custom error class or we go with the JavaScript

158
00:10:11,930 --> 00:10:18,680
one, because at the end of the day this is where we actually set up that error class.

159
00:10:18,710 --> 00:10:24,830
Now in my case I will still leave the custom ones in both places, but definitely wanted to mention

160
00:10:24,830 --> 00:10:25,240
that.

161
00:10:25,250 --> 00:10:28,670
And with this in place, we're ready to move on to the next topic.

