All right.
And up next, let's talk about validation layer.
And before I show you why we need one, let me again repeat the same speech I was giving for custom
errors.
Yes, It's going to be a little bit painful to set up the validation layer.
But keep in mind one thing.
Not only you'll be able to use it in this project, and as a side note, once we have the main structure
in place as far as just validating single request, it's going to be much faster.
But also you can reuse the same functionality in all your upcoming projects.
Just please keep that in mind.
Now, as far as the example, let me just navigate right now to my Thunder client.
And collections then recording and I want to go with Create Job.
And at the moment I'm sending company and the position and we did cover that technically if we add extra
property, if it's not in our schema, we are good to go.
Correct.
But what do you think happens if we send pretty much nothing if we send empty Json?
Well, actually we create a new instance.
Now of course, the company is missing the position.
We only have the default values.
But you have to agree that this is not the behavior we are expecting.
And if you're already familiar with the mongoose, you know that.
Technically, I can go to my models and I can set up quite extensive validation over here.
But a better approach is to set up a separate layer because again, we can reuse it from project to
project and it's going to be easier for us to catch those errors before the request even gets into the
controller.
So one way would be again, setting up over here all the validation.
Another way how we could handle technically validation is in the controllers directly.
But here's the issue.
The more validation you add over here in the controller, just like we saw with company and position,
I believe we used it in the update job, if I'm not mistaken.
The more code you will have and it's going to be harder to maintain the controllers.
Our goal is to have pretty much controllers as slim as possible.
And that's why we'll set up a separate layer which checks for those values before they even get to the
controllers.
And that way again, we can reuse it.
Our controllers are going to be way slimmer, so we won't have to add that much logic in here.
And also we won't have to implement any kind of validation here in the schema.
And once we're clear why we need validation layer in the first place.
Now let's roll up our sleeves and get to work.