DEV Community

Boluwatife Fakorede
Boluwatife Fakorede

Posted on

How I validate user input in golang

#go

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:

Alt Text

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.

Alt Text

Let us try to send a request but with a different type for the phoneNumber field (type int).

Alt Text

The response gotten is below:

Alt Text

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:

  1. What if the right type was entered or it was ignored totally and not sent?

  2. 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:

Alt Text

That way, we have some constraints defined already for each field required been the minimum.

We need to do just a little bit more.

  1. Import the validation package into our application.

  2. Define a way for the errors to be returned.

  3. Use it in our route handler function.

  4. Test it!

Let's do that.

Alt Text

2.
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:

Alt Text

Alt Text

PS: All just one code. You can find a very good explanation of the code above in the link below.

(https://stackedco.de/validating-rest-input-fields-using-golang-structs)

3.
Let's make use of our validator helper in our handler.

Alt Text

4.
Let's go for a ride with our code.

Alt Text

Notice we left out the phoneNumber field, the validator returned the field and the error.

Alt Text

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:

Alt Text

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.

Top comments (2)

Collapse
 
itscosmas profile image
Cosmas Gikunju

This is actually nice

Collapse
 
lavotp profile image
Topollo

which theme are you using?