ShouldBind才会报翻译的错误, 现将翻译的代码移动到 ShouldBindWith这个最根本的函数下

This commit is contained in:
gamife 2020-12-05 16:19:04 +08:00
parent bf312432ee
commit 0d83cfc042
1 changed files with 6 additions and 5 deletions

View File

@ -21,7 +21,12 @@ func ShouldBind(req *http.Request, obj interface{}) error {
return err return err
} }
b := binding.Default(req.Method, content) b := binding.Default(req.Method, content)
err = ShouldBindWith(req, obj, b) return ShouldBindWith(req, obj, b)
}
func ShouldBindWith(req *http.Request, obj interface{}, b binding.Binding) error {
err := b.Bind(req, obj)
errs, ok := err.(validator.ValidationErrors) errs, ok := err.(validator.ValidationErrors)
if !ok { if !ok {
// 非validator.ValidationErrors类型错误直接返回 // 非validator.ValidationErrors类型错误直接返回
@ -29,10 +34,6 @@ func ShouldBind(req *http.Request, obj interface{}) error {
} }
return errs.Translate(binding.ValidTrans) return errs.Translate(binding.ValidTrans)
} }
func ShouldBindWith(req *http.Request, obj interface{}, b binding.Binding) error {
return b.Bind(req, obj)
}
func ShouldBindJSON(req *http.Request, obj interface{}) error { func ShouldBindJSON(req *http.Request, obj interface{}) error {
return ShouldBindWith(req, obj, binding.JSON) return ShouldBindWith(req, obj, binding.JSON)
} }