In this video we're going to take this validation step right here and extract it into a helper middleware.
So inside of my middleware directory I'm going to make a new file and we're going to call this new middleware
validate request Dot T S then inside of here at the very top we're going to add in a couple of imports.
Right away I'm going to import request response and next function from Express.
Now remember these are types.
The only reason that we are importing these is to be able to provide some type annotations for typescript
to understand some information about the middleware.
We're gonna be building in effect it's very similar to the middleware we already put together.
The air handler one remember we had to import request response and next function to describe the type
of these arguments that are going to be flowing into our middleware after that will then import validation
results from Express validator and I'll also get that request validation error from up one directory
errors
who forgot the path request validation error.
That's better.
All right.
After that we can put together our actual middleware.
So going to export a new function called validate request
this thing will be called with Req.
That is a request rez that is a response and next.
That is a next function.
And I got to say this you can see that entire statement.
Here we go.
Now you might be curious why does this look different than validate request or some air handler member
air handler was an error handling middleware that is a very different type of middleware.
EXPRESS distinguishes between an area handling middleware and a normal one on the number of arguments
that the function accepts.
So air handler over here is an area handling middleware.
So it has four arguments.
It's got that extra air one in there.
In this case we're not making a distinct error handling middleware because we're not trying to capture
errors.
Instead we are trying to produce errors.
We want to throw errors.
If the request is not valid.
That's what we're trying to do it inside of your.
So inside the body of this middleware will say take a look at any errors that might have occurred using
validation results on the incoming request if errors is not empty so notice the not right there then
we will go ahead and throw a new request validation error and pass in our array of errors if we get
past this step right here.
That means we want to move on to the next middleware in our chain or potentially our final root handler.
So if we get past that if statement we will simply call next like so.
All right.
That's pretty much it.
So now we can go back over to sign up and sign in and make use of this new middleware.
Let's first go to sign up.
Back side of sign up at the very top.
I'm going to add in an import or validate requests from middleware as validate requests I'll collapse
my sidebar as you can see that whole line and then all we have to do is make sure that we add this thing
in after after we do that validation check.
Remember all the middleware is run in a row and they run sequentially in order.
So we tried to wire up this middleware before we do the actual validation.
Then we're going to run the middleware too early.
We have to do the validation first and then
handle that possible error right after we're going to add and validate request.
Right there OK.
So now we can remove all the aircheck and stuff from the request handler that looks better.
We can save the sign up file then go back over to the assigned in route handler so we're just working
on and we'll do the same thing over here.
So at the very top I will import validate request
up one directory middleware as validate request
then go and add validate request in and now we can delete everything inside of the function
and in both files we are no longer making use of validation results or request validation error so we
can delete both those imports.
I'll go back or to sign up and do the same thing.
So inside of sign up I'll delete request validation error and the validation result.
Function what's say that looks a good bit better as I mentioned we're going to end up using this middleware
right here in just about every root handler.
That is a post or a put or something similar across all of our different services.
So we are going to be reusing this middleware quite a bit.
This looks good.
I can do a very quick test.
So back over it inside a postman.
I'm going to put in an invalid password.
So an empty password.
Send it off and yep looks like everything is still working as expected.
Get going to flip back over.
Quick pause right here and we'll continue working on sign in in just a moment.