Skip to content

Commit

Permalink
fix: set validator error
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Sep 22, 2023
1 parent a924461 commit 1af1684
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
54 changes: 54 additions & 0 deletions pkg/app/server/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,60 @@ func Test_BindHeaderNormalize(t *testing.T) {
assert.DeepEqual(t, "", result3.Header)
}

type ValidateError struct {
ErrType, FailField, Msg string
}

// Error implements error interface.
func (e *ValidateError) Error() string {
if e.Msg != "" {
return e.ErrType + ": expr_path=" + e.FailField + ", cause=" + e.Msg
}
return e.ErrType + ": expr_path=" + e.FailField + ", cause=invalid"
}

func Test_ValidatorErrorFactory(t *testing.T) {
type TestBind struct {
A string `query:"a,required"`
}

r := protocol.NewRequest("GET", "/foo", nil)
r.SetRequestURI("/foo/bar?b=20")
CustomValidateErrFunc := func(failField, msg string) error {
err := ValidateError{
ErrType: "validateErr",
FailField: "[validateFailField]: " + failField,
Msg: "[validateErrMsg]: " + msg,
}

return &err
}

validateConfig := NewValidateConfig()
validateConfig.SetValidatorErrorFactory(CustomValidateErrFunc)

var req TestBind
err := Bind(r, &req, nil)
if err == nil {
t.Fatalf("unexpected nil, expected an error")
}

type TestValidate struct {
B int `query:"b" vd:"$>100"`
}

var reqValidate TestValidate
err = Bind(r, &reqValidate, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = Validate(&reqValidate)
if err == nil {
t.Fatalf("unexpected nil, expected an error")
}
assert.DeepEqual(t, "validateErr: expr_path=[validateFailField]: B, cause=[validateErrMsg]: ", err.Error())
}

func Benchmark_Binding(b *testing.B) {
type Req struct {
Version string `path:"v"`
Expand Down
6 changes: 1 addition & 5 deletions pkg/app/server/binding/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,5 @@ func (config *ValidateConfig) MustRegValidateFunc(funcName string, fn func(args

// SetValidatorErrorFactory customizes the factory of validation error.
func (config *ValidateConfig) SetValidatorErrorFactory(validatingErrFactory func(failField, msg string) error) {
if val, ok := DefaultValidator().(*defaultValidator); ok {
val.validate.SetErrorFactory(validatingErrFactory)
} else {
panic("customized validator can not use 'SetValidatorErrorFactory'")
}
validator.SetErrorFactory(validatingErrFactory)
}
4 changes: 3 additions & 1 deletion pkg/app/server/binding/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ type defaultValidator struct {
}

func NewDefaultValidator(config *ValidateConfig) StructValidator {
return &defaultValidator{}
vd := &defaultValidator{}
vd.lazyinit()
return vd
}

// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
Expand Down

0 comments on commit 1af1684

Please sign in to comment.