main
v1.3.0
github.com/GIN-GONIC/GIN/examples/struct-lvl-validations
Documentation
pkg.go.dev ↗Functions
UserStructLevelValidation
func UserStructLevelValidation(v *validator.Validate, structLevel *validator.StructLevel)
UserStructLevelValidation contains custom struct level validations that don't always make sense at the field validation level. For example, this function validates that either FirstName or LastName exist; could have done that with a custom field validation but then would have had to add it to both fields duplicating the logic + overhead, this way it's only validated once.
NOTE: you may ask why wouldn't not just do this outside of validator. Doing this way hooks right into validator and you can combine with validation tags and still have a common error output format.
Types
User
type User struct {
FirstName string `json:"fname"`
LastName string `json:"lname"`
Email string `binding:"required,email"`
}
User contains user information.