1 00:00:01,210 --> 00:00:06,840 In this video we're going to take this validation step right here and extract it into a helper middleware. 2 00:00:06,910 --> 00:00:12,780 So inside of my middleware directory I'm going to make a new file and we're going to call this new middleware 3 00:00:13,200 --> 00:00:21,330 validate request Dot T S then inside of here at the very top we're going to add in a couple of imports. 4 00:00:21,350 --> 00:00:30,740 Right away I'm going to import request response and next function from Express. 5 00:00:30,740 --> 00:00:32,320 Now remember these are types. 6 00:00:32,330 --> 00:00:36,950 The only reason that we are importing these is to be able to provide some type annotations for typescript 7 00:00:36,950 --> 00:00:39,440 to understand some information about the middleware. 8 00:00:39,460 --> 00:00:43,800 We're gonna be building in effect it's very similar to the middleware we already put together. 9 00:00:43,800 --> 00:00:49,340 The air handler one remember we had to import request response and next function to describe the type 10 00:00:49,430 --> 00:00:56,410 of these arguments that are going to be flowing into our middleware after that will then import validation 11 00:00:56,440 --> 00:01:08,670 results from Express validator and I'll also get that request validation error from up one directory 12 00:01:09,090 --> 00:01:09,540 errors 13 00:01:12,800 --> 00:01:16,290 who forgot the path request validation error. 14 00:01:16,320 --> 00:01:19,020 That's better. 15 00:01:19,050 --> 00:01:19,280 All right. 16 00:01:19,290 --> 00:01:21,400 After that we can put together our actual middleware. 17 00:01:21,400 --> 00:01:24,150 So going to export a new function called validate request 18 00:01:28,130 --> 00:01:30,710 this thing will be called with Req. 19 00:01:31,020 --> 00:01:36,240 That is a request rez that is a response and next. 20 00:01:36,240 --> 00:01:38,410 That is a next function. 21 00:01:38,430 --> 00:01:41,140 And I got to say this you can see that entire statement. 22 00:01:41,140 --> 00:01:42,290 Here we go. 23 00:01:42,330 --> 00:01:47,720 Now you might be curious why does this look different than validate request or some air handler member 24 00:01:47,730 --> 00:01:52,300 air handler was an error handling middleware that is a very different type of middleware. 25 00:01:52,530 --> 00:01:57,870 EXPRESS distinguishes between an area handling middleware and a normal one on the number of arguments 26 00:01:57,900 --> 00:01:59,750 that the function accepts. 27 00:01:59,820 --> 00:02:02,040 So air handler over here is an area handling middleware. 28 00:02:02,070 --> 00:02:03,640 So it has four arguments. 29 00:02:03,690 --> 00:02:05,870 It's got that extra air one in there. 30 00:02:05,910 --> 00:02:10,770 In this case we're not making a distinct error handling middleware because we're not trying to capture 31 00:02:10,770 --> 00:02:11,160 errors. 32 00:02:11,160 --> 00:02:13,100 Instead we are trying to produce errors. 33 00:02:13,110 --> 00:02:14,660 We want to throw errors. 34 00:02:14,790 --> 00:02:16,130 If the request is not valid. 35 00:02:16,170 --> 00:02:18,720 That's what we're trying to do it inside of your. 36 00:02:18,970 --> 00:02:25,480 So inside the body of this middleware will say take a look at any errors that might have occurred using 37 00:02:25,480 --> 00:02:36,510 validation results on the incoming request if errors is not empty so notice the not right there then 38 00:02:36,510 --> 00:02:49,600 we will go ahead and throw a new request validation error and pass in our array of errors if we get 39 00:02:49,600 --> 00:02:50,960 past this step right here. 40 00:02:51,010 --> 00:02:57,180 That means we want to move on to the next middleware in our chain or potentially our final root handler. 41 00:02:57,220 --> 00:03:03,710 So if we get past that if statement we will simply call next like so. 42 00:03:03,860 --> 00:03:04,150 All right. 43 00:03:04,160 --> 00:03:05,530 That's pretty much it. 44 00:03:05,540 --> 00:03:10,660 So now we can go back over to sign up and sign in and make use of this new middleware. 45 00:03:10,670 --> 00:03:12,590 Let's first go to sign up. 46 00:03:12,590 --> 00:03:14,620 Back side of sign up at the very top. 47 00:03:14,630 --> 00:03:24,130 I'm going to add in an import or validate requests from middleware as validate requests I'll collapse 48 00:03:24,130 --> 00:03:30,350 my sidebar as you can see that whole line and then all we have to do is make sure that we add this thing 49 00:03:30,380 --> 00:03:34,730 in after after we do that validation check. 50 00:03:34,750 --> 00:03:39,130 Remember all the middleware is run in a row and they run sequentially in order. 51 00:03:39,250 --> 00:03:43,540 So we tried to wire up this middleware before we do the actual validation. 52 00:03:43,750 --> 00:03:45,730 Then we're going to run the middleware too early. 53 00:03:45,760 --> 00:03:48,010 We have to do the validation first and then 54 00:03:50,990 --> 00:03:55,880 handle that possible error right after we're going to add and validate request. 55 00:03:55,880 --> 00:03:58,120 Right there OK. 56 00:03:58,120 --> 00:04:06,200 So now we can remove all the aircheck and stuff from the request handler that looks better. 57 00:04:06,320 --> 00:04:12,020 We can save the sign up file then go back over to the assigned in route handler so we're just working 58 00:04:12,020 --> 00:04:14,700 on and we'll do the same thing over here. 59 00:04:14,740 --> 00:04:18,320 So at the very top I will import validate request 60 00:04:23,110 --> 00:04:25,900 up one directory middleware as validate request 61 00:04:29,800 --> 00:04:36,400 then go and add validate request in and now we can delete everything inside of the function 62 00:04:39,120 --> 00:04:44,340 and in both files we are no longer making use of validation results or request validation error so we 63 00:04:44,340 --> 00:04:48,740 can delete both those imports. 64 00:04:48,820 --> 00:04:51,130 I'll go back or to sign up and do the same thing. 65 00:04:51,200 --> 00:04:55,840 So inside of sign up I'll delete request validation error and the validation result. 66 00:04:55,840 --> 00:05:03,220 Function what's say that looks a good bit better as I mentioned we're going to end up using this middleware 67 00:05:03,220 --> 00:05:05,900 right here in just about every root handler. 68 00:05:05,950 --> 00:05:10,660 That is a post or a put or something similar across all of our different services. 69 00:05:10,660 --> 00:05:13,960 So we are going to be reusing this middleware quite a bit. 70 00:05:14,170 --> 00:05:14,980 This looks good. 71 00:05:14,980 --> 00:05:16,480 I can do a very quick test. 72 00:05:16,840 --> 00:05:20,090 So back over it inside a postman. 73 00:05:20,270 --> 00:05:21,920 I'm going to put in an invalid password. 74 00:05:22,110 --> 00:05:23,490 So an empty password. 75 00:05:23,760 --> 00:05:28,690 Send it off and yep looks like everything is still working as expected. 76 00:05:28,810 --> 00:05:29,950 Get going to flip back over. 77 00:05:29,980 --> 00:05:33,300 Quick pause right here and we'll continue working on sign in in just a moment.