1 00:00:00,640 --> 00:00:02,610 Everyone needs a little validation, right? 2 00:00:02,610 --> 00:00:04,270 Here, let me validate you. 3 00:00:04,270 --> 00:00:07,100 You are doing great with the Spring Data REST framework. 4 00:00:07,100 --> 00:00:08,410 It's a lot to take in and 5 00:00:08,410 --> 00:00:11,870 even though there's very few lines of code that we have written, there is most likely 6 00:00:11,870 --> 00:00:16,440 a boatload of new concepts that you've been absorbing and that, that's a lot. 7 00:00:16,440 --> 00:00:18,760 But here you are, nine videos deep and you're still smiling. 8 00:00:19,990 --> 00:00:20,870 It's weird right? 9 00:00:20,870 --> 00:00:24,720 Sometimes it's almost more challenging when the work gets done for you. 10 00:00:24,720 --> 00:00:28,060 You spend time trying to find out what the magic is that's happening behind 11 00:00:28,060 --> 00:00:30,280 the scenes, but sincerely you? 12 00:00:30,280 --> 00:00:30,840 You're doing great. 13 00:00:30,840 --> 00:00:33,080 Do you feel validated? 14 00:00:33,080 --> 00:00:35,010 I hope so because I meant it. 15 00:00:35,010 --> 00:00:38,500 Okay now, let's ensure that we're getting the data that we want, and 16 00:00:38,500 --> 00:00:42,550 sending back clear error messages to our clients when they misuse our API. 17 00:00:42,550 --> 00:00:44,140 Let's do this. 18 00:00:44,140 --> 00:00:46,875 If you watch the course Spring with Irony, you've definitely seen this 19 00:00:46,875 --> 00:00:50,690 annotation-based style of validation that we're ready to do. 20 00:00:50,690 --> 00:00:51,870 Basically what you do is, 21 00:00:51,870 --> 00:00:56,260 you mark what a valid value on your entity looks like by adding a constraint. 22 00:00:56,260 --> 00:00:58,520 For instance let's open up course here. 23 00:01:01,470 --> 00:01:07,996 Course and we want to make sure that all these courses have a title, right? 24 00:01:07,996 --> 00:01:13,223 So let's go ahead and we'll add an annotation 25 00:01:13,223 --> 00:01:18,872 on here that is a validation constraint not null. 26 00:01:18,872 --> 00:01:21,261 Yep. 27 00:01:22,490 --> 00:01:24,590 So what this validation does, 28 00:01:24,590 --> 00:01:29,010 is mark the entity as being not valid if the title is null. 29 00:01:29,010 --> 00:01:33,900 And therefore it won't be able to be saved to the database when it's attempted. 30 00:01:33,900 --> 00:01:37,490 But we want this to happen in the lifecycle events of our application. 31 00:01:37,490 --> 00:01:40,820 We want this to happen before an entity is created. 32 00:01:40,820 --> 00:01:42,280 Or saved, right? 33 00:01:42,280 --> 00:01:45,000 And let the client know that they made a mistake. 34 00:01:45,000 --> 00:01:48,850 In order to do this we need to do a little bit of configuration and it's not quite as 35 00:01:48,850 --> 00:01:52,140 simple as we saw with the properties file but it's still pretty straightforward. 36 00:01:52,140 --> 00:01:55,280 So there's a handy little adapter that is included that allows you 37 00:01:55,280 --> 00:01:59,160 to optionally override some very specific configuration options. 38 00:01:59,160 --> 00:01:59,730 So let's do this. 39 00:01:59,730 --> 00:02:05,380 Let's go into the core class here and let's create a new class. 40 00:02:05,380 --> 00:02:07,500 And we're going to call it RestConfig. 41 00:02:07,500 --> 00:02:13,730 And we're going to make it extend that adapter I was talking about. 42 00:02:13,730 --> 00:02:18,020 So we're going to say extends and the adapter is repository 43 00:02:20,810 --> 00:02:22,740 RestCofigurerAdapter. 44 00:02:25,260 --> 00:02:25,930 Okay. 45 00:02:25,930 --> 00:02:29,730 And so right off the bat let's go ahead and mark this as configuration. 46 00:02:31,558 --> 00:02:38,560 [BLANK AUDIO] And 47 00:02:38,560 --> 00:02:41,720 now, let's go ahead and override one of the methods. 48 00:02:41,720 --> 00:02:45,035 So we're going to choose generate and we're going to override methods. 49 00:02:45,035 --> 00:02:46,540 Okay, so we’re going to override. 50 00:02:46,540 --> 00:02:49,130 And the one that we want to override is this guy. 51 00:02:50,270 --> 00:02:53,980 Configure validating repository event listener. 52 00:02:55,220 --> 00:02:55,900 Jeez. 53 00:02:55,900 --> 00:02:58,090 Try to say that ten times fast. 54 00:02:58,090 --> 00:03:03,800 Okay, so this is going to do the super call to its parent like we saw by default. 55 00:03:03,800 --> 00:03:06,710 We don't actually need that so I'm gonna get rid of that line. 56 00:03:06,710 --> 00:03:09,080 And I'm also going to get some real estate because look at that. 57 00:03:10,200 --> 00:03:12,010 Wow. It doesn't even fit. 58 00:03:13,210 --> 00:03:16,680 So, it gets passed of validating repository of that listener. 59 00:03:16,680 --> 00:03:21,240 And this is what we want to configure actually, so what we're going to do 60 00:03:21,240 --> 00:03:25,980 is we are going to get access to our global validator first. 61 00:03:25,980 --> 00:03:29,530 So, let's go ahead and we need to get access to it. 62 00:03:29,530 --> 00:03:31,700 So, we're going to say validator. 63 00:03:32,830 --> 00:03:35,240 Validater and we're going to get that by name and 64 00:03:35,240 --> 00:03:37,630 again that's going to be auto wired. 65 00:03:42,510 --> 00:03:45,120 And yes that is the Spring Framework validator. 66 00:03:46,150 --> 00:03:46,710 Yep. 67 00:03:46,710 --> 00:03:48,536 And Auto wired is there. 68 00:03:48,536 --> 00:03:49,730 There we go. 69 00:03:49,730 --> 00:03:53,930 So, what we wanted to have happen was we want, before things are created, so 70 00:03:53,930 --> 00:03:58,070 this validating listener that is passed in, is what we can configure. 71 00:03:58,070 --> 00:04:00,760 And what we're going to add on that is a validator. 72 00:04:00,760 --> 00:04:04,020 It's just so happen gonna be the global validator that we're setting up. 73 00:04:04,020 --> 00:04:07,530 So we're going to say here, the event is called beforeCreate. 74 00:04:07,530 --> 00:04:08,900 We'll take a look at these here in a second. 75 00:04:11,110 --> 00:04:15,070 And so that's before creation happens and we also want to do it for an update. 76 00:04:15,070 --> 00:04:17,560 So I'm gonna go ahead and I'm gonna a duplicate that line. 77 00:04:17,560 --> 00:04:22,062 And this is beforeSave. 78 00:04:22,062 --> 00:04:26,113 So like I said we'll explore those events here in a bit but for 79 00:04:26,113 --> 00:04:29,688 now we should have a working config that will cause our 80 00:04:29,688 --> 00:04:34,450 validation to send back clean error messages on create and save. 81 00:04:34,450 --> 00:04:36,000 Okay, so lets pop back over. 82 00:04:36,000 --> 00:04:37,843 I'm gonna go ahead and restart my server here. 83 00:04:43,057 --> 00:04:51,081 Let's make sure everything goes You form an pop over to post man here and 84 00:04:51,081 --> 00:04:56,562 let's go ahead and do a new post, two courses and 85 00:04:56,562 --> 00:05:01,643 we'll set the body left or last one in there so 86 00:05:01,643 --> 00:05:06,880 what we'll do is we'll just not have a title. 87 00:05:09,170 --> 00:05:12,690 Gotta quit doing the save early, save often habit. 88 00:05:12,690 --> 00:05:13,834 All right. So let's do it. 89 00:05:13,834 --> 00:05:14,490 Let's submit it. 90 00:05:14,490 --> 00:05:15,150 So it's raw. 91 00:05:15,150 --> 00:05:16,560 We got the right header in there. 92 00:05:16,560 --> 00:05:18,780 It's application json. 93 00:05:18,780 --> 00:05:22,690 Set courses, and we're gonna click send, and boom. 94 00:05:22,690 --> 00:05:25,180 We got back a 400 Bad Request. 95 00:05:25,180 --> 00:05:29,740 And what I want you to note here is that this is an array, it's errors and it says 96 00:05:29,740 --> 00:05:34,480 course and it says title may not be null and the value that was passed was null. 97 00:05:34,480 --> 00:05:38,140 How awesome is that, an automatic error message and 98 00:05:38,140 --> 00:05:41,840 on top of it of course those messages can also be configured. 99 00:05:41,840 --> 00:05:45,470 You can actually do it right there in the validator, you just put in message and 100 00:05:45,470 --> 00:05:48,030 whatever you want the message to be amazing. 101 00:05:48,030 --> 00:05:49,250 But you know what? 102 00:05:49,250 --> 00:05:53,580 Thinking like a hacker, what happens if we just submit an empty string. 103 00:05:53,580 --> 00:05:54,380 Let's see. 104 00:05:54,380 --> 00:05:57,610 Let's go back to what we had posted before. 105 00:05:57,610 --> 00:06:00,210 And let's go ahead and we're gonna. 106 00:06:00,210 --> 00:06:01,200 This isn't going to be null. 107 00:06:01,200 --> 00:06:02,760 Right? An empty string's not null. 108 00:06:03,800 --> 00:06:04,840 Did we hack? 109 00:06:04,840 --> 00:06:05,410 Did we do it? 110 00:06:06,520 --> 00:06:07,340 We did. 111 00:06:07,340 --> 00:06:10,670 So we're gonna need another validation, right. 112 00:06:10,670 --> 00:06:12,300 Now you can stack validators, right. 113 00:06:12,300 --> 00:06:13,470 You can just add another one. 114 00:06:13,470 --> 00:06:14,490 So let's go ahead. 115 00:06:14,490 --> 00:06:22,120 Let's go back to our course here. 116 00:06:23,290 --> 00:06:24,750 And we can stack ' right. 117 00:06:24,750 --> 00:06:29,810 So we want NotNull, and we also want the size of it let's see. 118 00:06:29,810 --> 00:06:36,105 We definitely want a minimum size, so let's, two characters seems fair, right? 119 00:06:36,105 --> 00:06:39,440 Cuz you could have a course on c or a course on go, right? 120 00:06:39,440 --> 00:06:42,820 But I don't know what the length of how long it should be? 121 00:06:42,820 --> 00:06:46,790 Why don't I ask my product owner real quick. 122 00:06:46,790 --> 00:06:48,420 So let's see. 123 00:06:48,420 --> 00:06:50,110 Got slack open here. 124 00:06:50,110 --> 00:06:58,100 And I'll say hey Hannah, how long should we allow the title field to be? 125 00:06:58,100 --> 00:07:02,230 And this is very common practice, right, so I'm gonna be developing and 126 00:07:02,230 --> 00:07:02,980 ask this question. 127 00:07:02,980 --> 00:07:07,910 And let's see, hopefully Hannah's online, look, she's typing. 128 00:07:09,610 --> 00:07:13,260 So 140 character seems legit for Twitter follow suit. 129 00:07:13,260 --> 00:07:19,080 Sounds good I'm going to go ahead and let her know with a, she send a money sign. 130 00:07:20,970 --> 00:07:24,020 I give her a thumbs up I guess. 131 00:07:24,020 --> 00:07:25,970 I collect money mouth face. 132 00:07:27,340 --> 00:07:31,170 So now that I know that Hannah thinks 140 is fine, I'm just gonna go and 133 00:07:31,170 --> 00:07:32,480 update things. 134 00:07:32,480 --> 00:07:35,710 Over here, so max equals 140. 135 00:07:35,710 --> 00:07:36,800 All right, awesome. 136 00:07:36,800 --> 00:07:38,705 So let's go ahead and reboot. 137 00:07:42,795 --> 00:07:44,980 And move off over here. 138 00:07:46,670 --> 00:07:48,850 Let's look at what we had done before, and 139 00:07:48,850 --> 00:07:53,580 I'm gonna leave the title there of nothing still and see what happens. 140 00:07:53,580 --> 00:07:58,020 And we got a 400 bad requests and it says, size must be between two and 140 and 141 00:07:58,020 --> 00:08:00,000 the invalid value was basically dead. 142 00:08:00,000 --> 00:08:01,090 Awesome, right? 143 00:08:01,090 --> 00:08:04,830 Out of the box and it's using existing standards incredible. 144 00:08:04,830 --> 00:08:07,518 I've got some links in the teacher's notes about more validators that 145 00:08:07,518 --> 00:08:08,650 are available out of the box. 146 00:08:08,650 --> 00:08:11,619 And you can also build your own and hook and other life cycle events. 147 00:08:11,619 --> 00:08:14,103 As you can imagine we want to litter our API with them and 148 00:08:14,103 --> 00:08:18,300 just make sure that we're getting the data that our application expects. 149 00:08:18,300 --> 00:08:22,610 But first let's just keep on moving through this frameworks amazing offering. 150 00:08:22,610 --> 00:08:25,610 Now one thing that I wanted to show off is how we hook up with security 151 00:08:25,610 --> 00:08:27,310 authentication and all that jazz. 152 00:08:27,310 --> 00:08:30,270 But before we do that we need to get some users in place. 153 00:08:30,270 --> 00:08:30,770 Let's do that,