-
Notifications
You must be signed in to change notification settings - Fork 10
/
validator.go
117 lines (87 loc) · 2.31 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package clop
import (
"reflect"
"sort"
"strings"
"sync"
"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
en_translations "github.com/go-playground/validator/v10/translations/en"
"github.com/go-playground/validator/v10"
)
var valid *defaultValidator = &defaultValidator{}
type defaultValidator struct {
once sync.Once
validate *validator.Validate
trans ut.Translator
}
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
if kindOfData(obj) == reflect.Struct {
v.lazyinit()
if err := v.validate.Struct(obj); err != nil {
return err
}
}
return nil
}
func (v *defaultValidator) Engine() interface{} {
v.lazyinit()
return v.validate
}
func showShortLongUsage(clopName string, tagName string) string {
var usage []string
opt := strings.Split(clopName, ";")
for _, o := range opt {
o = strings.TrimSpace(o)
if len(o) == 0 {
continue
}
switch o {
case "short":
usage = append(usage, "-"+strings.ToLower(string(tagName[0])))
continue
case "long":
if len(tagName) > 1 {
longName, _ := gnuOptionName(tagName)
usage = append(usage, "--"+longName)
}
continue
}
if o[0] != '-' {
continue
}
usage = append(usage, o)
}
sort.Slice(usage, func(i, j int) bool {
return len(usage[i]) < len(usage[j])
})
return strings.Join(usage, ";")
}
func (v *defaultValidator) lazyinit() {
v.once.Do(func() {
en := en.New()
uni := ut.New(en, en)
v.validate = validator.New()
v.validate.SetTagName("valid")
v.trans, _ = uni.GetTranslator("en")
en_translations.RegisterDefaultTranslations(v.validate, v.trans)
v.validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
return "error: " + showShortLongUsage(fld.Tag.Get("clop"), fld.Name)
})
v.validate.RegisterTranslation("required", v.trans, func(ut ut.Translator) error {
return ut.Add("required", "{0} must have a value!", true) // see universal-translator for details
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("required", fe.Field())
return t
})
// add any custom validations etc. here
})
}
func kindOfData(data interface{}) reflect.Kind {
value := reflect.ValueOf(data)
valueType := value.Kind()
if valueType == reflect.Ptr {
valueType = value.Elem().Kind()
}
return valueType
}