Closed aomirun closed 4 years ago
package main
import ( "fmt" "os"
validation "github.com/go-ozzo/ozzo-validation"
)
type user struct { Name string json:"name,omitempty" Age int json:"age,omitempty" }
json:"name,omitempty"
json:"age,omitempty"
func init() { u := user{} if err := validation.ValidateStruct(&u, validation.Field(&u.Name, validation.NilOrNotEmpty, validation.Length(3, 20)), ); err != nil { fmt.Println(err) os.Exit(0) } } // ouput: name: cannot be blank.
This is expected because Name is an empty string and the rule requires it not empty. omitempty has nothing to do here.
Name
package main
import ( "fmt" "os"
)
type user struct { Name string
json:"name,omitempty"
Age intjson:"age,omitempty"
}func init() { u := user{} if err := validation.ValidateStruct(&u, validation.Field(&u.Name, validation.NilOrNotEmpty, validation.Length(3, 20)), ); err != nil { fmt.Println(err) os.Exit(0) } } // ouput: name: cannot be blank.