And once we're done with our initial setup, your first question is probably, Hey, buddy, are you
okay?
You told us that once we set up the validation layer, our controllers are going to be leaner and instead
I just have this giant massive code sitting on top of it.
It's definitely not a easier or a more maintainable way.
And you're correct.
But keep in mind that we can move them to a separate file.
So essentially we can set up this as a middleware.
And again, the cool thing is that we can take it from project to project.
So essentially we will reuse this code in this project in all of the requests.
And if you want, you can do the same thing in your other projects as well.
So let's get to work.
We first want to create a validation middleware.
JS In the middleware folder, so we already have one for errors.
And now let's do the same thing over here.
So let's come up with the name.
In my case, validation.
Middleware.
For some reason, middleware spelling is just horrible for me.
I don't know why.
Then let's grab the same imports.
So again, we're obviously not going to keep this in server.
So I just want to scroll up.
I want to grab these ones and essentially we can remove it.
Okay, good.
Then we want to grab our bad request.
Remember, that's the.
Our class.
We set up a few videos ago since again, we're not going to hard code.
We will actually use our class and effectively in here we want to create two things.
We want to create, validate test where we'll pass in the values we want to validate.
And second one is going to be a function.
Which takes care of the error response.
And I guess let's start with the function that returns the errors.
We want to come up with the name.
In my case, I'm going to go with with.
Validation errors.
And in here we're going to be looking for that array.
So this one over here.
And in our case, of course, we only have the body.
But normally there's going to be more values and I'm going to give it a validate.
Values name.
So that's my parameter.
Now, what do I want to return from this function?
Well, since there's going to be two things, there's going to be validate values, just like we have
right now, and also the middleware in Express if we want to group them together, we essentially set
up an array.
Again, this is coming from Express.
If you have multiple middlewares, effectively you just group them in such manner.
So this is not express validator syntax.
We're just returning from this function in such a way.
So first we want to pass in validate values again, something we're going to set up in a second.
And the second one is going to be our error response.
So let's navigate back over here and notice this function effectively.
We can cut it out because of course we will pass here the validation errors function.
So let me copy and paste.
So this does not change still from this validate values.
We'll have access to the errors, but what we want to change over here is how we handle the response.
Simply I want to go with throw new.
And we're going to go with bad request error and we'll pass in the error messages.
So essentially, this property over here and what's missing is the validate test.
So pretty much for every controller, we are going to invoke this function.
And we'll pass in the values we want to validate.
And then what we're going to get back.
Is two of these middlewares sitting here.
And of course we'll set it up in the controller.
So what am I talking about over here?
Well, since at the moment we only have this for test, we're just going to go with export, then const,
then validate and let's call it test.
And as far as the name later we'll use validate, login, validate, register and hopefully you see
where I'm going with this.
Then we want to invoke with validate errors.
And in here, this is where I want to set up again the angle brackets and we want to set up the values.
So essentially I want to cut it out so the logic doesn't change.
I'm just setting it up over here.
As a middleware.
So pretty much for every controller, this is what we're going to create right now.
So this code we won't touch anymore.
Again, this is something we can reuse from project to project, but then for every controller, I'm
going to say, Hey, I'm looking for such and such value and the length should be this, or some other
property should be that.
And as a result, every time I have a new controller, I can quickly and easily set up a validation
for all of the values that are coming in.
Now in here, let's change it around.
I'm going to say is length is equal to min three.
So that's going to be the minimum and Max is going to be 50.
Again, this is just for testing purposes and we're going to go with name must be let's say.
Between.
And three.
50.
And let's write characters.
Long.
Then let's remove this And.
We get a bunch of methods that we can chain again over here.
For example, we can also use trim, which removes whitespace.
So super, super useful library.
Now we want to go back to a server and instead of setting up everything manually, we want to import.
Now, please keep in mind that of course this is going to be temporary.
I'm going to remove all of this at the end of the video, but for now we want to go validate.
And best.
Let's scroll down and notice where we have our request.
We want to reference it.
That's it.
That's all we want to do over here.
And if I go back to my new request and if I send over here.
Everything should be fine.
But it complains that.
Yep.
Validation error because probably I forgot to add JS over here.
Nope.
That one is over here.
Cannot find module custom errors.
Oh, over here.
Yep.
My bad.
Notice the JS is missing again.
Something I already warned you about that I'll probably forget that.
Let me try one more time.
Let's send notice in here I have.
Hello, Peter.
Because this past both of my checks.
But if I'm going to send an empty one, check it out.
I'll have name is required.
Name must be three and 50 long.
So notice essentially we're getting the list of all the errors.
And if, let's say I'm going to pass in.
First two characters.
Yeah, I passed the first check.
It's not empty, but I still have this one in place, so hopefully it's clear again.
We set up this middleware and essentially for every request, we're just going to create a new instance.
And in here we'll just set up our conditions.
So this one we won't touch.
That's it.
It works.
And pretty much for every request, we'll right away set up what we're looking for and what are the
rules.
And again, as a result, our controllers are going to be very lean because if the request gets all
the way to controller, we already know that it passed all of the tests that we set up over here in
the validation middleware.