All right, our user model is almost almost almost ready to go.
The only thing is missing is the validation for email and like already mentioned in previous video,
if you watched the other project, we want you to know what expression we use match property and we
passed in the reject.
However, there is another way how we can do that.
You see, in the Mongoose, we can set up a validate property with two more properties.
So essentially it's an object with two more properties, a validator property where we set up the function,
which is going to be looking for the field and then whatever we return from this function, meaning
either true or false.
That means that either we validate or don't validate the particular property and then we have a message
if, for example, there is an error.
So at the moment in the user, what do we have, what we have?
Please provide email.
But once we set up a validator, we're going to say please provide a valid email of what makes sense.
And if you want to find out more info about this one.
Just go to your -- Knox.
Search for custom validators and you'll see this example.
I said Revalidate validator.
That's the function.
Please keep in mind that you're not invoking this function over here.
You're just setting it up as a reference.
And we also have a message because this function will be invoked when you're trying to create that user
or job or whatever.
And technically, yes, we can set up our own function, but then it's kind of the same like we were
passing in that region.
So another option can do we can get a validator package, which essentially is a very cool library that
has all of those validations already in place.
So let's start with this.
I'm just going to go to a user and I'll add this object first.
So let me go to my email and it's my preference to set it up above unique.
Don't ask me why just a preference and I'm going to go with validate here.
I have the object and then we're looking for validator.
That's going to be my function.
And then the second one will be the message.
And as far as the message, like I said, please provide provide a valid email and then we want out
the column so we don't have any syntax bugs.
And now let's navigate and take a look at the package.
So this is going to be a link and we'll have to install the library at some point as well.
So let me navigate here or these are the docs where basically you can find all of the validators that
they provide.
And then in our case, we're looking for email.
Now, why would you want to use the extension package instead of setting up the function yourself?
Well, this is going to be more battle tested.
It also gives you a bunch of other options as well.
So those would be two reasons why.
Going with external package kind of makes sense.
So we're looking for this is email and what do we need to do?
Well, we need to install first, then we need to import in your address and then we're just going to
go with validator and email and we'll set it here where we have the validator.
So let me stop server and I'm going to go with my command.
So when I install the package, then I'll restart my entire project or npm start.
Then we want to go to a user right below the Mongoose.
So we're going to go with validator and that is going to be coming from the validator.
Then let's keep on scrolling and we're looking for validator.
And then is email.
That's the function.
Again, we pass in the reference.
We don't invoke the functionality here.
The function will be invoked and will automatically get access to whatever is passed in the email when
we're trying to set up the user.
And now that we are finally done with our initial setup for the user model, we can start setting up
our controller and actually start creating the users.