mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +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() {
|
func main() {
|
||||||
route := gin.Default()
|
route := gin.Default()
|
||||||
v := binding.ValidatorEngine()
|
|
||||||
if v == nil {
|
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||||
panic("validator engine is nil")
|
v.RegisterValidation("bookabledate", bookableDate)
|
||||||
}
|
}
|
||||||
v.RegisterValidation("bookabledate", bookableDate)
|
|
||||||
route.GET("/bookable", getBookable)
|
route.GET("/bookable", getBookable)
|
||||||
route.Run(":8085")
|
route.Run(":8085")
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,6 @@ package binding
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
validator "gopkg.in/go-playground/validator.v8"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
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.
|
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
|
||||||
// Otherwise nil must be returned.
|
// Otherwise nil must be returned.
|
||||||
ValidateStruct(interface{}) error
|
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
|
// Validator is the default validator which implements the StructValidator
|
||||||
@ -89,18 +91,3 @@ func validate(obj interface{}) error {
|
|||||||
}
|
}
|
||||||
return Validator.ValidateStruct(obj)
|
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
|
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() {
|
func (v *defaultValidator) lazyinit() {
|
||||||
v.once.Do(func() {
|
v.once.Do(func() {
|
||||||
config := &validator.Config{TagName: "binding"}
|
config := &validator.Config{TagName: "binding"}
|
||||||
|
@ -218,8 +218,8 @@ func TestValidatorEngine(t *testing.T) {
|
|||||||
// This validates that the function `notOne` matches
|
// This validates that the function `notOne` matches
|
||||||
// the expected function signature by `defaultValidator`
|
// the expected function signature by `defaultValidator`
|
||||||
// and by extension the validator library.
|
// and by extension the validator library.
|
||||||
engine := ValidatorEngine()
|
engine, ok := Validator.Engine().(*validator.Validate)
|
||||||
assert.NotNil(t, engine)
|
assert.True(t, ok)
|
||||||
|
|
||||||
err := engine.RegisterValidation("notone", notOne)
|
err := engine.RegisterValidation("notone", notOne)
|
||||||
// Check that we can register custom validation without error
|
// Check that we can register custom validation without error
|
||||||
|
@ -30,11 +30,11 @@ func bookableDate(
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
route := gin.Default()
|
route := gin.Default()
|
||||||
v := binding.ValidatorEngine()
|
|
||||||
if v == nil {
|
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||||
panic("validator engine is nil")
|
v.RegisterValidation("bookabledate", bookableDate)
|
||||||
}
|
}
|
||||||
v.RegisterValidation("bookabledate", bookableDate)
|
|
||||||
route.GET("/bookable", getBookable)
|
route.GET("/bookable", getBookable)
|
||||||
route.Run(":8085")
|
route.Run(":8085")
|
||||||
}
|
}
|
||||||
|
@ -42,11 +42,11 @@ func UserStructLevelValidation(v *validator.Validate, structLevel *validator.Str
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
route := gin.Default()
|
route := gin.Default()
|
||||||
v := binding.ValidatorEngine()
|
|
||||||
if v == nil {
|
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||||
panic("validator engine is nil")
|
v.RegisterStructValidation(UserStructLevelValidation, User{})
|
||||||
}
|
}
|
||||||
v.RegisterStructValidation(UserStructLevelValidation, User{})
|
|
||||||
route.POST("/user", validateUser)
|
route.POST("/user", validateUser)
|
||||||
route.Run(":8085")
|
route.Run(":8085")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user