feat: 增加label
continuous-integration/drone/push Build is passing Details

master
icechen 2022-01-13 02:33:44 +08:00
parent 372d70969b
commit d3b4e8f5e5
3 changed files with 11 additions and 6 deletions

View File

@ -11,7 +11,7 @@ import (
func LoginHandle(c *fiber.Ctx) error {
var p = struct {
Code string `json:"code" validate:"required"`
Email string `json:"email" validate:"email"`
Email string `json:"email" label:"邮箱" validate:"email"`
}{}
err := Bind(c, &p)
if err != nil {

View File

@ -24,11 +24,16 @@ func init() {
panic(fmt.Errorf("uni.GetTranslator(%s) failed", "zh"))
}
Validate.RegisterTagNameFunc(func(field reflect.StructField) string {
label := field.Tag.Get("json")
if label == "" {
return field.Name
addWarp := func(tag string) string {
return "[" + tag + "]"
}
return label
if labelTag := field.Tag.Get("label"); labelTag != "" {
return addWarp(labelTag)
}
if jsonTag := field.Tag.Get("json"); jsonTag != "" {
return addWarp(jsonTag)
}
return addWarp(field.Name)
})
err := zh_translations.RegisterDefaultTranslations(Validate, trans)
if err != nil {

View File

@ -41,7 +41,7 @@ func middlewareResp(c *fiber.Ctx) error {
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data"`
Data json.RawMessage `json:"data,omitempty"`
}
func middlewareError(ctx *fiber.Ctx, err error) error {