Skip to content

Commit

Permalink
fix: validate
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Sep 21, 2023
1 parent a6cfb63 commit e2ce1ce
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 45 deletions.
3 changes: 3 additions & 0 deletions pkg/app/server/binding/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func NewDefaultBinder(config *BindConfig) Binder {
}
}
config.initTypeUnmarshal()
if config.Validator == nil {
config.Validator = DefaultValidator()
}
return &defaultBinder{
config: config,
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/app/server/hertz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,11 +827,11 @@ func TestBindConfig(t *testing.T) {
type Req struct {
A int `query:"a"`
}
bindConfig := binding.NewBindConfig()
bindConfig.LooseZeroMode = true
h := New(
WithHostPorts("localhost:9332"),
WithBindConfig(&binding.BindConfig{
LooseZeroMode: true,
}))
WithBindConfig(bindConfig))
h.GET("/bind", func(c context.Context, ctx *app.RequestContext) {
var req Req
err := ctx.BindAndValidate(&req)
Expand All @@ -846,11 +846,11 @@ func TestBindConfig(t *testing.T) {
_, err := hc.Get("http://127.0.0.1:9332/bind?a=")
assert.Nil(t, err)

bindConfig = binding.NewBindConfig()
bindConfig.LooseZeroMode = false
h2 := New(
WithHostPorts("localhost:9448"),
WithBindConfig(&binding.BindConfig{
LooseZeroMode: false,
}))
WithBindConfig(bindConfig))
h2.GET("/bind", func(c context.Context, ctx *app.RequestContext) {
var req Req
err := ctx.BindAndValidate(&req)
Expand Down Expand Up @@ -938,8 +938,7 @@ func TestValidateConfig(t *testing.T) {
return fmt.Errorf("test validator")
})
h := New(
WithHostPorts("localhost:9229"),
WithValidateConfig(validateConfig))
WithHostPorts("localhost:9229"))
h.GET("/bind", func(c context.Context, ctx *app.RequestContext) {
var req Req
err := ctx.BindAndValidate(&req)
Expand Down
7 changes: 0 additions & 7 deletions pkg/app/server/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,6 @@ func WithCustomBinder(b binding.Binder) config.Option {
}}
}

// WithValidateConfig sets bind config.
func WithValidateConfig(vc *binding.ValidateConfig) config.Option {
return config.Option{F: func(o *config.Options) {
o.ValidateConfig = vc
}}
}

// WithCustomValidator sets customized Binder.
func WithCustomValidator(b binding.StructValidator) config.Option {
return config.Option{F: func(o *config.Options) {
Expand Down
1 change: 0 additions & 1 deletion pkg/common/config/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ type Options struct {
ListenConfig *net.ListenConfig
BindConfig interface{}
CustomBinder interface{}
ValidateConfig interface{}
CustomValidator interface{}

// TransporterNewer is the function to create a transporter.
Expand Down
1 change: 0 additions & 1 deletion pkg/common/config/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestDefaultOptions(t *testing.T) {
assert.DeepEqual(t, registry.NoopRegistry, options.Registry)
assert.Nil(t, options.BindConfig)
assert.Nil(t, options.CustomBinder)
assert.Nil(t, options.ValidateConfig)
assert.Nil(t, options.CustomValidator)
assert.DeepEqual(t, false, options.DisableHeaderNamesNormalizing)
}
Expand Down
24 changes: 9 additions & 15 deletions pkg/route/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,6 @@ func (engine *Engine) ServeStream(ctx context.Context, conn network.StreamConn)
func (engine *Engine) initBinderAndValidator(opt *config.Options) {
// init validator
engine.validator = binding.DefaultValidator()
if opt.ValidateConfig != nil {
vConf, ok := opt.ValidateConfig.(*binding.ValidateConfig)
if !ok {
panic("validate config error")
}
engine.validator = binding.NewDefaultValidator(vConf)
}
if opt.CustomValidator != nil {
customValidator, ok := opt.CustomValidator.(binding.StructValidator)
if !ok {
Expand All @@ -574,6 +567,14 @@ func (engine *Engine) initBinderAndValidator(opt *config.Options) {
engine.validator = customValidator
}

if opt.CustomBinder != nil {
customBinder, ok := opt.CustomBinder.(binding.Binder)
if !ok {
panic("customized binder can not implement binding.Binder")
}
engine.binder = customBinder
return
}
// Init binder. Due to the existence of the "BindAndValidate" interface, the Validator needs to be injected here.
defaultBindConfig := binding.NewBindConfig()
defaultBindConfig.Validator = engine.validator
Expand All @@ -583,18 +584,11 @@ func (engine *Engine) initBinderAndValidator(opt *config.Options) {
if !ok {
panic("bind config error")
}
if bConf.Validator != nil {
if bConf.Validator == nil {
bConf.Validator = engine.validator
}
engine.binder = binding.NewDefaultBinder(bConf)
}
if opt.CustomBinder != nil {
customBinder, ok := opt.CustomBinder.(binding.Binder)
if !ok {
panic("customized binder can not implement binding.Binder")
}
engine.binder = customBinder
}
}

func NewEngine(opt *config.Options) *Engine {
Expand Down
24 changes: 12 additions & 12 deletions pkg/route/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,11 @@ func TestInitBinderAndValidator(t *testing.T) {
}
}()
opt := config.NewOptions([]config.Option{})
opt.BindConfig = &binding.BindConfig{
EnableDefaultTag: true,
}
bindConfig := binding.NewBindConfig()
bindConfig.LooseZeroMode = true
opt.BindConfig = bindConfig
binder := &mockBinder{}
opt.CustomBinder = binder
opt.ValidateConfig = &binding.ValidateConfig{}
validator := &mockValidator{}
opt.CustomValidator = validator
NewEngine(opt)
Expand All @@ -706,12 +705,11 @@ func TestInitBinderAndValidatorForPanic(t *testing.T) {
}
}()
opt := config.NewOptions([]config.Option{})
opt.BindConfig = &binding.BindConfig{
EnableDefaultTag: true,
}
bindConfig := binding.NewBindConfig()
bindConfig.LooseZeroMode = true
opt.BindConfig = bindConfig
binder := &mockBinder{}
opt.CustomBinder = binder
opt.ValidateConfig = &binding.ValidateConfig{}
nonValidator := &mockNonValidator{}
opt.CustomValidator = nonValidator
NewEngine(opt)
Expand All @@ -722,7 +720,9 @@ func TestBindConfig(t *testing.T) {
A int `query:"a"`
}
opt := config.NewOptions([]config.Option{})
opt.BindConfig = &binding.BindConfig{LooseZeroMode: false}
bindConfig := binding.NewBindConfig()
bindConfig.LooseZeroMode = false
opt.BindConfig = bindConfig
e := NewEngine(opt)
e.GET("/bind", func(c context.Context, ctx *app.RequestContext) {
var req Req
Expand All @@ -733,7 +733,9 @@ func TestBindConfig(t *testing.T) {
})
performRequest(e, "GET", "/bind?a=")

opt.BindConfig = &binding.BindConfig{LooseZeroMode: true}
bindConfig = binding.NewBindConfig()
bindConfig.LooseZeroMode = true
opt.BindConfig = bindConfig
e = NewEngine(opt)
e.GET("/bind", func(c context.Context, ctx *app.RequestContext) {
var req Req
Expand Down Expand Up @@ -773,7 +775,6 @@ func TestValidateConfig(t *testing.T) {
validateConfig.MustRegValidateFunc("f", func(args ...interface{}) error {
return fmt.Errorf("test error")
})
opt.ValidateConfig = validateConfig
e := NewEngine(opt)
e.GET("/validate", func(c context.Context, ctx *app.RequestContext) {
var req Req
Expand All @@ -793,7 +794,6 @@ func TestCustomValidator(t *testing.T) {
validateConfig.MustRegValidateFunc("d", func(args ...interface{}) error {
return fmt.Errorf("test error")
})
opt.ValidateConfig = validateConfig
opt.CustomValidator = &mockValidator{}
e := NewEngine(opt)
e.GET("/validate", func(c context.Context, ctx *app.RequestContext) {
Expand Down
1 change: 0 additions & 1 deletion pkg/route/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func performRequest(e *Engine, method, path string, headers ...header) *httptest
ctx.HTMLRender = e.htmlRender

r := protocol.NewRequest(method, path, nil)
r.PostArgs()
r.CopyTo(&ctx.Request)
for _, v := range headers {
ctx.Request.Header.Add(v.Key, v.Value)
Expand Down

0 comments on commit e2ce1ce

Please sign in to comment.