-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.go
63 lines (51 loc) · 1.37 KB
/
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
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
package validator
import (
"reflect"
"sync"
"gopkg.in/go-playground/validator.v8"
)
var EchotoolValidator = &echotoolValidator{}
type echotoolValidator struct {
once sync.Once
validate *validator.Validate
}
var _ Validator = (*echotoolValidator)(nil)
func (v *echotoolValidator) ValidateStruct(obj interface{}) error {
if kindOfData(obj) == reflect.Struct {
v.lazyInit()
return v.validate.Struct(obj)
}
return nil
}
func (v *echotoolValidator) RegisterValidation(key string, fn validator.Func) error {
v.lazyInit()
return v.validate.RegisterValidation(key, fn)
}
func (v *echotoolValidator) RegisterAliasValidation(alias, tags string) {
v.lazyInit()
v.validate.RegisterAliasValidation(alias, tags)
}
func (v *echotoolValidator) RegisterStructValidation(fn validator.StructLevelFunc, types ...interface{}) {
v.lazyInit()
v.validate.RegisterStructValidation(fn, types...)
}
func (v *echotoolValidator) RegisterCustomTypeFunc(fn validator.CustomTypeFunc, types ...interface{}) {
v.lazyInit()
v.validate.RegisterCustomTypeFunc(fn, types...)
}
func (v *echotoolValidator) lazyInit() {
v.once.Do(func() {
config := &validator.Config{
TagName: TagValid,
}
v.validate = validator.New(config)
})
}
func kindOfData(data interface{}) reflect.Kind {
rv := reflect.ValueOf(data)
kind := rv.Kind()
if kind == reflect.Ptr {
kind = rv.Elem().Kind()
}
return kind
}