Go Struct and Field Validation Examples
Package validator implements value validations for structs and individual fields based on tags. It has the following unique features for Cross Field and Cross Struct validations by using validation tags or custom validators. Slice, Array and Map diving, which allows any or all levels of a multidimensional field to be validated. Ability to dive into both map keys and values for validation.
Installation
go get github.com/go-playground/validator
Example
package main
import (
"fmt"
"github.com/go-playground/validator"
)
// User contains user information
type User struct {
FirstName string `json:"fname" validate:"alpha"`
LastName string `json:"lname" validate:"alpha"`
Age uint8 `validate:"gte=20,lte=65"`
Email string `json:"e-mail" validate:"required,email"`
JoiningDate string `validate:"datetime"`
}
// use a single instance of Validate, it caches struct info
var validate *validator.Validate
func main() {
validate = validator.New()
user := &User{
FirstName: "Test25",
LastName: "Test",
Age: 75,
Email: "Badger.Smith@",
JoiningDate: "005-25-10",
}
err := validate.Struct(user)
if err != nil {
if _, ok := err.(*validator.InvalidValidationError); ok {
fmt.Println(err)
return
}
fmt.Println("------ List of tag fields with error ---------")
for _, err := range err.(validator.ValidationErrors) {
fmt.Println(err.StructField())
fmt.Println(err.ActualTag())
fmt.Println(err.Kind())
fmt.Println(err.Value())
fmt.Println(err.Param())
fmt.Println("---------------")
}
return
}
}
Output
------ List of tag fields with error ---------
FirstName
alpha
string
Test25
---------------
Age
lte
uint8
75
65
---------------
Email
email
string
Badger.Smith@
---------------
JoiningDate
datetime
string
005-25-10
---------------