Welcome To Golang By Example

Validate the range of the integer in a struct in Go (Golang)

Table of Contents

Overview

The below library can be used to validate the range of an integer in a struct in Golang

For this tutorial, we will use the below employee struct

type employee struct {
    Age int
}

Example

Let’s see an example for the same. Below is the code

go.mod

module sample.com/validate
go 1.14
require (
    github.com/go-playground/universal-translator v0.17.0 // indirect
    github.com/leodido/go-urn v1.2.1 // indirect
    gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
    gopkg.in/go-playground/validator.v9 v9.31.0
)

main.go

package main
import (
    "fmt"
    "gopkg.in/go-playground/validator.v9"
)
var validate *validator.Validate
type employee struct {
    Age int `validate:"required,gte=10,lte=20"`
}
func main() {
    e := employee{}
    err := validateStruct(e)
    if err != nil {
        fmt.Printf("Error: %s\n", err)
    }
    e = employee{Age: 5}
    err = validateStruct(e)
    if err != nil {
        fmt.Printf("Error: %s\n", err)
    }
    e = employee{Age: 25}
    err = validateStruct(e)
    if err != nil {
        fmt.Printf("Error: %s\n", err)
    }
}
func validateStruct(e employee) error {
    validate = validator.New()
    err := validate.Struct(e)
    if err != nil {
        return err
    }
    return nil
}

Output

Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'required' tag
Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'gte' tag
Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'lte' tag

First, we need to declare the instance of Validate

var validate *validator.Validate

Notice here that we need to associate meta tags with fields of the struct to let the validator know that you want to validate this field. In the above example, we added the tag with the Age field. This tag is interpreted by the playground validate library. Notice we added three validations for the Age field

type employee struct {
	Age int `validate:"required,gte=10,lte=20"`
}

Then call the Struct method to validate the struct

validate.Struct(e)

For

e := employee{}

it gives the output as below is Age field is empty

Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'required' tag

For

e := employee{Age: 5}

it gives the output as below as Age field value is 5 which is less than 10

Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'gte' tag

For

e := employee{Age: 25}

it gives the output as below as Age field value is 25 which is greater than 20

Error: Key: 'employee.Age' Error:Field validation for 'Age' failed on the 'lte' tag