One of the things we haven't done yet, and we probably should have at least a quick look at, is server
side validation.
All of our validation right now is done, client side using bootstraps validation and some custom JavaScript.
And it works really well.
But it's always good practice to validate your data, both on client side and on server side.
Now, as you can see here, I added a picture just because I wanted to look at something other than
a single line of text here, but that's neither here nor there.
If you want that, you can download the source for this lecture and get that picture.
But that's hardly important.
What we'll do this time around is on this page.
Subscription page is will add validation just for one field, just to show you how it works.
And you can validate to your heart's content after that.
And it's pretty straightforward.
So let's go back to Visual Studio.
And what I'll do is I probably want my validation to be available to pretty much anything that I'm building
using this code base.
So I'll make it an internal package.
I'll open my internal folder and I'll create a new folder called Validator
like that.
And instead of that, I'll create a very simple go file called Validator or Go.
And of course it has the package validator.
And the first thing I'll do is to find a type called, not surprisingly, validator.
And it's just a struct.
And it only has one field called errors and it will be a map with a key of string and the content of
string as well.
OK, so that's the type we'll use to hold errors, if any.
Now we'll create a new function called New and it will be a pointer to the type validator.
That's what it returns.
OK, and all we're going to do is return a reference to validator and we'll populate errors with an
empty make map, string, string.
So any time we want to perform validation on any kind of data, we can just create a new instance of
this and have any of the functions that have the receiver pointer to the validator available to them.
And we'll make a couple here.
So the first one I'll make is valid.
So func with a receiver of the pointer to validate or valid.
This is what I'll call to check to see if the current value of my map is empty.
In other words, it's an empty map, so we'll just return.
Return.
Len, the errors is equal to zero and of course it has to return of Boolean.
OK, so we'll call that to see if our data passes, whatever validation checks we want to put in there.
Now, obviously, we also need a means of adding errors to the map.
So from the pointer to validator and we'll call it an error and it will take a key and a message, both
of which will be straight.
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
already have an error for that piece of data will ignore the first return value and pay attention to
the second one exists.
It's a sign the value of Vyborg errors looking for our key.
And if not exists, then we'll just say vido errors.
Key
is equal to message and I will add something to our map of errors.
Now let's put one rule in there and we'll make it a very generalized rule so we can do all kinds of
checks with a really simple syntax.
And you'll see what I mean in a minute.
So func the pointer to validator and we'll say check.
And it takes an argument, OK, which is a ball and it takes a key and a message which both of which
are strings and it doesn't return anything.
And we'll say, if not, OK.
In other words, if false, then we that error key.
And message.
OK, so there's a very, very simple validator, but as you'll see shortly, this is a really powerful
tool for adding errors and putting our checks in a really simple way.
OK, now, if we have an error, we need some means of sending that message back to the user and we're
doing this on the back end.
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
add a very specific function to call when validation fails.
So it'll be a function with the receiver of appointed application, pretty much like everything else
in this package for in this file.
And we'll just call it field validation and it will take W, which is an intuitive response writer and
I might need the request at some point.
So I'll just put it here right now.
Probably not going to use it right now, but we'll use it, we'll will add it in there and we'll have
errors which will be a map of string string.
And all we have to do here is create a payload for our response of our payload will be a struct and
it has three members error, which is a bool.
And in JSON we call that, as usual, error.
And it also has the message, which will be a string.
And in JSON, as usual, we'll call that message and we'll also have our errors, which there might
be more than one, because it's a map of string, the string and some calling it errors, plural, in
JSON and in the field name.
And how we populate our payload payload error equals true payload message.
Equals fame, validation and payload errors, plural is equal to the value parts to this function.
And then we reached Jason, so Abdulkarim, Jason, and I'm going to write with the response writer,
which is required, I'm going to give it a specific error code, HDP status, unprocessed LBL entity
right there, OK?
And that's the correct one for feeling validation, I think.
And we haven't handed our payload.
OK, so now this is available to us.
So of course we need to use this validation.
Now in the next lecture will actually go to our handlers API and validate a single field.
Say first name.