-
Notifications
You must be signed in to change notification settings - Fork 0
/
xiren.go
77 lines (63 loc) · 1.66 KB
/
xiren.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package xiren
import (
`github.com/go-playground/locales/en`
`github.com/go-playground/locales/zh`
`github.com/go-playground/universal-translator`
`github.com/go-playground/validator/v10`
enLang `github.com/go-playground/validator/v10/translations/en`
zhLang `github.com/go-playground/validator/v10/translations/zh`
)
var (
_ = Struct
_ = Var
_ = New
validate *validator.Validate
translator *ut.UniversalTranslator
)
// Struct 验证结构体
func Struct(obj interface{}, opts ...option) (err error) {
_options := defaultOptions()
for _, opt := range opts {
opt.apply(_options)
}
if errs := validate.Struct(obj); nil != errs {
err = Localization(_options.lang, errs.(validator.ValidationErrors))
}
return
}
// Var 验证变量
func Var(field interface{}, tag string, opts ...option) (err error) {
_options := defaultOptions()
for _, opt := range opts {
opt.apply(_options)
}
if errs := validate.Var(field, tag); nil != errs {
err = Localization(_options.lang, errs.(validator.ValidationErrors))
}
return
}
// New 创建新的验证器
func New() *validator.Validate {
return validate
}
// 创建内置验证器
// 单例设计模式
func newValidate() (err error) {
validate = validator.New()
translator = ut.New(en.New(), zh.New())
english, _ := translator.GetTranslator(`en`)
if err = enLang.RegisterDefaultTranslations(validate, english); nil != err {
return
}
chinese, _ := translator.GetTranslator(`zh`)
if err = zhLang.RegisterDefaultTranslations(validate, chinese); nil != err {
return
}
if err = initValidator(validate); nil != err {
return
}
if err = initTranslation(validate, chinese); nil != err {
return
}
return
}