1 00:00:01,180 --> 00:00:04,660 We just wrote out to test to handle validation around our request handler. 2 00:00:04,660 --> 00:00:07,070 So now we have to do the actual implementation. 3 00:00:07,180 --> 00:00:11,320 Remember how we are doing validation of incoming requests at the very top. 4 00:00:11,320 --> 00:00:20,910 We are going to import that library specifically the body function from Express validator we use this 5 00:00:20,910 --> 00:00:25,440 body function to validate certain properties on the incoming request body. 6 00:00:25,500 --> 00:00:27,550 So we're gonna wire this thing up as a middleware. 7 00:00:27,720 --> 00:00:32,700 I'm gonna put the R validation logic right after require off if a user is not authenticated. 8 00:00:32,700 --> 00:00:35,810 I don't really want to waste any time trying to validate the body. 9 00:00:36,060 --> 00:00:40,260 So we're gonna make sure we put these things right after that if we put it before then we would do the 10 00:00:40,320 --> 00:00:44,010 validation and then check to see if the user is authenticated. 11 00:00:44,010 --> 00:00:48,890 So let's try to essentially fail early and get out as soon as possible. 12 00:00:48,910 --> 00:00:52,480 So now inside this array We'll list out all the different properties we want to validate by using that 13 00:00:52,480 --> 00:00:52,940 body. 14 00:00:52,940 --> 00:01:00,060 Function so going to call body the first property I want to try to validate is title and in this case 15 00:01:00,090 --> 00:01:04,590 I'm gonna make sure that it is not empty. 16 00:01:04,680 --> 00:01:16,210 So we call not is empty if it is I will return a customer error message of title is required the not 17 00:01:16,210 --> 00:01:21,940 is empty check right here is going to handle both a case in which a title is not provided and the case 18 00:01:21,940 --> 00:01:26,290 in which the title is provided but it is an empty string. 19 00:01:26,320 --> 00:01:31,390 Now remember just doing this validation check right here doesn't really cause any errors to be thrown 20 00:01:31,400 --> 00:01:34,010 or a response to be sent back or anything like that. 21 00:01:34,030 --> 00:01:37,880 Instead it's going to set an error on the incoming request. 22 00:01:38,080 --> 00:01:43,660 It is then up to you and eye to inspect that request and somehow respond to it if required. 23 00:01:43,660 --> 00:01:47,380 We already encoded or handled all that logic back inside of our common module. 24 00:01:47,380 --> 00:01:50,440 Specifically in that validate request middleware. 25 00:01:50,440 --> 00:01:54,640 So again we take a look at that request we see if there were any errors if there were we then throw 26 00:01:54,640 --> 00:02:00,130 that request validation error so we do have to wire up that validate request middleware. 27 00:02:00,390 --> 00:02:06,330 Right after that validation attempt to make sure that all occurs so I once again find my common module 28 00:02:06,330 --> 00:02:09,140 import and I will add in validate. 29 00:02:09,240 --> 00:02:17,110 Request and then after the validation attempts I will add in validate. 30 00:02:17,520 --> 00:02:21,280 Request Okay so that should be it for the title. 31 00:02:21,310 --> 00:02:30,560 Let's save this go back over George terminal and it looks like that test is now passing. 32 00:02:30,630 --> 00:02:31,890 Not bad. 33 00:02:31,980 --> 00:02:38,240 Now we're gonna repeat that same process over or the price so as a second element inside this array. 34 00:02:38,450 --> 00:02:45,720 I'm not going to add anybody I want to look at price and then with this thing when we ever we enter 35 00:02:45,720 --> 00:02:50,730 in a price I'm going to assume that we are working with the U.S. dollars U.S. dollars can have both 36 00:02:50,940 --> 00:02:53,920 dollar values and sense or in other words a decimal. 37 00:02:54,380 --> 00:02:57,750 So I really want to check and make sure that this is some kind of decimal value. 38 00:02:57,930 --> 00:02:59,990 And as we saw in our test we wrote out. 39 00:03:00,060 --> 00:03:04,680 We also probably want to validate and make sure that this thing is greater than zero as well. 40 00:03:04,680 --> 00:03:09,090 So to do so we're going to write in a check right here of is Lote 41 00:03:11,660 --> 00:03:14,150 afloat is a number that has a decimal component. 42 00:03:14,200 --> 00:03:20,060 So this is gonna make sure that we are providing a number that has a decimal or possibly has a decimal 43 00:03:21,240 --> 00:03:22,280 as the argument to this. 44 00:03:22,290 --> 00:03:27,750 We can provide a balance for this number so we can put in a greater than or less than check to put in 45 00:03:27,780 --> 00:03:28,710 a greater than check. 46 00:03:28,710 --> 00:03:32,850 We'll write in GTD standing for greater than we want to make sure. 47 00:03:32,850 --> 00:03:38,430 Again this thing is greater than 0 so then if we fail that check. 48 00:03:38,680 --> 00:03:49,590 Let's put it in once again a customer error message we'll say price must be greater than 0. 49 00:03:49,910 --> 00:03:57,100 Let's say this go back over and it looks like both of our tests around validation are passing fantastic 50 00:03:58,410 --> 00:03:58,650 well. 51 00:03:58,660 --> 00:04:00,850 That really just leaves us with the last case right here. 52 00:04:00,850 --> 00:04:05,290 We have to actually create a ticket with some valid inputs and make sure it's somehow gets saved the 53 00:04:05,290 --> 00:04:06,810 database or something like that. 54 00:04:07,150 --> 00:04:09,340 Let's start to work on that final test in just a moment.