From 7940dd5237eabbd6b25a0cc024bf99ba461cb4a1 Mon Sep 17 00:00:00 2001 From: gamife Date: Thu, 4 Feb 2021 10:31:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=85=E8=A3=85=E4=B8=80=E4=B8=8B=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E9=94=99=E8=AF=AF=E7=9A=84=E7=B1=BB=E5=9E=8B,?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=8C=BA=E5=88=AB=E7=94=A8=E6=88=B7=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E9=94=99=E8=AF=AF=E5=92=8C=E7=B3=BB=E7=BB=9F=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/b/b.go | 8 ++++++-- go-playground/validator/v10/errors.go | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) 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 {