go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.79k stars 1.32k forks source link

grpc uses ut.Translator to report an error #1051

Closed HalcyonDaysssss closed 1 year ago

HalcyonDaysssss commented 1 year ago
image

`import ( CN_ZH "github.com/go-playground/locales/zh" ut "github.com/go-playground/universal-translator" "github.com/go-playground/validator/v10" zhTranslations "github.com/go-playground/validator/v10/translations/zh" "reflect" )

var ( Validate *validator.Validate

// Validate/v10 全局验证器
uni ut.Translator

)

// 初始化Validate/v10国际化 func init() {

zh := CN_ZH.New()
uni := ut.New(zh, zh)
trans, _ := uni.GetTranslator("zh")

Validate = validator.New()

//通过label标签返回自定义错误内容
Validate.RegisterTagNameFunc(func(field reflect.StructField) string {
    label := field.Tag.Get("label")
    if label == "" {
        return field.Name
    }
    return label
})
zhTranslations.RegisterDefaultTranslations(Validate, trans)

//自定义required_if错误内容
Validate.RegisterTranslation("required_if", trans, func(ut ut.Translator) error {
    return ut.Add("required_if", "{0}为必填字段!", false) // see universal-translator for details
}, func(ut ut.Translator, fe validator.FieldError) string {
    t, _ := ut.T("required_if", fe.Field())
    return t
})

}

// 检验并返回检验错误信息 func Translate(err error) (errMsg string) { errs := err.(validator.ValidationErrors) for _, err := range errs { errMsg = err.Translate(uni) } return }`