We just wrote out to test to handle validation around our request handler.
So now we have to do the actual implementation.
Remember how we are doing validation of incoming requests at the very top.
We are going to import that library specifically the body function from Express validator we use this
body function to validate certain properties on the incoming request body.
So we're gonna wire this thing up as a middleware.
I'm gonna put the R validation logic right after require off if a user is not authenticated.
I don't really want to waste any time trying to validate the body.
So we're gonna make sure we put these things right after that if we put it before then we would do the
validation and then check to see if the user is authenticated.
So let's try to essentially fail early and get out as soon as possible.
So now inside this array We'll list out all the different properties we want to validate by using that
body.
Function so going to call body the first property I want to try to validate is title and in this case
I'm gonna make sure that it is not empty.
So we call not is empty if it is I will return a customer error message of title is required the not
is empty check right here is going to handle both a case in which a title is not provided and the case
in which the title is provided but it is an empty string.
Now remember just doing this validation check right here doesn't really cause any errors to be thrown
or a response to be sent back or anything like that.
Instead it's going to set an error on the incoming request.
It is then up to you and eye to inspect that request and somehow respond to it if required.
We already encoded or handled all that logic back inside of our common module.
Specifically in that validate request middleware.
So again we take a look at that request we see if there were any errors if there were we then throw
that request validation error so we do have to wire up that validate request middleware.
Right after that validation attempt to make sure that all occurs so I once again find my common module
import and I will add in validate.
Request and then after the validation attempts I will add in validate.
Request Okay so that should be it for the title.
Let's save this go back over George terminal and it looks like that test is now passing.
Not bad.
Now we're gonna repeat that same process over or the price so as a second element inside this array.
I'm not going to add anybody I want to look at price and then with this thing when we ever we enter
in a price I'm going to assume that we are working with the U.S. dollars U.S. dollars can have both
dollar values and sense or in other words a decimal.
So I really want to check and make sure that this is some kind of decimal value.
And as we saw in our test we wrote out.
We also probably want to validate and make sure that this thing is greater than zero as well.
So to do so we're going to write in a check right here of is Lote
afloat is a number that has a decimal component.
So this is gonna make sure that we are providing a number that has a decimal or possibly has a decimal
as the argument to this.
We can provide a balance for this number so we can put in a greater than or less than check to put in
a greater than check.
We'll write in GTD standing for greater than we want to make sure.
Again this thing is greater than 0 so then if we fail that check.
Let's put it in once again a customer error message we'll say price must be greater than 0.
Let's say this go back over and it looks like both of our tests around validation are passing fantastic
well.
That really just leaves us with the last case right here.
We have to actually create a ticket with some valid inputs and make sure it's somehow gets saved the
database or something like that.
Let's start to work on that final test in just a moment.