-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_test.go
520 lines (486 loc) · 13.2 KB
/
validate_test.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package validator
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
const verboseLogs = false
func TestLexer(t *testing.T) {
types := []tokenType{typeFunction, typeColon, typeSpace, typeString, typeComma, typeSpace, typeNumber, typeSpace, typeAnd, typeSpace, typeFunction, typeSpace, typeAnd, typeSpace, typeFunction, typeSpace, typeOr, typeSpace, typeFunction, typeSpace, typeAnd, typeSpace, typeFunction, typeSpace, typeOr, typeSpace, typeOpenParen, typeFunction, typeSpace, typeAnd, typeSpace, typeFunction, typeCloseParen, typeSpace, typeOr, typeSpace, typeFunction, typeColon, typeSpace, typeString, typeComma, typeSpace, typeNumber, typeSpace, typeAnd, typeSpace, typeFunction, typeColon, typeSpace, typeBool, typeEOF}
values := []string{"func", ":", " ", "'param1'", ",", " ", "2", " ", "&", " ", "empty", " ", "&", " ", "i", " ", "|", " ", "one", " ", "&", " ", "two", " ", "|", " ", "(", "three", " ", "&", " ", "four", ")", " ", "|", " ", "five", ":", " ", "'six'", ",", " ", "02.0", " ", "&", " ", "seven", ":", " ", "false", ""}
l := newLexer("func: 'param1', 2 & empty & i | one & two | (three & four) | five: 'six', 02.0 & seven: false")
l.debug = verboseLogs
for i := 0; true; i++ {
token := l.Next()
if token.typ != types[i] {
t.Log(token)
t.Fatalf("token[%d].typ: '%s' != '%s'", i, token.typ, types[i])
return
} else if token.val != values[i] {
t.Fatalf("token[%d].val: '%+v' != '%+v'", i, token.val, values[i])
return
}
if token.typ == typeEOF {
break
}
}
for _, s := range []string{
"func: param1, 2",
"f & t",
"t & (f | t | f)",
"t & (f | f | t) & t",
} {
t.Run(s, func(t *testing.T) {
l = newLexer(s)
for token := l.Next(); token.typ != typeEOF; token = l.Next() {
if token.typ == typeError {
t.Fatal(token.val)
break
}
}
})
}
}
func TestParser(t *testing.T) {
parser := newParser()
parser.debug = verboseLogs
tr := func(ps *RuleParams) error {
return nil
}
fl := func(ps *RuleParams) error {
return fmt.Errorf("error called")
}
var params []string
rules := map[string]Rule{
"t": tr,
"f": fl,
"a": tr,
"b": tr,
"c": fl,
"d": fl,
"e": tr,
"func": func(ps *RuleParams) error {
params = ps.Params
return nil
},
}
// test function
for _, s := range []string{
"func: 'hello world', 2",
} {
if isValid := t.Run(s, func(t *testing.T) {
if parsed, err := parser.parse(s, rules); err != nil {
t.Fatal(err)
} else if err := parsed.execute(&RuleParams{}); err != nil {
t.Fatalf("execution failed: %s", err)
} else {
a := assert.New(t)
a.Equal(params, []string{`'hello world'`, `2`})
}
}); !isValid {
t.Fatal("failed")
return
}
}
// resolves to true
for _, s := range []string{
"t & t",
"t & (f | t | f)",
"a & (b | c | d) & e",
} {
if isValid := t.Run(s, func(t *testing.T) {
if parsed, err := parser.parse(s, rules); err != nil {
t.Fatalf("parse failed: %s", err)
} else if err := parsed.execute(&RuleParams{}); err != nil {
t.Fatalf("execution failed: %s", err)
}
}); !isValid {
t.Fatal("failed")
return
}
}
// resolves to false
for _, s := range []string{
"t & f",
"t & (f | t & f)",
"t & (f | f & t) & t",
"t & (f | f | t) & f",
} {
if isValid := t.Run(s, func(t *testing.T) {
if parsed, err := parser.parse(s, rules); err != nil {
t.Fatalf("parse failed: %s", err)
} else if err := parsed.execute(&RuleParams{}); err == nil {
t.Fatal("there should be an error returned")
}
}); !isValid {
t.Fatal("failed")
return
}
}
// parse with bad bad syntax
for _, s := range []string{
"t f",
"t (f | t & f)",
"t & (f | f & t) t",
"t & (f | f t) & f",
"t & (f | f | t & f",
"t & : f",
} {
if isValid := t.Run(s, func(t *testing.T) {
if _, err := parser.parse(s, rules); err == nil {
t.Fatal("should return a parse error")
}
}); !isValid {
t.Fatal("failed")
return
}
}
}
func TestValidator(t *testing.T) {
debug = verboseLogs
if pass := t.Run("test tag name parsing", func(t *testing.T) {
// create a rule that always fails
rules := Rules{
"fail": func(*RuleParams) error {
return errors.New("this will always fail")
},
}
// create a validator with a default tag and a different tag with this rule
v, v1 := New(&Config{
Rules: rules,
}), New(&Config{
Tag: "test",
Rules: rules,
})
// create structs with a default tag and a test tag
var s struct {
Field string `validate:"fail"`
}
var s1 struct {
Field string `test:"fail"`
}
// make sure that only the correct tag is parsed
a := assert.New(t)
a.NotNil(v.Validate(&s))
a.Nil(v.Validate(&s1))
a.Nil(v1.Validate(&s))
a.NotNil(v1.Validate(&s1))
}) && t.Run("test and / or logic", func(t *testing.T) {
// create a rule that always passes and a rule that always fails fails
rules := Rules{
"pass": func(*RuleParams) error {
return nil
},
"fail": func(*RuleParams) error {
return errors.New("this will always fail")
},
}
// create structs with a default tag and a test tag
var s1 struct {
Field string `validate:"fail | pass | fail & pass"`
}
var s2 struct {
Field string `validate:"fail | fail | pass & fail"`
}
var s3 struct {
Field string `validate:"pass | fail | fail & pass"`
}
var s4 struct {
Field string `validate:"false | pass | false & pass & false | pass & false"`
}
// create a validator with a default tag and a different tag with this rule
v := New(&Config{
Rules: rules,
})
// make sure that only the correct tag is parsed
a := assert.New(t)
a.Nil(v.Validate(&s1))
a.NotNil(v.Validate(&s2))
a.Nil(v.Validate(&s3))
a.NotNil(v.Validate(&s4))
}) && t.Run("multiple errors are returned", func(t *testing.T) {
// create a validator with a default tag and a different tag with this rule
v := New(&Config{
Rules: Rules{
"fail": func(*RuleParams) error {
return errors.New("fail")
},
},
})
var s1 struct {
One string `validate:"fail"`
Two string `validate:"fail"`
Three string `validate:"fail"`
}
a := assert.New(t)
a.EqualError(v.Validate(&s1), `["fail","fail","fail"]`)
}) && t.Run("checks for bad syntax in the validate tag", func(t *testing.T) {
type s struct {
String string `json:"a" validate:"required & : empty"`
}
a := assert.New(t)
v := New()
if passed := a.EqualError(v.CheckSyntax(&s{}), `["bad ':' at 11"]`); !passed {
t.FailNow()
}
}); !pass {
t.Fatal("tests failed!")
}
}
func TestRules(t *testing.T) {
debug = verboseLogs
if pass := t.Run("required", func(t *testing.T) {
var s1 struct {
Field string `validate:"required"`
}
s2 := struct {
Field string `validate:"required"`
}{
Field: "populated",
}
v := New()
a := assert.New(t)
a.EqualError(v.Validate(&s1), `["'Field' is required"]`)
a.Nil(v.Validate(&s2))
}) && t.Run("empty", func(t *testing.T) {
var s1 struct {
Field string `validate:"empty | fail"`
}
s2 := struct {
Field string `validate:"empty | fail"`
}{
Field: "populated",
}
v := New(&Config{
Rules: Rules{
"empty": Empty,
"fail": func(*RuleParams) error {
return errors.New("this will always fail")
},
},
})
a := assert.New(t)
a.Nil(v.Validate(&s1))
a.EqualError(v.Validate(&s2), `["this will always fail"]`)
}) && t.Run("email", func(t *testing.T) {
var s1 struct {
EmailAddress string `validate:"email"`
}
var s2 struct {
EmailAddress uint `validate:"email"`
}
v := New(&Config{
Rules: Rules{
"email": Email,
},
})
a := assert.New(t)
// empty emails fail
a.EqualError(v.Validate(&s1), `["'EmailAddress' must be a valid email address"]`)
// incorrect emails fail
s1.EmailAddress = "notAnEmail@"
a.EqualError(v.Validate(&s1), `["'EmailAddress' must be a valid email address"]`)
// empty emails fail
s1.EmailAddress = "[email protected]"
a.Nil(v.Validate(&s1))
// syntax check
a.EqualError(v.CheckSyntax(&s2), "the email tag must be applied to a string")
}) && t.Run("password", func(t *testing.T) {
var s1 struct {
Password string `validate:"password"`
}
var s2 struct {
Password []byte `validate:"password"`
}
v := New(&Config{
Rules: Rules{
"password": Password,
},
})
a := assert.New(t)
// empty emails fail
a.EqualError(v.Validate(&s1), `["'Password' must be a at least 6 characters long and contain at least one number or special character (eg. @!#)"]`)
// password without special characters fails
s1.Password = "notavalidpassword"
a.EqualError(v.Validate(&s1), `["'Password' must be a at least 6 characters long and contain at least one number or special character (eg. @!#)"]`)
// password that is too short fails
s1.Password = "abc12"
a.EqualError(v.Validate(&s1), `["'Password' must be a at least 6 characters long and contain at least one number or special character (eg. @!#)"]`)
// valid password succeeds
s1.Password = "abc123"
a.Nil(v.Validate(&s1))
// syntax check
a.EqualError(v.CheckSyntax(&s2), "the password tag must be applied to a string")
}) && t.Run("number", func(t *testing.T) {
var s1 struct {
Number string `validate:"number"`
}
var s2 struct {
Number string `validate:"number:2,4"`
}
var s3 struct {
Number int `validate:"number:2,4"`
}
var s4 struct {
Int int `validate:"number"`
Int8 int8 `validate:"number"`
Int16 int16 `validate:"number"`
Int32 int32 `validate:"number"`
Int64 int64 `validate:"number"`
Uint uint `validate:"number"`
Uint8 uint8 `validate:"number"`
Uint16 uint16 `validate:"number"`
Uint32 uint32 `validate:"number"`
Uint64 uint64 `validate:"number"`
Float32 float32 `validate:"number"`
Float64 float64 `validate:"number"`
}
v := New(&Config{
Rules: Rules{
"number": Number,
},
})
a := assert.New(t)
// empty strings fail
a.NotNil(v.Validate(&s1))
// incorrect string number fails
s1.Number = "not a number"
a.NotNil(v.Validate(&s1))
// strings that is only numbers works
s1.Number = "12345"
a.Nil(v.Validate(&s1))
// digit range
s2.Number = "0"
a.EqualError(v.Validate(&s2), `["'Number' must be 2 to 4 digits"]`)
s2.Number = "000"
a.Nil(v.Validate(&s2))
s2.Number = "00000"
a.EqualError(v.Validate(&s2), `["'Number' must be 2 to 4 digits"]`)
// int range
s3.Number = 1
a.EqualError(v.Validate(&s3), `["'Number' must be 2 to 4"]`)
s3.Number = 3
a.Nil(v.Validate(&s3))
s3.Number = 5
a.EqualError(v.Validate(&s3), `["'Number' must be 2 to 4"]`)
// all numbers are valid
a.Nil(v.Validate(&s4))
}) && t.Run("eq", func(t *testing.T) {
type s struct {
Uint uint `json:"a" validate:"eq:1,2,3"`
Int int `json:"b" validate:"eq:1,2,3"`
String string `json:"c" validate:"eq:1,2,3"`
}
s1 := s{
1, 2, "3",
}
var s2 s
var s3 struct {
Uint uint `json:"a" validate:"eq"`
}
v := New()
a := assert.New(t)
a.Nil(v.Validate(&s1))
a.EqualError(v.Validate(&s2), `["'a' must equal '1', '2' or '3'","'b' must equal '1', '2' or '3'","'c' must equal '1', '2' or '3'"]`)
a.EqualError(v.CheckSyntax(&s3), "eq requires at least one parameter")
}) && t.Run("xor", func(t *testing.T) {
type s struct {
Uint uint `json:"a" validate:"xor:Int,String"`
Int int `json:"b"`
String string `json:"c"`
}
s1 := s{
Uint: 1,
}
s2 := s{
Int: 1,
}
s3 := s{
String: "1",
}
s4 := s{
Uint: 1,
Int: 1,
String: "1",
}
s5 := s{}
var s6 struct {
Uint uint `json:"a" validate:"xor:Int,String"`
}
v := New()
a := assert.New(t)
a.Nil(v.Validate(&s1))
a.Nil(v.Validate(&s2))
a.Nil(v.Validate(&s3))
a.EqualError(v.Validate(&s4), `["either 'a', 'b' or 'c' must be set"]`)
a.EqualError(v.Validate(&s5), `["either 'a', 'b' or 'c' must be set"]`)
a.EqualError(v.CheckSyntax(&s6), "'.Int' is not a valid field")
}) && t.Run("or", func(t *testing.T) {
type s struct {
Uint uint `json:"a" validate:"or:Int,String"`
Int int `json:"b"`
String string `json:"c"`
}
s1 := s{
Uint: 1,
}
s2 := s{
Int: 1,
}
s3 := s{
String: "1",
}
s4 := s{
Uint: 1,
Int: 1,
String: "1",
}
s5 := s{}
var s6 struct {
Uint uint `json:"a" validate:"or:Int,String"`
}
v := New()
a := assert.New(t)
a.Nil(v.Validate(&s1))
a.Nil(v.Validate(&s2))
a.Nil(v.Validate(&s3))
a.Nil(v.Validate(&s4))
a.EqualError(v.Validate(&s5), `["either 'a', 'b' and/or 'c' must be set"]`)
a.EqualError(v.CheckSyntax(&s6), "'.Int' is not a valid field")
}) && t.Run("and", func(t *testing.T) {
type s struct {
Uint uint `json:"a" validate:"and:Int,String"`
Int int `json:"b"`
String string `json:"c"`
}
s1 := s{
Uint: 1,
}
s2 := s{
Int: 1,
}
s3 := s{
String: "1",
}
s4 := s{
Uint: 1,
Int: 1,
String: "1",
}
s5 := s{}
var s6 struct {
Uint uint `json:"a" validate:"or:Int,String"`
}
v := New()
a := assert.New(t)
a.EqualError(v.Validate(&s1), `["'a', 'b' and 'c' must be set"]`)
a.EqualError(v.Validate(&s2), `["'a', 'b' and 'c' must be set"]`)
a.EqualError(v.Validate(&s3), `["'a', 'b' and 'c' must be set"]`)
a.Nil(v.Validate(&s4))
a.EqualError(v.Validate(&s5), `["'a', 'b' and 'c' must be set"]`)
a.EqualError(v.CheckSyntax(&s6), "'.Int' is not a valid field")
}); !pass {
t.Fatal("error")
}
}