-
Notifications
You must be signed in to change notification settings - Fork 3
/
validate.go
64 lines (53 loc) · 1.37 KB
/
validate.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
package validateKit
import (
"github.com/go-playground/validator/v10"
"github.com/richelieu-yang/chimera/v2/src/core/sliceKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"github.com/richelieu-yang/chimera/v2/src/netKit"
)
// New
/*
PS:
(1) 默认的tag name: "validate";
(2) Gin使用的tag name: "binding".
@param tagNameArgs 不传参的话,将采用默认的tag name
*/
func New(tagNameArgs ...string) *validator.Validate {
v := validator.New(validator.WithRequiredStructEnabled())
tagName := sliceKit.GetFirstItemWithDefault("", tagNameArgs...)
if strKit.IsNotEmpty(tagName) {
v.SetTagName(tagName)
}
if err := registerDefaultValidation(v); err != nil {
panic(err)
}
return v
}
// registerDefaultValidation 注册默认的验证器(s).
func registerDefaultValidation(v *validator.Validate) error {
tag := "port"
err := v.RegisterValidation(tag, func(fl validator.FieldLevel) bool {
field := fl.Field()
return netKit.IsValidPort(field)
})
if err != nil {
return err
}
return nil
}
// Struct 验证结构体.
/*
@param s 如果为nil,将返回error(e.g. validator: (nil *main.User))
*/
func Struct(s interface{}) error {
if validatable, ok := s.(Validatable); ok {
return validatable.Validate()
}
v := New()
return v.Struct(s)
}
// Var 验证变量.
func Var(field interface{}, tag string) error {
v := New()
return v.Var(field, tag)
}