Gin

gin参数校验

May 8, 2024
Go, Gin

第三方包 # github.com/go-playground/validator/v10 常用参数 # required //必填; len=11 //长度=11; min=3 //如果是数字,验证的是数据大小范围,最小值为3,如果是文本,验证的是最小长度为3, max=6 //如果是数字,验证的是数字最大值为6,如果是文本,验证的是最大长度为6 mail //验证邮箱 gt=3 //对于文本就是长度>=3 lt=6 //对于文本就是长度<=6 翻译中间件 # package middleware import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/locales/en" "github.com/go-playground/locales/zh" ut "github.com/go-playground/universal-translator" validator "github.com/go-playground/validator/v10" enTranslations "github.com/go-playground/validator/v10/translations/en" zhTranslations "github.com/go-playground/validator/v10/translations/zh" ) func Translations() gin.HandlerFunc { return func(c *gin.Context) { //locale := .GetHeader("Acept-Language") locale := "zh" uni := ut.New(en.New(), zh.New()) trans, _ := uni.GetTranslator(locale) v, ok := binding.Validator.Engine().(*validator.Validate) if ok { switch locale { case "zh": _ = zhTranslations. ...