1 00:00:01,130 --> 00:00:05,240 As I mentioned at the end of last video we now apparently are doing some actual validation attempts 2 00:00:05,240 --> 00:00:09,790 right now but we are not actually communicating the results of that validation back to the user. 3 00:00:09,800 --> 00:00:13,900 So let me show you how we're going to do that at the very top of this file. 4 00:00:13,910 --> 00:00:16,520 I need to find where he imported express validator. 5 00:00:16,520 --> 00:00:22,910 In addition to importing body we're going to import a second function called validation results. 6 00:00:22,940 --> 00:00:28,880 This si function that we're going to run on the incoming request inside of our request handler whenever 7 00:00:28,880 --> 00:00:30,260 we run validation results. 8 00:00:30,260 --> 00:00:35,840 It's going to inspect the request and pull out any information it was appended to the request during 9 00:00:35,840 --> 00:00:37,300 this validation step. 10 00:00:37,310 --> 00:00:41,510 So in other words when these two steps right here do validation on the incoming request that there's 11 00:00:41,510 --> 00:00:46,780 anything wrong with the email or the password these functions are going to append some properties onto 12 00:00:46,790 --> 00:00:48,180 that request object. 13 00:00:48,290 --> 00:00:53,760 So we use the validation result function to pull that validation information off. 14 00:00:53,790 --> 00:00:57,510 So here's how we actually use it down inside of our handler. 15 00:00:57,660 --> 00:01:00,040 Right above a re pull off email and password. 16 00:01:00,150 --> 00:01:09,550 We're going to add in fonts to Ayres is validation results on request Well then check to see if any 17 00:01:09,550 --> 00:01:14,440 errors are actually present at this point time we just pulled off an object that might contain some 18 00:01:14,440 --> 00:01:15,160 errors. 19 00:01:15,190 --> 00:01:19,150 So we need to take a look at that object and decide whether or not something actually went wrong with 20 00:01:19,150 --> 00:01:20,350 the validation attempt. 21 00:01:20,560 --> 00:01:30,650 We'll say if not errors dot is empty so if there is an error or in other words if the errors object 22 00:01:30,650 --> 00:01:36,020 is not empty then we're going to enter this if statement and we need to handle the error that just occurred 23 00:01:36,050 --> 00:01:41,710 in some way so inside of here this is probably a good location to somehow take the errors that were 24 00:01:41,710 --> 00:01:47,120 produced and then send them back to the user early before executing anything else inside this function. 25 00:01:47,560 --> 00:01:58,830 So we can run something inside of here like return rez dot status of 400 not cent and we'll just send 26 00:01:58,830 --> 00:02:04,980 back the errors array object or of the errors object we're going to call a little method on it really 27 00:02:04,980 --> 00:02:10,290 quickly called array that's going to take the errors object which currently is an object and has some 28 00:02:10,290 --> 00:02:16,080 methods attached to it and turn it into an array of errors that can be safely sent down be sent down 29 00:02:16,110 --> 00:02:16,950 as Jace on data 30 00:02:20,140 --> 00:02:24,630 so in total we've now said Take a look at that request objects off any errors that might have occurred. 31 00:02:24,670 --> 00:02:30,820 See if any errors actually exist and if they do return early and send back a error response to the person 32 00:02:30,880 --> 00:02:38,010 who gave us or sent us this request so if we make it past that if statement and we get down here now 33 00:02:38,010 --> 00:02:44,540 we don't have to do this kind of silly check right there anymore we could do a adding some logic right 34 00:02:44,540 --> 00:02:46,660 here that tries to create a user. 35 00:02:46,700 --> 00:02:52,270 So I'll put in a console log right now that says something like reading a user and then again for right 36 00:02:52,270 --> 00:02:58,150 now very temporarily I'll do a resort send of an empty object just we can actually test this thing out 37 00:02:59,870 --> 00:03:00,220 OK. 38 00:03:00,290 --> 00:03:01,540 So let's save this. 39 00:03:01,580 --> 00:03:03,000 We're gonna open up man. 40 00:03:03,020 --> 00:03:08,240 Remember that API client and we're going to attempt to send a post request to this sign up root handler. 41 00:03:08,330 --> 00:03:13,370 We're just gonna make sure that this validation stuff is actually working I'm going to save the file 42 00:03:13,520 --> 00:03:17,540 and go back over to my terminal and just make sure that I got the updates over on scaffold. 43 00:03:17,540 --> 00:03:17,750 Yep. 44 00:03:17,780 --> 00:03:18,680 Looks good. 45 00:03:20,310 --> 00:03:27,750 I'll then open up postman and then once postman is open we'll make a post request that point that we 46 00:03:27,750 --> 00:03:28,440 just put together. 47 00:03:30,450 --> 00:03:33,200 So I'm going to make sure I'm making a post request. 48 00:03:33,200 --> 00:03:45,630 I want to make that request to ticketing dot Dev flash API users sign up well then go over to headers. 49 00:03:45,630 --> 00:03:49,140 I'll make sure I still have a content type of application slash Jason. 50 00:03:49,310 --> 00:03:50,500 I'll then go to body. 51 00:03:50,790 --> 00:03:52,720 I'll make sure I've got Ross elected. 52 00:03:52,950 --> 00:03:58,230 And remember we are expecting to receive an email and a password for this first test. 53 00:03:58,250 --> 00:04:03,170 I'm gonna put in something that is definitely not a valid email but just a plain string like so I'm 54 00:04:03,220 --> 00:04:10,150 to also enter a password that it's just one character I'll then make sure I've got Jason selected and 55 00:04:10,150 --> 00:04:12,000 I'm going to attempt to send this off. 56 00:04:12,970 --> 00:04:18,730 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 57 00:04:18,730 --> 00:04:24,490 says down here something like h TTP error or something like that which is entirely possible then pauses 58 00:04:24,490 --> 00:04:28,090 video right now and check out a text lecture right after this one. 59 00:04:28,120 --> 00:04:33,310 So again if you get a distinct air down here around H TTP s or something like that go check the text 60 00:04:33,340 --> 00:04:38,810 in the next lecture where relevant however if I take a look at the response you got back. 61 00:04:38,890 --> 00:04:43,810 You'll notice I got 400 bad request and it has some information about exactly what just went wrong with 62 00:04:43,810 --> 00:04:44,740 the request. 63 00:04:44,980 --> 00:04:48,460 So I've got some error message around the email. 64 00:04:48,460 --> 00:04:54,900 I've also got an error message around the password as well if I actually enter in a valid email so I'll 65 00:04:54,900 --> 00:05:03,420 put in about my favorite which is test at test dot com and a password of password and set that off. 66 00:05:03,420 --> 00:05:08,130 Now I got back an empty object which means I got past that validation step and so if I go back with 67 00:05:08,130 --> 00:05:12,040 my terminal I will see that console that says creating a user OK. 68 00:05:12,100 --> 00:05:13,040 So this definitely looks good. 69 00:05:13,060 --> 00:05:15,470 We've definitely got some validation site here. 70 00:05:15,640 --> 00:05:19,960 Now at this point you might be thinking to yourself hey Steven this is of course about micro services 71 00:05:19,960 --> 00:05:21,230 not validation. 72 00:05:21,280 --> 00:05:22,510 I know I know. 73 00:05:22,510 --> 00:05:26,710 Unfortunately we're gonna go through a lot of steps like this where I just tell you and give you a reminder 74 00:05:26,710 --> 00:05:28,600 of how some stuff and express works. 75 00:05:28,600 --> 00:05:33,490 The reason for that is that all the code you see right here where you went to abstract out and write 76 00:05:33,490 --> 00:05:35,940 in a well not all of this but a decent amount of it. 77 00:05:36,040 --> 00:05:41,080 We're going to abstract a lot of this out and rewrite it in a very reusable fashion and then reuse it 78 00:05:41,080 --> 00:05:43,740 between multiple different services we create. 79 00:05:43,870 --> 00:05:48,220 I don't want to just jump directly to that reuse step because if we do so a this stuff is gonna get 80 00:05:48,220 --> 00:05:50,170 really confusing really quickly. 81 00:05:50,170 --> 00:05:56,230 So I want you to very directly and right now immediately understand how this validation stuff really 82 00:05:56,230 --> 00:05:57,160 truly works. 83 00:05:57,160 --> 00:06:03,190 Because again we're going to start to rewrite this stuff very quickly and in a much more reusable and 84 00:06:03,450 --> 00:06:07,040 a little bit more challenging to understand fashion. 85 00:06:07,070 --> 00:06:11,890 So again I apologize if a lot of this stuff is kind of repetitive for you but I do feel like we've got 86 00:06:11,890 --> 00:06:15,430 to get a quick review before we just jump to that reusable version. 87 00:06:15,470 --> 00:06:15,670 Okay. 88 00:06:15,710 --> 00:06:18,130 So quick pause right here and continue in just a minute.