And as far as the validation layer, we're going to use package by the name of Express Validator.
Just keep in mind, yes, there are other packages as well.
For example, you can use Joy, but in my opinion, Express Validator is super, super straightforward
and therefore we're going to stick with this one and we'll start with another test route.
So essentially, I just want you to understand the main concepts in isolation.
And once that's the case, then of course we'll apply it to rest of the requests.
So first of all, let's navigate to server and let's scroll up.
So at the moment I have this post route to the home page where essentially we're just logging req.body.
I believe we use this one to test out the express Json.
Let's refactor it a little bit.
I'm going to change the URL.
I'm going to go with API version one because we already have the URL variable in the Thunder client.
Correct.
And I'm just going to go with the test.
That's going to be my complete URL.
For now, let's just grab the name since I know that I'm going to send that.
So I'm going to go with name and I want to destructure it from req dot body.
Then as far as the response, simply let's send back Res.json and instead of all this, we're just going
to go with template string and we're going to go with Hello.
And let's access the name.
We're going to navigate to Thunder Client.
And let's set up a new one again.
This is just going to be a test one.
So we can access the URL?
Of course.
Let's close the curlies.
Then we're going to go with Test Body.
Then let's provide name.
And I'm going to go with Peter because why not?
Let's send it.
And it complains that we cannot find the root.
And this is a good use case.
So we go here with get an API version one.
So why we have the error?
Well, because this is a post road.
And again, this is a good demonstration for the Morgan.
You're right away.
See that?
Hey, listen, you're using the wrong method.
So let's go here with post.
Let's send it.
And now, of course I have.
Hello, Peter.
Now, it's not going to be surprised if I go with Empty.
String.
Then of course, in my response I'll have this same result and this is what we want to fix with Express
Validator.
I want to set up a validation.
If the name is not present, I actually want to send back 404.
I don't want to send back 200.
And again, just bear with me.
We'll set up everything in a server and then once we have everything.
In place, then we'll move it to a separate middleware and again apply to rest of the routes.
First of all, let's import.
We're looking for two things.
We want to grab the body and the validation.
Result.
And both of these things are coming from.
The Express validator package.
Now, as far as the body, remember, so there are multiple places where we can check for values.
For example, params.
And therefore, since I know that I'm going to be checking for values in a body, I want to get the.
Body export from express validator.
And as far as the validation result, you'll see it in action in second.
So let's scroll down.
I think I can close the console for now.
And here in between the controller and the URL, we want to set up express Validator as our middleware.
So Express Validator is going to be sitting on top of our controller and if.
The value is not present or it doesn't match our condition.
Then we'll right away spit back the 400 error.
And in order to set it up, we want to go here with the brackets.
Then we need to invoke body.
So essentially this is where we're looking for the value and the way it works.
We just pass here the string.
Now, of course, this string matches whatever we're expecting in the req.body.
So since I'm expecting that there's going to be a name, that's why I type here, string of name.
Later, when we check for lastname, when we check for job status and all that, of course we'll use
the same string value and then we can nicely chain the methods that are provided and one of them is
not empty.
So essentially I'm saying, hey, listen.
There has to be some kind of value in here.
And what's also really cool, we can chain for specific errors, specific messages.
So what am I talking about?
Well, we can set up one main one, but also, for example, for not empty, I can say, hey, listen,
name is required, but then when we'll add one to check the length, I can say, you know what the length
should be between, let's say three and 50 and you'll see it in action in a second.
So first, like I said, we can just keep chaining these methods.
And the one we want to use is with message.
So essentially this is going to be the error message we send back.
So here we're going to go with name is required.
And once we have this code in place, we actually want to add a function which will check for those
errors.
So essentially, yes, we'll have to middleware functions.
So one is going to be where we'll set up our roles.
And the second one is where we'll check for errors.
If they exist.
And if that's the case, then of course we'll send back the 404 or whatever error we want to send back.
So in this middleware function, we want to access three things req res and the next.
So when it comes to next, if everything is successful, we want to pass it to the next middleware.
Otherwise, the request is going to stop over here in this middleware function.
So next is a special one.
And yes, of course, in this case, the next middleware technically is our controller, so that's the
end of the chain.
But normally if you set up next, that just passes on to the next middleware and that's something always
you want to add because otherwise, again, this request is just going to stop over here in this function.
Now, of course, we will invoke next based on the condition whether we have errors.
But in general, try not to forget about the next and essentially.
We want to set up a variable.
I'm going to go over here errors.
And this is where we want to invoke validation result and we're looking for the request.
So essentially we pass in the incoming request.
And since we already have this logic in place, it checks for that name in the body.
Hopefully that is clear.
And now, of course, I just want to log it just so we can see what's happening.
And then we're going to discuss what methods we can use.
So first, let's navigate to the new request.
Let's send it one more time.
So at the moment, yeah, the response is still the same.
What I'm interested is the console.
So notice over here I have this errors property and in here each error is represented with this object.
Now what we're looking for is the message.
So that's the one that we're going to send back.
And what's really awesome, this instance has a few methods that we can right away use.
For example, one of them is is an empty.
So invoke it over here.
We send it one more time and check it out.
Basically, this is false.
Now, what does that mean?
Well, it means that we have the errors.
So if is empty is true, then we're good to go.
Then we can pass it on to the next middleware in our case controller.
But if it's false, if it's not empty, that means we have errors.
And of course, in that case we want to send back the error response.
So let's set up the condition.
I'll say if is empty is false, basically if we have the errors.
Let's send back some kind of error response.
Now, in my case, I'm going to hardcode the 400, but in the upcoming videos, when we set up the proper
middleware, we'll actually invoke one of the classes we set up previously.
Then we can also.
Set up messages.
So let me go here with some kind of variable error messages.
And let me just make sure that the spelling is correct and that one is equal to errors.
Again, this instance has this array method, which essentially turns it into an array and we can map
over it.
And what I want is, again, just to return that message property.
Now, if you want to have different setup, of course you can change it around.
But in my case, I'm just looking for that message property.
So this one over here and again, this is built in this right away just turns it into an array and then
we can iterate over and then when it comes to the response.
Let's go here with return res dot.
Again, we're hardcoding for now.
We'll fix that in the middleware.
Then let's go json.
And since potentially I can have multiple error messages, I'm going to go with errors property and
I'll set it equal to my error messages.
So now if we go over here and if I send this one notice, I'll get back the errors and it's going to
say name is required.
And keep in mind, of course we can add more conditions.
So let's say I add here, Peter, but I can go back over here where I have the angle brackets and I
can just keep chaining my conditions.
So for example, I can go with, you know, what is length should be, I don't know, let me go with
something ridiculous and I'm going to go with 50.
So we'll pass here the object and we'll say min is equal to 50.
Again, something will change in a second.
But just for testing purposes.
And then remember, we can still change the message.
So I'm going to copy and paste.
Now we have with message and in here we'll just write something else.
So I'll say name must be at least.
And 50.
And I'm not going to type characters because we'll set it up later and let's navigate back to the request.
So technically we have the beta, but now we have a different error.
And with this in place we have successfully added the express validator middleware to our test request.