All right.
And now I want to set up validation for job input.
So what am I talking about?
Well, let's take a look at our current controllers.
We have one for get all jobs, create job, get single job, update job and delete job.
And as you're looking at these controllers, which ones require req.body?
So which ones are expecting some kind of value?
Well, we have one for create job and we also have one for update job.
Correct.
So both of these are looking for req.body.
And in there, of course, we'll have the Json value.
So the cool thing is that we can set up, validate job input, essentially our validation and we can
reuse it.
We can use it for both requests.
So let's try it out.
And first we're going to navigate to, you know what, let's go to a server because I want to remove
it.
So we're not going to use validate test.
Of course, that was just for demonstration purposes.
And we also don't need this request.
Just want to keep it nice and clean.
And then when it comes to validation.
I'm not going to leave it for your reference because you do have readmy.
Same deal.
I'm just going to remove it and effectively we right away want to export since we'll use it in our router.
Let's come up with a name.
I'm going to go with validate job input.
Then remember, we have our function with validation errors and it's looking for the array.
And this is what I'm talking about.
Basically, once you have the setup in place, once you understand how express validate their works,
essentially you'll just need to invoke this function and pass in which values you want to validate.
And as far as the reference, well, we will use our schema.
So in a schema we can clearly see what we're expecting.
We'll go one by one and we'll decide, okay, what do we want from this property?
Because of course, in order to create our instance, well, these are the values that we're looking
for.
Correct?
So long story short, let's look for body, since that's where the values are going to be located.
And what's the first property name?
Well, company and effectively, I just want to have some kind of value.
So I'm going to go with not empty.
And then if there is an error, what is going to be my message?
I'm just going to say company is required.
Okay, good.
Now we can copy and paste since it's going to be the same deal for position and job location.
Again, if we take a look at the job model, notice position is simply a string and same goes for the
job location.
So these two are going to be a little bit different.
But when it comes to job, location and position, I'm just checking for empty values.
Now, can you add more validation here?
Of course.
So I'm just showing you the most basic one, but if you want to check for some different things, just
visit the docs for Express Validator and you'll see all the options they provide.
And of course, also please keep in mind that this is just our first validation apart from the test
one.
So as we're building the project, we'll cover some other options as well.
So in here I just want to change it.
So instead of company, this is going to be position and then the last one will be job location.
Now please make sure that the name in here is the same.
So yes, we want to go here in camel case since that's how we're going to be sending from the front
end as well as the thunder client.
Now, in here, of course we can change it.
We can say job location is required and then we have job status and job type.
And if we want to check for enums, we want to use is in.
Now of course we can hardcode those values.
We can hardcode the array.
But remember.
We set up the constants in the previous video.
Correct.
So let's utilize that.
Let's go here with the body.
Then what is the property?
I'm checking.
It's going to be a status.
And then, like I said, we want to chain is in and instead of hard coding the array, well, let's
try to get the job status one over here.
Let me import.
And now, of course, I want to go with object dot values.
So remember, that's the built in method we have in the JavaScript, which is going to get only the
values.
So again, the result is going to be exactly the same.
We'll get the array, but instead of hard coding, well, we're getting it from the constants, which
means that these two will match always.
So that way we don't need to worry about making some kind of typo.
And then as far as the width message, let me think.
Um, I think I'm going to go with invalid status.
Value.
Let's save it.
And then, of course, we want to do the same thing for the job type.
So let me scroll up.
I don't think the auto import is going to work if I'm going to copy and paste.
Then we want to change it.
Then we want to copy in here.
Let's go with job type.
Job type.
And then we're looking for different object and we'll say invalid type value.
Now, once we have the validate job input in place, let's navigate to our router.
So we're looking for routes our job router.
And remember, when it comes to middleware, essentially we can stick it in front of the controller.
Correct.
And again, which two requests are going to look for the values in the Req.body?
Well, the post one and the patch one.
So create job and update job.
And what's really awesome, once I have validate job input in place, of course I can reuse it.
So this is what I'm talking about.
Once you have the entire setup in place, it's not as bad as it looks.
It's just that initial time when you need to build everything after that.
You can set up the validation for multiple requests and it's actually much faster than you think.
And trust me.
It just saves you a bunch of mental energy because your controllers are way slimmer.
So now let's navigate to a job router.
In here, let's first look for the post one.
And we're looking for validate job input.
Yep.
And we want to add the comma.
And also we want to do the same thing, of course, for the patch one.
And now we just need to go to our thunder client and test it out.
Now in the Readme, you will find something very useful and this is effectively a Json code you want
to use in two of those requests.
So of course you can set it up from the scratch, but this is just going to be a nice starting point.
And then of course, in order to test whether the validation works correctly, you can just change them
again just so you can save a little bit of time.
So first, let me navigate, I guess, to the Thunder client.
Let me look for my great job.
Let me send it here empty and in here.
It's basically going to complain that, yep, job model.
All right.
Let me go back right now.
So guess I'm looking for constants or in the job model.
Yep.
This is the thing.
I mean, I just keep forgetting about those JS extensions.
Let me try one more time.
Great job.
Let me send a notice.
Companies required position is required and down the list.
So at the moment, since I'm sending pretty much empty object, well I'll have a bunch of errors.
Correct.
So now let me copy and paste.
So company is going to be coding addict, blah blah blah blah blah.
Now let me send it and check it out.
We successfully created a job and now it's going to be very easy to see whether our validator works.
So, for example, I'm going to go with a remote.
Remember, this is not a valid job type, so let's see whether everything is correct.
And yep, I get back 400 and the value of course is invalid.
Then let's also try to send without the company value.
Let me save it over here.
Yep.
Company is required.
So as you can see, our validator nicely works.
Now let's go back to all jobs.
Let me look for the last one.
Since those are the values I'm going to reuse.
Then we want to first navigate to edit job.
I'm going to change the ID, so I want to make sure that I'm using the correct one.
And again, remember, there is a validator, so if I'm going to send position DevOps, of course I'm
going to get company is required.
So now let's navigate back to create job.
And let's just copy and paste and instead of the.
Position now let me change it to front end.
Let me send.
Yep, everything's correct.
And now position is correct.
And of course you can test it out with more options.
For example, you can send here without job location or you can change the job type value to something
that's invalid.
So this is totally up to you.
I think in my case, I'm going to stop.
Hopefully it's clear that our validate job input works as expected and now we can move on to the next
step.