-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
custom_validator.go
33 lines (30 loc) · 1.41 KB
/
custom_validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package request
import (
"context"
"regexp"
validatorPkg "github.com/go-playground/validator/v10"
"github.com/webx-top/com"
"github.com/webx-top/validation"
"github.com/webx-top/validator"
)
func init() {
validator.RegisterCustomValidation(`username`, func(_ context.Context, f validatorPkg.FieldLevel) bool {
return com.IsUsername(f.Field().String())
}, validator.OptTranslations(map[string]*validator.Translation{
`zh`: {Text: `输入的用户名无效 (用户名只能由字母、数字、下划线或汉字组成)`},
`en`: {Text: `invalid username. {0} can only consist of letters, numbers, underscores or Chinese characters`},
}))
validator.RegisterCustomValidation(`alphanum_`, func(_ context.Context, f validatorPkg.FieldLevel) bool {
return com.IsAlphaNumericUnderscore(f.Field().String())
}, validator.OptTranslations(map[string]*validator.Translation{
`zh`: {Text: `{0} 的值无效 (只能由字母、数字或下划线组成)`},
`en`: {Text: `invalid value. {0} can only consist of letters, numbers or underscores`},
}))
regexpMobile := regexp.MustCompile(validation.DefaultRule.Mobile)
validator.RegisterCustomValidation(`mobile`, func(_ context.Context, f validatorPkg.FieldLevel) bool {
return regexpMobile.MatchString(f.Field().String())
}, validator.OptTranslations(map[string]*validator.Translation{
`zh`: {Text: `手机号码无效`},
`en`: {Text: `Invalid value for parameter {0}`},
}))
}