We've now conquered our four most common ways of manipulating data inside of our Mongar database create
read update and destroy.
So now that we've covered these basics we can start to explore a lot more of what mongoose has to offer.
And then after we're done with a broad overview we can work on our next project.
I want to take a second to reconsider or remember what we're working with when we touched Mungo's or
mongooses excuse me when we work with Mongoose we are working with the interface to Mongo mongooses
all about just giving us a nicer API something that has advanced functionality to work with Mongo all
of the operations that we've worked with so far.
Like specifically reading writing updating and destroying data out of our Mongo database is implemented
by mongoose but we could have just as easily reached directly to Mongo and essentially had basically
the same experience.
So what I mean by that is everything we've done so far with Mongoose is kind of just a very nice little
wrapper around lango.
But essentially it's very close to us just reaching out to Mongo directly where we're going to start
to talk about now is a lot more of the advanced features around long views that offer us great awesome
functionality around Mongo that is not implemented in Lango at all by default.
So we're going to start working with features that are special and implemented by mongooses specifically
to give us and way better experience when working with lango.
The first feature that we're going to work with that's going to make us giddy with joy about how much
nicer is in working directly with Mongo is validation validation is the process of evaluating whether
or not the properties of a record are safe to save to our database.
I want to add a validation rule to our user model and say that a user must have a name longer than two
characters in order to be considered valid.
So in this case we would consider Joe to be valid because it's three characters a name of a.
Or maybe better than a would be Apple would be invalid because it's only two and Amanda would be valid
because it's a lot longer than two characters as well.
Once the record or a model is marked as being invalid inside of mongoose it should not be able to be
saved to our database.
The purpose of validation is to make sure that we don't insert junk data into our database.
We don't want users to be submitting or signing up with usernames that don't exist or really short names
or this would even be a great way to prevent profanity inside of our user names as well.
This validation that we're about to add in is solely implemented by mongoose as we're using it at least
as the validation level that we're using is 100 percent mongoose.
Mongo does have support for validation but we are not directly working with it with the validation that
we're about to use on the longest side.
OK.
So let's get started on this.
Let's walk through an example of adding in some validation on our name property of our users to learn
more about validation on our users.
We're going to take the same approach as we have with all of our other operations.
I'm going to create a new test file and inside of there will write a test to kind of verify that validation
is working the way that we would expect it to.
With mongoose.
So I'm going to start off by making a new test file inside of my test directory and I'm going to call
this one validation test J.S. and then inside of here we can place a little bit of our boiler plate
that we've come to love so much.
So we will require a surge at the top.
We will grab our user model
and then we'll place our described block and I'm going to give this description of validating records.
OK.
So now inside if you're we're not going to make a book for each and declared Joe ahead of time remember
the whole purpose of validation is to say that we should not be able to insert a record to our database
if the record isn't valid.
So the entire purpose of our before each up to this point has been to make a record and then save it
to our database just to avoid the repetitiveness of all that stuff.
In this case I want to specifically create records and then validate them without saving them to the
database.
So I'm not going to use that before each year.
I will add my first it and I say inside of your requires a user name so I expect that we should be able
to write a test where we can create a new user validate it somehow and see an error message.
If we do not assign a user name to it.
So that's our goal.
Let's start figuring out how are we going to implement this in the next section.