-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
189 lines (162 loc) · 4.36 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package typutil
import (
"fmt"
"reflect"
"strings"
"sync"
)
type validatorObject struct {
fnc reflect.Value
arg reflect.Type
}
var (
validators = map[string]*validatorObject{}
validatorsLk sync.RWMutex
)
// A validator func is a function that takes one argument (the value being validated) and returns either nil or an error
// If the function accepts a modifiable value (a pointer for example) it might be possible to modify the value during validation
// SetValidator sets the given function as validator with the given name. This should be typically called in init()
func SetValidator[T any](validator string, fnc func(T) error) {
vfnc := reflect.ValueOf(fnc)
argt := reflect.TypeOf((*T)(nil)).Elem()
validatorsLk.Lock()
defer validatorsLk.Unlock()
validators[validator] = &validatorObject{fnc: vfnc, arg: argt}
}
func SetValidatorArgs(validator string, fnc any) {
vfnc := reflect.ValueOf(fnc)
if vfnc.Kind() != reflect.Func {
panic("not a function")
}
t := vfnc.Type()
if t.NumIn() < 1 {
panic("validator function must accept at least one argument")
}
argt := t.In(0)
validators[validator] = &validatorObject{fnc: vfnc, arg: argt}
}
// getValidators returns the validator objects for a given validator tag value. Multiple validators can be defined
func getValidators(s string) ([]*validatorObject, [][]reflect.Value, error) {
if s == "" {
return nil, nil, nil
}
a := strings.Split(s, ",")
res := make([]*validatorObject, 0, len(a))
res2 := make([][]reflect.Value, 0, len(a))
validatorsLk.RLock()
defer validatorsLk.RUnlock()
for _, v := range a {
p := strings.IndexByte(v, '=')
a := ""
// allow arguments after =, such as maxlength=2
if p != -1 {
a = v[p+1:]
v = v[:p]
}
o, ok := validators[v]
if !ok {
return res, res2, fmt.Errorf("validator not found: %s", a)
}
res = append(res, o)
res2 = append(res2, o.convertArgs(a))
}
return res, res2, nil
}
type fieldValidator struct {
fld int // field index
name string // field name
vals []*validatorObject
args [][]reflect.Value // extra validator param, if any
}
type structValidator []*fieldValidator
var (
validatorCache = make(map[reflect.Type]structValidator)
validatorCacheLk sync.Mutex
)
func getValidatorForType(t reflect.Type) structValidator {
validatorCacheLk.Lock()
defer validatorCacheLk.Unlock()
if t.Kind() != reflect.Struct {
return nil
}
val, ok := validatorCache[t]
if ok {
return val
}
n := t.NumField()
for i := 0; i < n; i++ {
f := t.Field(i)
vals, args, err := getValidators(f.Tag.Get("validator"))
if err != nil {
// skip
continue
}
if len(vals) == 0 {
continue
}
val = append(val, &fieldValidator{fld: i, name: f.Name, vals: vals, args: args})
}
validatorCache[t] = val
return val
}
func (sv structValidator) validate(val reflect.Value) error {
var err error
for _, vd := range sv {
f := val.Field(vd.fld).Addr()
for n, sub := range vd.vals {
err = sub.runReflectValue(f, vd.args[n])
if err != nil {
return fmt.Errorf("on field %s: %w", vd.name, err)
}
}
}
return nil
}
// Validate accept any struct as argument and returns if the struct is valid. The parameter should be a pointer
// to the struct so validators can edit values.
func Validate(obj any) error {
v := reflect.ValueOf(obj)
if v.Kind() != reflect.Pointer {
return ErrStructPtrRequired
}
for v.Kind() == reflect.Pointer {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return ErrStructPtrRequired
}
return getValidatorForType(v.Type()).validate(v)
}
func (v *validatorObject) runReflectValue(val reflect.Value, args []reflect.Value) error {
valT := reflect.New(v.arg)
err := AssignReflect(valT, val)
if err != nil {
return err
}
res := v.fnc.Call(append([]reflect.Value{valT.Elem()}, args...))
if res[0].IsNil() {
return nil
}
return res[0].Interface().(error)
}
func (v *validatorObject) convertArgs(args string) []reflect.Value {
t := v.fnc.Type()
if t.NumIn() <= 1 {
// 0 shouldn't happen, 1 means there are no extra args to take into account
return nil
}
argsArray := strings.Split(args, ",")
extraCnt := t.NumIn() - 1
if len(argsArray) < extraCnt {
// not enough args
extraCnt = len(argsArray)
}
res := make([]reflect.Value, 0, extraCnt)
for i := 0; i < extraCnt; i++ {
argt := t.In(i + 1)
v := reflect.New(argt).Elem()
AssignReflect(v, reflect.ValueOf(argsArray[i]))
res = append(res, v)
}
return res
}