diff --git a/gin/b/b.go b/gin/b/b.go index 784d14b..e26f7c3 100644 --- a/gin/b/b.go +++ b/gin/b/b.go @@ -11,7 +11,7 @@ type ValidError struct { ErrString string } -func (e *ValidError) Error() string { +func (e ValidError) Error() string { return e.ErrString } @@ -32,7 +32,11 @@ func ShouldBindWith(req *http.Request, obj interface{}, b binding.Binding) error // 非validator.ValidationErrors类型错误直接返回 return err } - return errs.Translate(binding.ValidTrans) + err0 := errs.Translate(binding.ValidTrans) + if err0 != nil { + return ValidError{ErrString: err0.Error()} + } + return nil } func ShouldBindJSON(req *http.Request, obj interface{}) error { return ShouldBindWith(req, obj, binding.JSON) diff --git a/go-playground/validator/v10/errors.go b/go-playground/validator/v10/errors.go index 7444849..cf89f46 100644 --- a/go-playground/validator/v10/errors.go +++ b/go-playground/validator/v10/errors.go @@ -86,7 +86,7 @@ type TransValidError struct { func (e TransValidError) Error() string { return e.ErrorString } -func (ve ValidationErrors) Translate(ut ut.Translator) TransValidError { +func (ve ValidationErrors) Translate(ut ut.Translator) error { var result TransValidError var fe *fieldError if len(ve) == 0 {