Clarify and complete tag names example

pull/530/head
Jean-Philippe Moal 5 years ago
parent 94aa4243a2
commit c370ccf1ba
  1. 4
      _examples/simple/main.go
  2. 27
      _examples/struct-level/main.go

@ -68,8 +68,8 @@ func validateStruct() {
fmt.Println(err.Namespace()) fmt.Println(err.Namespace())
fmt.Println(err.Field()) fmt.Println(err.Field())
fmt.Println(err.StructNamespace()) // can differ when a custom TagNameFunc is registered or fmt.Println(err.StructNamespace())
fmt.Println(err.StructField()) // by passing alt name to ReportError like below fmt.Println(err.StructField())
fmt.Println(err.Tag()) fmt.Println(err.Tag())
fmt.Println(err.ActualTag()) fmt.Println(err.ActualTag())
fmt.Println(err.Kind()) fmt.Println(err.Kind())

@ -2,6 +2,8 @@ package main
import ( import (
"fmt" "fmt"
"reflect"
"strings"
"gopkg.in/go-playground/validator.v9" "gopkg.in/go-playground/validator.v9"
) )
@ -11,7 +13,7 @@ type User struct {
FirstName string `json:"fname"` FirstName string `json:"fname"`
LastName string `json:"lname"` LastName string `json:"lname"`
Age uint8 `validate:"gte=0,lte=130"` Age uint8 `validate:"gte=0,lte=130"`
Email string `validate:"required,email"` Email string `json:"e-mail" validate:"required,email"`
FavouriteColor string `validate:"hexcolor|rgb|rgba"` FavouriteColor string `validate:"hexcolor|rgb|rgba"`
Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage... Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
} }
@ -31,6 +33,15 @@ func main() {
validate = validator.New() validate = validator.New()
// register function to get tag name from json tags.
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
// register validation for 'User' // register validation for 'User'
// NOTE: only have to register a non-pointer type for 'User', validator // NOTE: only have to register a non-pointer type for 'User', validator
// interanlly dereferences during it's type checks. // interanlly dereferences during it's type checks.
@ -48,7 +59,7 @@ func main() {
FirstName: "", FirstName: "",
LastName: "", LastName: "",
Age: 45, Age: 45,
Email: "Badger.Smith@gmail.com", Email: "Badger.Smith@gmail",
FavouriteColor: "#000", FavouriteColor: "#000",
Addresses: []*Address{address}, Addresses: []*Address{address},
} }
@ -67,10 +78,10 @@ func main() {
for _, err := range err.(validator.ValidationErrors) { for _, err := range err.(validator.ValidationErrors) {
fmt.Println(err.Namespace()) fmt.Println(err.Namespace()) // can differ when a custom TagNameFunc is registered or
fmt.Println(err.Field()) fmt.Println(err.Field()) // by passing alt name to ReportError like below
fmt.Println(err.StructNamespace()) // can differ when a custom TagNameFunc is registered or fmt.Println(err.StructNamespace())
fmt.Println(err.StructField()) // by passing alt name to ReportError like below fmt.Println(err.StructField())
fmt.Println(err.Tag()) fmt.Println(err.Tag())
fmt.Println(err.ActualTag()) fmt.Println(err.ActualTag())
fmt.Println(err.Kind()) fmt.Println(err.Kind())
@ -101,8 +112,8 @@ func UserStructLevelValidation(sl validator.StructLevel) {
user := sl.Current().Interface().(User) user := sl.Current().Interface().(User)
if len(user.FirstName) == 0 && len(user.LastName) == 0 { if len(user.FirstName) == 0 && len(user.LastName) == 0 {
sl.ReportError(user.FirstName, "FirstName", "fname", "fnameorlname", "") sl.ReportError(user.FirstName, "fname", "FirstName", "fnameorlname", "")
sl.ReportError(user.LastName, "LastName", "lname", "fnameorlname", "") sl.ReportError(user.LastName, "lname", "LastName", "fnameorlname", "")
} }
// plus can do more, even with different tag than "fnameorlname" // plus can do more, even with different tag than "fnameorlname"

Loading…
Cancel
Save