As I mentioned at the end of last video we now apparently are doing some actual validation attempts
right now but we are not actually communicating the results of that validation back to the user.
So let me show you how we're going to do that at the very top of this file.
I need to find where he imported express validator.
In addition to importing body we're going to import a second function called validation results.
This si function that we're going to run on the incoming request inside of our request handler whenever
we run validation results.
It's going to inspect the request and pull out any information it was appended to the request during
this validation step.
So in other words when these two steps right here do validation on the incoming request that there's
anything wrong with the email or the password these functions are going to append some properties onto
that request object.
So we use the validation result function to pull that validation information off.
So here's how we actually use it down inside of our handler.
Right above a re pull off email and password.
We're going to add in fonts to Ayres is validation results on request Well then check to see if any
errors are actually present at this point time we just pulled off an object that might contain some
errors.
So we need to take a look at that object and decide whether or not something actually went wrong with
the validation attempt.
We'll say if not errors dot is empty so if there is an error or in other words if the errors object
is not empty then we're going to enter this if statement and we need to handle the error that just occurred
in some way so inside of here this is probably a good location to somehow take the errors that were
produced and then send them back to the user early before executing anything else inside this function.
So we can run something inside of here like return rez dot status of 400 not cent and we'll just send
back the errors array object or of the errors object we're going to call a little method on it really
quickly called array that's going to take the errors object which currently is an object and has some
methods attached to it and turn it into an array of errors that can be safely sent down be sent down
as Jace on data
so in total we've now said Take a look at that request objects off any errors that might have occurred.
See if any errors actually exist and if they do return early and send back a error response to the person
who gave us or sent us this request so if we make it past that if statement and we get down here now
we don't have to do this kind of silly check right there anymore we could do a adding some logic right
here that tries to create a user.
So I'll put in a console log right now that says something like reading a user and then again for right
now very temporarily I'll do a resort send of an empty object just we can actually test this thing out
OK.
So let's save this.
We're gonna open up man.
Remember that API client and we're going to attempt to send a post request to this sign up root handler.
We're just gonna make sure that this validation stuff is actually working I'm going to save the file
and go back over to my terminal and just make sure that I got the updates over on scaffold.
Yep.
Looks good.
I'll then open up postman and then once postman is open we'll make a post request that point that we
just put together.
So I'm going to make sure I'm making a post request.
I want to make that request to ticketing dot Dev flash API users sign up well then go over to headers.
I'll make sure I still have a content type of application slash Jason.
I'll then go to body.
I'll make sure I've got Ross elected.
And remember we are expecting to receive an email and a password for this first test.
I'm gonna put in something that is definitely not a valid email but just a plain string like so I'm
to also enter a password that it's just one character I'll then make sure I've got Jason selected and
I'm going to attempt to send this off.
Now when we send this I'll take a look at the response and just you know if you get an error or if it
says down here something like h TTP error or something like that which is entirely possible then pauses
video right now and check out a text lecture right after this one.
So again if you get a distinct air down here around H TTP s or something like that go check the text
in the next lecture where relevant however if I take a look at the response you got back.
You'll notice I got 400 bad request and it has some information about exactly what just went wrong with
the request.
So I've got some error message around the email.
I've also got an error message around the password as well if I actually enter in a valid email so I'll
put in about my favorite which is test at test dot com and a password of password and set that off.
Now I got back an empty object which means I got past that validation step and so if I go back with
my terminal I will see that console that says creating a user OK.
So this definitely looks good.
We've definitely got some validation site here.
Now at this point you might be thinking to yourself hey Steven this is of course about micro services
not validation.
I know I know.
Unfortunately we're gonna go through a lot of steps like this where I just tell you and give you a reminder
of how some stuff and express works.
The reason for that is that all the code you see right here where you went to abstract out and write
in a well not all of this but a decent amount of it.
We're going to abstract a lot of this out and rewrite it in a very reusable fashion and then reuse it
between multiple different services we create.
I don't want to just jump directly to that reuse step because if we do so a this stuff is gonna get
really confusing really quickly.
So I want you to very directly and right now immediately understand how this validation stuff really
truly works.
Because again we're going to start to rewrite this stuff very quickly and in a much more reusable and
a little bit more challenging to understand fashion.
So again I apologize if a lot of this stuff is kind of repetitive for you but I do feel like we've got
to get a quick review before we just jump to that reusable version.
Okay.
So quick pause right here and continue in just a minute.