Skip to content

Commit 1507bf2

Browse files
committed
fix(binding): Move validator engine getter inside interface
1 parent 610cb98 commit 1507bf2

File tree

6 files changed

+26
-31
lines changed

6 files changed

+26
-31
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ func bookableDate(
564564

565565
func main() {
566566
route := gin.Default()
567-
v := binding.ValidatorEngine()
568-
if v == nil {
569-
panic("validator engine is nil")
567+
568+
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
569+
v.RegisterValidation("bookabledate", bookableDate)
570570
}
571-
v.RegisterValidation("bookabledate", bookableDate)
571+
572572
route.GET("/bookable", getBookable)
573573
route.Run(":8085")
574574
}

binding/binding.go

+4-17
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package binding
66

77
import (
88
"net/http"
9-
10-
validator "gopkg.in/go-playground/validator.v8"
119
)
1210

1311
const (
@@ -42,6 +40,10 @@ type StructValidator interface {
4240
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
4341
// Otherwise nil must be returned.
4442
ValidateStruct(interface{}) error
43+
44+
// Engine returns the underlying validator engine which powers the
45+
// StructValidator implementation.
46+
Engine() interface{}
4547
}
4648

4749
// Validator is the default validator which implements the StructValidator
@@ -89,18 +91,3 @@ func validate(obj interface{}) error {
8991
}
9092
return Validator.ValidateStruct(obj)
9193
}
92-
93-
// ValidatorEngine returns the underlying validator engine which powers the
94-
// default Validator instance. This is useful if you want to register custom
95-
// validations or struct level validations. See validator GoDoc for more info -
96-
// https://godoc.org/gopkg.in/go-playground/validator.v8
97-
func ValidatorEngine() *validator.Validate {
98-
if Validator == nil {
99-
return nil
100-
}
101-
if v, ok := Validator.(*defaultValidator); ok {
102-
v.lazyinit()
103-
return v.validate
104-
}
105-
return nil
106-
}

binding/default_validator.go

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ func (v *defaultValidator) ValidateStruct(obj interface{}) error {
2828
return nil
2929
}
3030

31+
// Engine returns the underlying validator engine which powers the default
32+
// Validator instance. This is useful if you want to register custom validations
33+
// or struct level validations. See validator GoDoc for more info -
34+
// https://godoc.org/gopkg.in/go-playground/validator.v8
35+
func (v *defaultValidator) Engine() interface{} {
36+
v.lazyinit()
37+
return v.validate
38+
}
39+
3140
func (v *defaultValidator) lazyinit() {
3241
v.once.Do(func() {
3342
config := &validator.Config{TagName: "binding"}

binding/validate_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ func TestValidatorEngine(t *testing.T) {
218218
// This validates that the function `notOne` matches
219219
// the expected function signature by `defaultValidator`
220220
// and by extension the validator library.
221-
engine := ValidatorEngine()
222-
assert.NotNil(t, engine)
221+
engine, ok := Validator.Engine().(*validator.Validate)
222+
assert.True(t, ok)
223223

224224
err := engine.RegisterValidation("notone", notOne)
225225
// Check that we can register custom validation without error

examples/custom-validation/server.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func bookableDate(
3030

3131
func main() {
3232
route := gin.Default()
33-
v := binding.ValidatorEngine()
34-
if v == nil {
35-
panic("validator engine is nil")
33+
34+
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
35+
v.RegisterValidation("bookabledate", bookableDate)
3636
}
37-
v.RegisterValidation("bookabledate", bookableDate)
37+
3838
route.GET("/bookable", getBookable)
3939
route.Run(":8085")
4040
}

examples/struct-lvl-validations/server.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ func UserStructLevelValidation(v *validator.Validate, structLevel *validator.Str
4242

4343
func main() {
4444
route := gin.Default()
45-
v := binding.ValidatorEngine()
46-
if v == nil {
47-
panic("validator engine is nil")
45+
46+
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
47+
v.RegisterStructValidation(UserStructLevelValidation, User{})
4848
}
49-
v.RegisterStructValidation(UserStructLevelValidation, User{})
5049
route.POST("/user", validateUser)
5150
route.Run(":8085")
5251
}

0 commit comments

Comments
 (0)