WEBVTT 00:00.940 --> 00:06.460 One of the things we haven't done yet, and we probably should have at least a quick look at, is server 00:06.460 --> 00:07.600 side validation. 00:07.720 --> 00:13.480 All of our validation right now is done, client side using bootstraps validation and some custom JavaScript. 00:13.660 --> 00:14.650 And it works really well. 00:15.040 --> 00:20.190 But it's always good practice to validate your data, both on client side and on server side. 00:20.890 --> 00:25.240 Now, as you can see here, I added a picture just because I wanted to look at something other than 00:25.240 --> 00:27.580 a single line of text here, but that's neither here nor there. 00:27.580 --> 00:31.180 If you want that, you can download the source for this lecture and get that picture. 00:31.190 --> 00:32.280 But that's hardly important. 00:32.800 --> 00:35.770 What we'll do this time around is on this page. 00:35.910 --> 00:40.870 Subscription page is will add validation just for one field, just to show you how it works. 00:40.870 --> 00:43.330 And you can validate to your heart's content after that. 00:43.330 --> 00:44.390 And it's pretty straightforward. 00:45.280 --> 00:46.540 So let's go back to Visual Studio. 00:47.560 --> 00:53.200 And what I'll do is I probably want my validation to be available to pretty much anything that I'm building 00:53.200 --> 00:54.180 using this code base. 00:54.190 --> 00:56.170 So I'll make it an internal package. 00:56.170 --> 01:01.510 I'll open my internal folder and I'll create a new folder called Validator 01:04.300 --> 01:04.780 like that. 01:05.560 --> 01:11.230 And instead of that, I'll create a very simple go file called Validator or Go. 01:12.100 --> 01:14.440 And of course it has the package validator. 01:16.300 --> 01:21.040 And the first thing I'll do is to find a type called, not surprisingly, validator. 01:21.760 --> 01:22.780 And it's just a struct. 01:22.780 --> 01:29.500 And it only has one field called errors and it will be a map with a key of string and the content of 01:29.500 --> 01:30.230 string as well. 01:30.550 --> 01:33.830 OK, so that's the type we'll use to hold errors, if any. 01:34.630 --> 01:41.920 Now we'll create a new function called New and it will be a pointer to the type validator. 01:41.950 --> 01:42.880 That's what it returns. 01:42.970 --> 01:51.550 OK, and all we're going to do is return a reference to validator and we'll populate errors with an 01:51.550 --> 01:56.440 empty make map, string, string. 01:56.800 --> 02:01.360 So any time we want to perform validation on any kind of data, we can just create a new instance of 02:01.360 --> 02:07.000 this and have any of the functions that have the receiver pointer to the validator available to them. 02:07.000 --> 02:08.010 And we'll make a couple here. 02:08.920 --> 02:10.780 So the first one I'll make is valid. 02:10.870 --> 02:14.710 So func with a receiver of the pointer to validate or valid. 02:14.880 --> 02:20.620 This is what I'll call to check to see if the current value of my map is empty. 02:21.460 --> 02:24.520 In other words, it's an empty map, so we'll just return. 02:26.890 --> 02:27.490 Return. 02:27.880 --> 02:34.870 Len, the errors is equal to zero and of course it has to return of Boolean. 02:37.030 --> 02:42.660 OK, so we'll call that to see if our data passes, whatever validation checks we want to put in there. 02:43.300 --> 02:46.300 Now, obviously, we also need a means of adding errors to the map. 02:46.300 --> 02:55.060 So from the pointer to validator and we'll call it an error and it will take a key and a message, both 02:55.060 --> 02:56.050 of which will be straight. 02:57.190 --> 03:02.740 And it doesn't return anything because all we have to do here is to say if we want to make sure we don't 03:02.740 --> 03:09.550 already have an error for that piece of data will ignore the first return value and pay attention to 03:09.550 --> 03:11.110 the second one exists. 03:12.100 --> 03:16.510 It's a sign the value of Vyborg errors looking for our key. 03:17.620 --> 03:22.210 And if not exists, then we'll just say vido errors. 03:22.210 --> 03:22.570 Key 03:25.480 --> 03:30.410 is equal to message and I will add something to our map of errors. 03:31.480 --> 03:37.000 Now let's put one rule in there and we'll make it a very generalized rule so we can do all kinds of 03:37.000 --> 03:39.910 checks with a really simple syntax. 03:40.240 --> 03:41.360 And you'll see what I mean in a minute. 03:41.770 --> 03:46.000 So func the pointer to validator and we'll say check. 03:47.350 --> 03:53.890 And it takes an argument, OK, which is a ball and it takes a key and a message which both of which 03:53.890 --> 03:56.740 are strings and it doesn't return anything. 03:57.130 --> 03:59.210 And we'll say, if not, OK. 03:59.260 --> 04:04.330 In other words, if false, then we that error key. 04:06.430 --> 04:07.090 And message. 04:09.830 --> 04:16.030 OK, so there's a very, very simple validator, but as you'll see shortly, this is a really powerful 04:16.030 --> 04:20.090 tool for adding errors and putting our checks in a really simple way. 04:20.760 --> 04:27.520 OK, now, if we have an error, we need some means of sending that message back to the user and we're 04:27.520 --> 04:28.510 doing this on the back end. 04:28.520 --> 04:35.890 So what I'm going to do is go to my CMD folder and go to API and open up the Helper's file and we'll 04:35.890 --> 04:39.660 add a very specific function to call when validation fails. 04:40.330 --> 04:44.680 So it'll be a function with the receiver of appointed application, pretty much like everything else 04:44.680 --> 04:46.390 in this package for in this file. 04:46.900 --> 04:54.640 And we'll just call it field validation and it will take W, which is an intuitive response writer and 04:54.640 --> 04:56.020 I might need the request at some point. 04:56.020 --> 04:57.630 So I'll just put it here right now. 04:58.930 --> 05:03.280 Probably not going to use it right now, but we'll use it, we'll will add it in there and we'll have 05:03.280 --> 05:06.850 errors which will be a map of string string. 05:09.550 --> 05:15.730 And all we have to do here is create a payload for our response of our payload will be a struct and 05:15.730 --> 05:18.490 it has three members error, which is a bool. 05:18.820 --> 05:21.280 And in JSON we call that, as usual, error. 05:22.990 --> 05:25.990 And it also has the message, which will be a string. 05:28.720 --> 05:37.300 And in JSON, as usual, we'll call that message and we'll also have our errors, which there might 05:37.300 --> 05:43.450 be more than one, because it's a map of string, the string and some calling it errors, plural, in 05:43.450 --> 05:45.220 JSON and in the field name. 05:49.440 --> 05:57.660 And how we populate our payload payload error equals true payload message. 06:01.810 --> 06:12.210 Equals fame, validation and payload errors, plural is equal to the value parts to this function. 06:14.570 --> 06:21.850 And then we reached Jason, so Abdulkarim, Jason, and I'm going to write with the response writer, 06:21.850 --> 06:28.990 which is required, I'm going to give it a specific error code, HDP status, unprocessed LBL entity 06:29.140 --> 06:30.310 right there, OK? 06:30.400 --> 06:32.890 And that's the correct one for feeling validation, I think. 06:33.430 --> 06:34.690 And we haven't handed our payload. 06:35.350 --> 06:37.080 OK, so now this is available to us. 06:38.050 --> 06:40.270 So of course we need to use this validation. 06:40.510 --> 06:46.510 Now in the next lecture will actually go to our handlers API and validate a single field. 06:46.510 --> 06:47.440 Say first name.