mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 18:22:23 +08:00
fix(binding): Move validator engine getter inside interface
This commit is contained in:
parent
bf4942c4ff
commit
b308c8b0a9
@ -564,11 +564,11 @@ func bookableDate(
|
||||
|
||||
func main() {
|
||||
route := gin.Default()
|
||||
v := binding.ValidatorEngine()
|
||||
if v == nil {
|
||||
panic("validator engine is nil")
|
||||
|
||||
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||
v.RegisterValidation("bookabledate", bookableDate)
|
||||
}
|
||||
v.RegisterValidation("bookabledate", bookableDate)
|
||||
|
||||
route.GET("/bookable", getBookable)
|
||||
route.Run(":8085")
|
||||
}
|
||||
|
@ -6,8 +6,6 @@ package binding
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
validator "gopkg.in/go-playground/validator.v8"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -42,6 +40,10 @@ type StructValidator interface {
|
||||
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
|
||||
// Otherwise nil must be returned.
|
||||
ValidateStruct(interface{}) error
|
||||
|
||||
// Engine returns the underlying validator engine which powers the
|
||||
// StructValidator implementation.
|
||||
Engine() interface{}
|
||||
}
|
||||
|
||||
// Validator is the default validator which implements the StructValidator
|
||||
@ -89,18 +91,3 @@ func validate(obj interface{}) error {
|
||||
}
|
||||
return Validator.ValidateStruct(obj)
|
||||
}
|
||||
|
||||
// ValidatorEngine returns the underlying validator engine which powers the
|
||||
// default Validator instance. This is useful if you want to register custom
|
||||
// validations or struct level validations. See validator GoDoc for more info -
|
||||
// https://godoc.org/gopkg.in/go-playground/validator.v8
|
||||
func ValidatorEngine() *validator.Validate {
|
||||
if Validator == nil {
|
||||
return nil
|
||||
}
|
||||
if v, ok := Validator.(*defaultValidator); ok {
|
||||
v.lazyinit()
|
||||
return v.validate
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -28,6 +28,15 @@ func (v *defaultValidator) ValidateStruct(obj interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Engine returns the underlying validator engine which powers the default
|
||||
// Validator instance. This is useful if you want to register custom validations
|
||||
// or struct level validations. See validator GoDoc for more info -
|
||||
// https://godoc.org/gopkg.in/go-playground/validator.v8
|
||||
func (v *defaultValidator) Engine() interface{} {
|
||||
v.lazyinit()
|
||||
return v.validate
|
||||
}
|
||||
|
||||
func (v *defaultValidator) lazyinit() {
|
||||
v.once.Do(func() {
|
||||
config := &validator.Config{TagName: "binding"}
|
||||
|
@ -218,8 +218,8 @@ func TestValidatorEngine(t *testing.T) {
|
||||
// This validates that the function `notOne` matches
|
||||
// the expected function signature by `defaultValidator`
|
||||
// and by extension the validator library.
|
||||
engine := ValidatorEngine()
|
||||
assert.NotNil(t, engine)
|
||||
engine, ok := Validator.Engine().(*validator.Validate)
|
||||
assert.True(t, ok)
|
||||
|
||||
err := engine.RegisterValidation("notone", notOne)
|
||||
// Check that we can register custom validation without error
|
||||
|
@ -30,11 +30,11 @@ func bookableDate(
|
||||
|
||||
func main() {
|
||||
route := gin.Default()
|
||||
v := binding.ValidatorEngine()
|
||||
if v == nil {
|
||||
panic("validator engine is nil")
|
||||
|
||||
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||
v.RegisterValidation("bookabledate", bookableDate)
|
||||
}
|
||||
v.RegisterValidation("bookabledate", bookableDate)
|
||||
|
||||
route.GET("/bookable", getBookable)
|
||||
route.Run(":8085")
|
||||
}
|
||||
|
@ -42,11 +42,11 @@ func UserStructLevelValidation(v *validator.Validate, structLevel *validator.Str
|
||||
|
||||
func main() {
|
||||
route := gin.Default()
|
||||
v := binding.ValidatorEngine()
|
||||
if v == nil {
|
||||
panic("validator engine is nil")
|
||||
|
||||
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||
v.RegisterStructValidation(UserStructLevelValidation, User{})
|
||||
}
|
||||
v.RegisterStructValidation(UserStructLevelValidation, User{})
|
||||
|
||||
route.POST("/user", validateUser)
|
||||
route.Run(":8085")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user