1
00:00:00,200 --> 00:00:00,830
All right.

2
00:00:01,070 --> 00:00:07,460
I don't know whether you remember or not, but some time ago I showcased that we can have two potential

3
00:00:07,460 --> 00:00:11,870
errors when it comes to IDs in parameter.

4
00:00:11,900 --> 00:00:19,670
So, for example, if we have the same syntax but a wrong value, basically the product doesn't exist

5
00:00:19,670 --> 00:00:25,760
in our database or in our case I guess more correctly job, then we get back 404.

6
00:00:25,910 --> 00:00:31,700
Now if we provide invalid syntax, then we get different type of error from the MongoDB.

7
00:00:32,030 --> 00:00:35,360
And this is something that essentially we want to fix.

8
00:00:35,360 --> 00:00:39,560
So we will create another validation middleware.

9
00:00:39,560 --> 00:00:45,890
In this case I'm going to call this validate ID param and in the process we'll cover how to validate

10
00:00:45,890 --> 00:00:49,610
parameters with express Validator.

11
00:00:49,790 --> 00:00:52,580
First, let's navigate to.

12
00:00:53,370 --> 00:00:54,540
Our middleware.

13
00:00:55,210 --> 00:01:01,120
We're looking for validation middleware and if we want to access the values in the body.

14
00:01:01,150 --> 00:01:08,410
Of course we're looking for the body helper, but if we want to get the value from params, we want

15
00:01:08,410 --> 00:01:09,400
to go with params.

16
00:01:09,430 --> 00:01:11,010
So it starts with import.

17
00:01:11,020 --> 00:01:13,270
Yes, those are two different places.

18
00:01:13,270 --> 00:01:17,730
So if we want to validate the params we want to go with params.

19
00:01:17,740 --> 00:01:19,690
So that's the first thing we want to import.

20
00:01:19,720 --> 00:01:27,580
Then let's set up our middleware and honestly most of it is going to be exactly the same, but we'll

21
00:01:27,580 --> 00:01:33,580
just take it to the next level and we'll essentially set up a custom function.

22
00:01:33,580 --> 00:01:34,660
So what am I talking about?

23
00:01:34,660 --> 00:01:37,030
First of all, let's come up with a name.

24
00:01:37,030 --> 00:01:39,810
I'm going to go with validate ID param.

25
00:01:39,820 --> 00:01:41,080
So same deal.

26
00:01:41,080 --> 00:01:45,340
We want to go with validation errors and in here.

27
00:01:45,940 --> 00:01:48,290
We want to pass in what we want to validate.

28
00:01:48,310 --> 00:01:53,860
So first of all, since we have access to parameters instead of body, we're going to go with parameters.

29
00:01:53,950 --> 00:01:56,450
Now, remember the name.

30
00:01:56,470 --> 00:02:00,250
This is very, very important With me, navigate to routes.

31
00:02:00,280 --> 00:02:02,020
What is the name of the parameter?

32
00:02:02,050 --> 00:02:02,920
It's an ID.

33
00:02:03,340 --> 00:02:03,970
Correct.

34
00:02:03,970 --> 00:02:05,550
So it needs to match.

35
00:02:05,560 --> 00:02:11,560
So of course, in here we're going to go with ID and then we have this custom function.

36
00:02:11,740 --> 00:02:19,390
So if we want to access the actual value of the ID that's coming in with the request, we simply pass

37
00:02:19,390 --> 00:02:23,650
in the callback function and then whatever we name the parameter.

38
00:02:23,650 --> 00:02:30,010
So in my case, I'm going to go with value and depending on what we return from the function, either

39
00:02:30,010 --> 00:02:32,290
the request is going to be validated.

40
00:02:32,290 --> 00:02:38,350
So basically we will pass it on to the next middleware, in our case the controller, or we're going

41
00:02:38,350 --> 00:02:46,760
to reject it and we can simply test it out with, let's say, return and let's go with true for now.

42
00:02:46,940 --> 00:02:51,230
Yes, eventually, of course, we'll set up a more complex logic.

43
00:02:51,230 --> 00:02:53,990
But for now I just want to showcase how it works.

44
00:02:53,990 --> 00:03:00,320
And then if there's some kind of error, the message I want to use is following.

45
00:03:00,320 --> 00:03:03,800
I'm just going to say invalid mongo.

46
00:03:04,630 --> 00:03:07,660
The B ID, let's save it.

47
00:03:07,660 --> 00:03:11,770
And now let's navigate to our job router and let's import it.

48
00:03:11,770 --> 00:03:12,340
Correct.

49
00:03:12,900 --> 00:03:19,050
So now we have first middleware and the second one is going to be validate ID param.

50
00:03:19,260 --> 00:03:22,560
So which controllers are using it?

51
00:03:22,560 --> 00:03:25,860
So which routes are using the ID?

52
00:03:25,860 --> 00:03:30,510
Well this one get job update, job and delete job.

53
00:03:30,570 --> 00:03:34,500
So in here, let's go to the first one.

54
00:03:35,170 --> 00:03:40,300
Let's add a comma and then let's look for our validate ID param.

55
00:03:40,390 --> 00:03:46,780
Then we want to set up the same one for update job and of course also for delete job.

56
00:03:46,960 --> 00:03:50,890
Let's save it and let's test it out right now.

57
00:03:51,010 --> 00:03:56,860
So I'm going to send it over here and once we make the request, we can nicely see that we're getting

58
00:03:56,860 --> 00:03:59,760
the correct response from the controller.

59
00:03:59,770 --> 00:04:02,980
So that means that this validation works.

60
00:04:02,990 --> 00:04:07,670
Essentially, if we return true, we're not kicking back the request.

61
00:04:07,690 --> 00:04:12,530
However, if I'm going to change this around and if I'm going to go with false.

62
00:04:12,550 --> 00:04:13,720
Now check it out.

63
00:04:13,730 --> 00:04:17,709
Same request I send and I get back invalid MongoDB.

64
00:04:18,070 --> 00:04:24,790
Again, this just demonstrates that if we return false then we right away.

65
00:04:25,350 --> 00:04:28,720
Invoke this whole validation setup.

66
00:04:28,740 --> 00:04:33,090
So essentially we'll send back the error with invalid MongoDB.

67
00:04:33,510 --> 00:04:37,470
Now if we return true, then everything is going to be correct.

68
00:04:37,500 --> 00:04:43,470
So how we can validate whether it is a valid MongoDB ID?

69
00:04:43,890 --> 00:04:46,110
Well, first of all, we'll need Mongoose.

70
00:04:46,320 --> 00:04:50,140
So we will import Mongoose from Mongoose.

71
00:04:50,160 --> 00:04:51,690
Then let's keep on moving.

72
00:04:51,840 --> 00:04:55,830
And essentially, I can set up the implicit return.

73
00:04:56,750 --> 00:05:07,360
And we want to go here with mangoes, then dot types, then object ID and it has a method of is valid.

74
00:05:07,370 --> 00:05:11,840
So this is going to return either true or false.

75
00:05:11,840 --> 00:05:15,950
And of course the way I access the actual ID is over here.

76
00:05:15,950 --> 00:05:19,280
So if this returns true, then everything is fine.

77
00:05:19,280 --> 00:05:20,900
We pass it on to controller.

78
00:05:20,900 --> 00:05:24,980
If not, if it returns false, then you'll see the error message.

79
00:05:24,980 --> 00:05:26,780
So now let's try it out.

80
00:05:26,780 --> 00:05:34,160
I'm going to first grab the correct ID, So let me navigate right now back to my thunder client.

81
00:05:34,160 --> 00:05:35,390
Get all jobs.

82
00:05:36,640 --> 00:05:38,680
And here, let's send it one more time.

83
00:05:38,680 --> 00:05:41,260
I think I'm going to mess with the coding addict.

84
00:05:41,260 --> 00:05:45,940
At least I'm going to test the coding addict, then get single job.

85
00:05:47,090 --> 00:05:47,930
Let me send it here.

86
00:05:47,930 --> 00:05:49,340
Yeah, everything is correct.

87
00:05:49,430 --> 00:05:55,580
So if I'm going to just mess with the values but keep the syntax the same.

88
00:05:55,760 --> 00:05:57,920
So this is coming from our controller.

89
00:05:57,920 --> 00:05:59,460
We're going to go with 404.

90
00:05:59,480 --> 00:06:05,990
But if I'll start removing the values or adding some extra characters, you'll notice something interesting.

91
00:06:05,990 --> 00:06:12,140
So now we have this bad request, which is exactly what I was expecting.

92
00:06:12,140 --> 00:06:20,390
So if you want to test the params, you want to go with param and also whether it's a value in the body

93
00:06:20,390 --> 00:06:27,560
or param, you can go with your own custom function and just remember if the return is going to be true,

94
00:06:27,560 --> 00:06:28,820
everything is going to be fine.

95
00:06:28,820 --> 00:06:33,350
It's going to be passed on to the controller or to the next middleware.

96
00:06:33,350 --> 00:06:40,760
But if the result of this functionality is going to be false, then of course server is going to send

97
00:06:40,760 --> 00:06:42,980
back the error response.

