-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.go
More file actions
329 lines (278 loc) · 6.32 KB
/
validators.go
File metadata and controls
329 lines (278 loc) · 6.32 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package validator
import (
"fmt"
"os"
"reflect"
"regexp"
"strconv"
"strings"
)
type validatorI interface {
validate(tagValue string, v reflect.Value) error
}
func ptr[T any](v T) *T {
return &v
}
type minV struct{}
func (minV) validate(tagValue string, v reflect.Value) error {
k := v.Kind()
if k == reflect.Pointer {
k = v.Elem().Kind()
v = v.Elem()
}
switch {
case isIntKind(k):
req, _ := strconv.ParseInt(tagValue, 0, 64)
val := v.Int()
if req > val {
return fmt.Errorf("invalid value: %d, minimum value is %s", val, tagValue)
}
case isUintKind(k):
req, _ := strconv.ParseUint(tagValue, 0, 64)
val := v.Uint()
if req > val {
return fmt.Errorf("invalid value: %d, minimum value is %s", val, tagValue)
}
case isFloatKind(k):
req, _ := strconv.ParseFloat(tagValue, 64)
val := v.Float()
if req > val {
return fmt.Errorf("invalid value: %f, minimum value is %s", val, tagValue)
}
case isLenKind(k):
req, _ := strconv.Atoi(tagValue)
val := v.Len()
if int(req) > val {
return fmt.Errorf("invalid length: %d, minimum value is %s", val, tagValue)
}
case !v.IsValid():
return nil
default:
return fmt.Errorf("invalid type for min validator")
}
return nil
}
type maxV struct{}
func (maxV) validate(tagValue string, v reflect.Value) error {
k := v.Kind()
if k == reflect.Pointer {
k = v.Elem().Kind()
v = v.Elem()
}
switch {
case isIntKind(k):
req, _ := strconv.ParseInt(tagValue, 0, 64)
val := v.Int()
if val > req {
return fmt.Errorf("invalid value: %d, maximum value is %s", val, tagValue)
}
case isUintKind(k):
req, _ := strconv.ParseUint(tagValue, 0, 64)
val := v.Uint()
if val > req {
return fmt.Errorf("invalid value: %d, maximum value is %s", val, tagValue)
}
case isFloatKind(k):
req, _ := strconv.ParseFloat(tagValue, 64)
val := v.Float()
if val > req {
return fmt.Errorf("invalid value: %f, maximum value is %s", val, tagValue)
}
case isLenKind(k):
req, _ := strconv.Atoi(tagValue)
val := v.Len()
if val > int(req) {
return fmt.Errorf("invalid length: %d, maximum value is %s", val, tagValue)
}
case !v.IsValid():
return nil
default:
return fmt.Errorf("invalid type for max validator")
}
return nil
}
type regexV struct{}
func (regexV) validate(tagValue string, v reflect.Value) error {
if !v.IsValid() {
return nil
}
if !strings.HasPrefix(tagValue, "^") && !strings.HasSuffix(tagValue, "$") {
tagValue = "^" + tagValue + "$"
}
re, err := regexp.Compile(tagValue)
if err != nil {
return fmt.Errorf("invalid regex: %s", tagValue)
}
if v.Kind() == reflect.Pointer {
if v.IsNil() {
// return fmt.Errorf("invalid value: nil")
return nil
}
v = v.Elem()
}
val := v.String()
// if len(val) == 0 {
// return fmt.Errorf("invalid value: empty string")
// }
if !re.MatchString(val) {
return fmt.Errorf("invalid value: %s does not match regex %s", val, tagValue)
}
return nil
}
type flagsV struct{}
func (flagsV) validate(tagValue string, v reflect.Value) error {
for _, flag := range strings.Split(tagValue, ",") {
switch strings.TrimSpace(flag) {
case "required":
if v.Kind() == reflect.Pointer {
if v.IsNil() {
return fmt.Errorf("required value not filled")
}
v = v.Elem()
}
if !v.IsValid() || v.IsZero() {
return fmt.Errorf("required value not filled")
}
}
}
return nil
}
type envV struct{}
func (envV) validate(tagValue string, v reflect.Value) error {
if !v.CanSet() {
panic("validator: Using `env` validator with unsettable value")
}
if !v.IsValid() {
return nil
}
fromEnv := os.Getenv(tagValue)
if fromEnv == "" {
return nil
}
var pointer bool
k := v.Kind()
if k == reflect.Pointer {
k = v.Type().Elem().Kind()
pointer = true
}
switch {
case isIntKind(k):
req, err := strconv.ParseInt(fromEnv, 0, 64)
if err != nil {
return fmt.Errorf("invalid `env` value %s", fromEnv)
}
if pointer {
v.Set(reflect.ValueOf(ptr(req)))
} else {
v.SetInt(req)
}
case isUintKind(k):
req, err := strconv.ParseUint(fromEnv, 0, 64)
if err != nil {
return fmt.Errorf("invalid `env` value %s", fromEnv)
}
if pointer {
v.Set(reflect.ValueOf(ptr(req)))
} else {
v.SetUint(req)
}
case isFloatKind(k):
req, err := strconv.ParseFloat(fromEnv, 64)
if err != nil {
return fmt.Errorf("invalid `env` value %s", fromEnv)
}
if pointer {
v.Set(reflect.ValueOf(ptr(req)))
} else {
v.SetFloat(req)
}
case k == reflect.String:
if pointer {
v.Set(reflect.ValueOf(ptr(fromEnv)))
} else {
v.SetString(fromEnv)
}
case k == reflect.Bool:
req, err := strconv.ParseBool(fromEnv)
if err != nil {
return fmt.Errorf("invalid `env` value %s", fromEnv)
}
if pointer {
v.Set(reflect.ValueOf(ptr(req)))
} else {
v.SetBool(req)
}
default:
return fmt.Errorf("invalid type for `env` validator")
}
return nil
}
type defaultV struct{}
func (defaultV) validate(tagValue string, v reflect.Value) error {
if !v.CanSet() {
panic("validator: Using `default` validator with unsettable value")
}
if !v.IsValid() {
return nil
}
if tagValue == "" {
return nil
}
var pointer bool
k := v.Kind()
if k == reflect.Pointer {
k = v.Type().Elem().Kind()
pointer = true
}
switch {
case isIntKind(k):
req, err := strconv.ParseInt(tagValue, 0, 64)
if err != nil {
return fmt.Errorf("invalid `default` value %s", tagValue)
}
if pointer {
v.Set(reflect.ValueOf(ptr(req)))
} else {
v.SetInt(req)
}
case isUintKind(k):
req, err := strconv.ParseUint(tagValue, 0, 64)
if err != nil {
return fmt.Errorf("invalid `default` value %s", tagValue)
}
if pointer {
v.Set(reflect.ValueOf(ptr(req)))
} else {
v.SetUint(req)
}
case isFloatKind(k):
req, err := strconv.ParseFloat(tagValue, 64)
if err != nil {
return fmt.Errorf("invalid `default` value %s", tagValue)
}
if pointer {
v.Set(reflect.ValueOf(ptr(req)))
} else {
v.SetFloat(req)
}
case k == reflect.String:
if pointer {
v.Set(reflect.ValueOf(ptr(tagValue)))
} else {
v.SetString(tagValue)
}
case k == reflect.Bool:
req, err := strconv.ParseBool(tagValue)
if err != nil {
return fmt.Errorf("invalid `default` value %s", tagValue)
}
if pointer {
v.Set(reflect.ValueOf(ptr(req)))
} else {
v.SetBool(req)
}
default:
return fmt.Errorf("invalid type for `default` validator")
}
return nil
}