-
Notifications
You must be signed in to change notification settings - Fork 14
/
Validate.go
425 lines (354 loc) · 11 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package pgo
import (
"encoding/json"
"net/http"
"regexp"
"strings"
"unicode"
"unicode/utf8"
"github.com/pinguo/pgo/Util"
)
var (
emailRe = regexp.MustCompile(`(?i)^[a-z0-9_-]+@[a-z0-9_-]+(\.[a-z0-9_-]+)+$`)
mobileRe = regexp.MustCompile(`^(\+\d{2,3} )?1[35789]\d{9}$`)
ipv4Re = regexp.MustCompile(`^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$`)
)
// validate bool value
func ValidateBool(data interface{}, name string, dft ...interface{}) *BoolValidator {
value, useDft := getValidateValue(data, name, dft...)
return &BoolValidator{name, useDft, Util.ToBool(value)}
}
// validate int value
func ValidateInt(data interface{}, name string, dft ...interface{}) *IntValidator {
value, useDft := getValidateValue(data, name, dft...)
return &IntValidator{name, useDft, Util.ToInt(value)}
}
// validate float value
func ValidateFloat(data interface{}, name string, dft ...interface{}) *FloatValidator {
value, useDft := getValidateValue(data, name, dft...)
return &FloatValidator{name, useDft, Util.ToFloat(value)}
}
// validate string value
func ValidateString(data interface{}, name string, dft ...interface{}) *StringValidator {
value, useDft := getValidateValue(data, name, dft...)
return &StringValidator{name, useDft, Util.ToString(value)}
}
// get validate value, four situations:
// 1. data: map, name: field, dft[0]: default
// 2. data: map, name: field, dft: empty
// 3. data: value, name: field, dft[0]: default
// 4. data: value, name: field, dft: empty
func getValidateValue(data interface{}, name string, dft ...interface{}) (interface{}, bool) {
var value interface{}
var useDft = false
switch v := data.(type) {
case map[string]interface{}:
if mv, ok := v[name]; ok {
value = mv
}
case map[string]string:
if mv, ok := v[name]; ok {
value = mv
}
case map[string][]string:
sliceValue, sliceOk := v[name]
if sliceOk && len(sliceValue) > 0 {
value = sliceValue[0]
}
default:
value = data
}
if value == nil {
if len(dft) == 1 {
value = dft[0]
useDft = true
} else {
panic(NewException(http.StatusBadRequest, "%s is required", name))
}
} else if strValue, strOk := value.(string); strOk {
strValue = strings.Trim(strValue, " \r\n\t")
if len(strValue) > 0 {
value = strValue
} else if len(dft) == 1 {
value = dft[0]
useDft = true
} else {
panic(NewException(http.StatusBadRequest, "%s can't be empty", name))
}
}
return value, useDft
}
// BoolValidator validator for bool value
type BoolValidator struct {
Name string
UseDft bool
Value bool
}
func (b *BoolValidator) Must(v bool) *BoolValidator {
if !b.UseDft && b.Value != v {
panic(NewException(http.StatusBadRequest, "%s must be %v", b.Name, v))
}
return b
}
func (b *BoolValidator) Do() bool {
return b.Value
}
// IntValidator validator for int value
type IntValidator struct {
Name string
UseDft bool
Value int
}
func (i *IntValidator) Min(v int) *IntValidator {
if !i.UseDft && i.Value < v {
panic(NewException(http.StatusBadRequest, "%s is too small", i.Name))
}
return i
}
func (i *IntValidator) Max(v int) *IntValidator {
if !i.UseDft && i.Value > v {
panic(NewException(http.StatusBadRequest, "%s is too large", i.Name))
}
return i
}
func (i *IntValidator) Enum(enums ...int) *IntValidator {
found := false
for _, v := range enums {
if v == i.Value {
found = true
break
}
}
if !i.UseDft && !found {
panic(NewException(http.StatusBadRequest, "%s is invalid", i.Name))
}
return i
}
func (i *IntValidator) Do() int {
return i.Value
}
// FloatValidator validator for float value
type FloatValidator struct {
Name string
UseDft bool
Value float64
}
func (f *FloatValidator) Min(v float64) *FloatValidator {
if !f.UseDft && f.Value < v {
panic(NewException(http.StatusBadRequest, "%s is too small", f.Name))
}
return f
}
func (f *FloatValidator) Max(v float64) *FloatValidator {
if !f.UseDft && f.Value > v {
panic(NewException(http.StatusBadRequest, "%s is too large", f.Name))
}
return f
}
func (f *FloatValidator) Do() float64 {
return f.Value
}
// StringValidator validator for string value
type StringValidator struct {
Name string
UseDft bool
Value string
}
func (s *StringValidator) Min(v int) *StringValidator {
if !s.UseDft && utf8.RuneCountInString(s.Value) < v {
panic(NewException(http.StatusBadRequest, "%s is too short", s.Name))
}
return s
}
func (s *StringValidator) Max(v int) *StringValidator {
if !s.UseDft && utf8.RuneCountInString(s.Value) > v {
panic(NewException(http.StatusBadRequest, "%s is too long", s.Name))
}
return s
}
func (s *StringValidator) Len(v int) *StringValidator {
if !s.UseDft && utf8.RuneCountInString(s.Value) != v {
panic(NewException(http.StatusBadRequest, "%s has invalid length", s.Name))
}
return s
}
func (s *StringValidator) Enum(enums ...string) *StringValidator {
found := false
for _, v := range enums {
if v == s.Value {
found = true
break
}
}
if !s.UseDft && !found {
panic(NewException(http.StatusBadRequest, "%s is invalid", s.Name))
}
return s
}
func (s *StringValidator) RegExp(v interface{}) *StringValidator {
var re *regexp.Regexp
if pat, ok := v.(string); ok {
re = regexp.MustCompile(pat)
} else {
re = v.(*regexp.Regexp)
}
if !s.UseDft && !re.MatchString(s.Value) {
panic(NewException(http.StatusBadRequest, "%s is invalid", s.Name))
}
return s
}
func (s *StringValidator) Filter(f func(v, n string) string) *StringValidator {
defer func() {
if v := recover(); !s.UseDft && v != nil {
panic(NewException(http.StatusBadRequest, "%s is invalid", s.Name))
}
}()
if v := f(s.Value, s.Name); len(v) > 0 {
s.Value = v
} else if !s.UseDft {
panic(NewException(http.StatusBadRequest, "%s is invalid", s.Name))
}
return s
}
func (s *StringValidator) Password() *StringValidator {
length, number, letter, special := false, false, false, false
if l := len(s.Value); 6 <= l && l <= 32 {
length = true
for i := 0; i < l; i++ {
switch {
case unicode.IsNumber(rune(s.Value[i])):
number = true
case unicode.IsLetter(rune(s.Value[i])):
letter = true
case unicode.IsPunct(rune(s.Value[i])):
special = true
case unicode.IsSymbol(rune(s.Value[i])):
special = true
}
}
}
if !s.UseDft && (!length || !number || !letter || !special) {
panic(NewException(http.StatusBadRequest, "%s is invalid password", s.Name))
}
return s
}
func (s *StringValidator) Email() *StringValidator {
if !s.UseDft && !emailRe.MatchString(s.Value) {
panic(NewException(http.StatusBadRequest, "%s is invalid email", s.Name))
}
return s
}
func (s *StringValidator) Mobile() *StringValidator {
if !s.UseDft && !mobileRe.MatchString(s.Value) {
panic(NewException(http.StatusBadRequest, "%s is invalid mobile", s.Name))
}
return s
}
func (s *StringValidator) IPv4() *StringValidator {
if !s.UseDft && !ipv4Re.MatchString(s.Value) {
panic(NewException(http.StatusBadRequest, "%s is invalid ipv4", s.Name))
}
return s
}
func (s *StringValidator) Bool() *BoolValidator {
return &BoolValidator{s.Name, s.UseDft, Util.ToBool(s.Value)}
}
func (s *StringValidator) Int() *IntValidator {
return &IntValidator{s.Name, s.UseDft, Util.ToInt(s.Value)}
}
func (s *StringValidator) Float() *FloatValidator {
return &FloatValidator{s.Name, s.UseDft, Util.ToFloat(s.Value)}
}
func (s *StringValidator) Slice(sep string) *StringSliceValidator {
validator := &StringSliceValidator{s.Name, s.UseDft, make([]string, 0)}
if len(s.Value) > 0 {
parts := strings.Split(s.Value, sep)
for _, v := range parts {
validator.Value = append(validator.Value, strings.TrimSpace(v))
}
}
return validator
}
func (s *StringValidator) Json() *JsonValidator {
validator := &JsonValidator{s.Name, s.UseDft, make(map[string]interface{})}
decoder := json.NewDecoder(strings.NewReader(s.Value))
if err := decoder.Decode(&validator.Value); !s.UseDft && err != nil {
panic(NewException(http.StatusBadRequest, "%s is invalid json", s.Name))
}
return validator
}
func (s *StringValidator) Do() string {
return s.Value
}
// IntSliceValidator validator for int slice value
type IntSliceValidator struct {
Name string
Value []int
}
func (i *IntSliceValidator) Do() []int {
return i.Value
}
// FloatSliceValidator validator for float slice value
type FloatSliceValidator struct {
Name string
Value []float64
}
func (f *FloatSliceValidator) Do() []float64 {
return f.Value
}
// StringSliceValidator validator for string slice value
type StringSliceValidator struct {
Name string
UseDft bool
Value []string
}
func (s *StringSliceValidator) Min(v int) *StringSliceValidator {
if !s.UseDft && len(s.Value) < v {
panic(NewException(http.StatusBadRequest, "%s has too few elements", s.Name))
}
return s
}
func (s *StringSliceValidator) Max(v int) *StringSliceValidator {
if !s.UseDft && len(s.Value) > v {
panic(NewException(http.StatusBadRequest, "%s has too many elements", s.Name))
}
return s
}
func (s *StringSliceValidator) Len(v int) *StringSliceValidator {
if !s.UseDft && len(s.Value) != v {
panic(NewException(http.StatusBadRequest, "%s has invalid length", s.Name))
}
return s
}
func (s *StringSliceValidator) Int() *IntSliceValidator {
validator := &IntSliceValidator{s.Name, make([]int, 0)}
for _, v := range s.Value {
validator.Value = append(validator.Value, Util.ToInt(v))
}
return validator
}
func (s *StringSliceValidator) Float() *FloatSliceValidator {
validator := &FloatSliceValidator{s.Name, make([]float64, 0)}
for _, v := range s.Value {
validator.Value = append(validator.Value, Util.ToFloat(v))
}
return validator
}
func (s *StringSliceValidator) Do() []string {
return s.Value
}
// JsonValidator validator for json value
type JsonValidator struct {
Name string
UseDft bool
Value map[string]interface{}
}
func (j *JsonValidator) Has(key string) *JsonValidator {
if v := Util.MapGet(j.Value, key); !j.UseDft && v == nil {
panic(NewException(http.StatusBadRequest, "%s json field missing", j.Name))
}
return j
}
func (j *JsonValidator) Do() map[string]interface{} {
return j.Value
}