Every application needs some way to validate the user inputs, this prevents the wrong kind of inputs been entered before ever reaching the database.
Coming from a Node.Js background for API service development, I always do some validation on the backend with JOI, I wanted something great to work with also in golang.
Using golang has a lot of advantages and I found been strongly typed one of the many useful features of golang.
The strongly typed built-in feature of golang already provided some sort of validation for the application, let's dive into some code.
Let's say you want to build an application, with the model defined below:
With the code above, the type of each field has been defined with some tags
In the route handler function, we will decode the request into the struct defined above.
Let us try to send a request but with a different type for the phoneNumber field (type int).
The response gotten is below:
This shows that there is some form of validation already in Go because it is strongly typed.
But there is so much we can do with this, some listed below:
What if the right type was entered or it was ignored totally and not sent?
What if I want to set a minimum amount of entry to be entered for maybe a password or a phone number not to exceed a particular length?
So I needed to find a way to validate each field and not re-invent the wheel.
I came across a validation package (https://godoc.org/gopkg.in/go-playground/validator.v9) amongst many out there and it is quite easy to use.
You add to the tags already defined as shown below:
That way, we have some constraints defined already for each field required been the minimum.
We need to do just a little bit more.
Import the validation package into our application.
Define a way for the errors to be returned.
Use it in our route handler function.
Let's do that.
I created a helper function that takes a type interface (actually just any type of struct) and returns a bool (true) and errors (a map of key string and value string holding the field that failed validation and the corresponding error fail) or it returns false and nil if it passed the validation as shown below:
PS: All just one code. You can find a very good explanation of the code above in the link below.
Let's make use of our validator helper in our handler.
Let's go for a ride with our code.
Notice we left out the phoneNumber field, the validator returned the field and the error.
Now we have a working validation process for our application.
Let's deal with another case.
I struggled with really, Imagine you have to embed another struct in your model struct and this can be required for many reasons one of them been you are performing a left join in your database query and need to return this new struct fields alongside the initial query, but you want to avoid validating this field since it is not required for the user to input the fields.
There is also a way to avoid this, all you have to do is in the embedded struct, add a validate with "-" as shown below:
This way, it is ignored when validating even if validations are required on the struct definition.
Thanks for reading!!
PS: This is my first post.