Skip to content
Santekno.com | Level Up Your Engineering Skills
EN
📖 0%
28 Jan 2024 · 2 min read ·Article 85 / 119
Go

08 Adding a Request Validator Using Golang Playground

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

At this stage we will add validation for each request sent to the API Application on our Services for example on add, change and delete requests. The validation we use is package github.com/go-playground/validator.

If we look at the documentation, there is a function that we will use in the package by checking the struct that we will validate.

go
1err := validate.Struct(myStruct)
2validateErrors := err(.Validator.ValidationErrors)

Then we need to add some tags to the fields in the struct that we will validate.

Adding Dependency Validator

First we add the validator package that we will need to the project by using the module golang.

bash
1go get github.com/go-playground/validator/v10
Then import the validator package into the code we are using
go
1import "github.com/go-playground/validator/v10"

Adding Validation to Struct

In the create article request there are several validations that we must apply by adding to the struct create request as below.

go
1type ArticleCreateRequest struct {
2	Title string `validate: "required,max=160,min=0"`
3	Content string `validate: "required"`
4	CreateAt time.Time
5}

Next, add a request to the article update request

go
1type ArticleUpdateRequest struct {
2	ID int64 `validate: "required"`
3	Title string `validate: "required,max=160,min=0"`
4	Content string `validate: "required"`
5	UpdateAt time.Time
6}

After the struct adds validators to each of the fields that need validation, we then add the validator initialization to the handler.

go
 1type Delivery struct {
 2	articleUsecase usecase.ArticleUsecase
 3	validate *validator.Validate
 4}
 5
 6func New(articleUsecase usecase.ArticleUsecase) *Delivery {
 7	return &Delivery{
 8		articleUsecase: articleUsecase,
 9		validate: validator.New(),
10	}
11}

And add struct validation to the Create and Update functions in the position handler before accessing the usecase layer.

go
 1func (d *Delivery) Store(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
 2	...
 3	...
 4
 5	err = d.validate.Struct(request)
 6	if err != nil {
 7		response.Code = http.StatusBadRequest
 8		response.Status = err.Error()
 9	}
10
11	...
12	...
13}

And in the Update function we also add a handler like this.

go
 1func (d *Delivery) Update(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
 2	...
 3	...
 4
 5	err = d.validate.Struct(request)
 6	if err != nil {
 7		response.Code = http.StatusBadRequest
 8		response.Status = err.Error()
 9	}
10	
11	...
12	...
13}

All the functions that we need to validate requests have been implemented so that when there is a request from the user this validator works so that the form fields in the struct do not contain blanks and according to the rules that have been determined by us.

Related Articles

💬 Comments