Skip to content

Commit

Permalink
fix(binding): Move validator engine getter inside interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-suhas committed Mar 10, 2018
1 parent 610cb98 commit 1507bf2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 31 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
21 changes: 4 additions & 17 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package binding

import (
"net/http"

validator "gopkg.in/go-playground/validator.v8"
)

const (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions binding/default_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
4 changes: 2 additions & 2 deletions binding/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions examples/custom-validation/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
7 changes: 3 additions & 4 deletions examples/struct-lvl-validations/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ 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")
}
Expand Down

0 comments on commit 1507bf2

Please sign in to comment.