-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
325 lines (306 loc) · 8.43 KB
/
validator_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
package structvalidator
import (
"log"
"testing"
)
type Test1 struct {
FirstName string `validation:"req lenmin:5 lenmax:25"`
LastName string `validation:"req lenmin:2 lenmax:50"`
Age int `validation:"req valmin:18 valmax:150"`
Price int `validation:"req valmin:0 valmax:9999"`
PostCode string `validation:"req" validation_regexp:"^[0-9][0-9]-[0-9][0-9][0-9]$"`
Email string `validation:"req email"`
BelowZero int `validation:"valmin:-6 valmax:-2"`
DiscountPrice int `validation:"valmin:0 valmax:8000"`
Country string `validation_regexp:"^[A-Z][A-Z]$"`
County string `validation:"lenmax:40 uitextarea"`
}
type Test2 struct {
FirstName string `mytag:"req lenmin:5 lenmax:25"`
LastName string `mytag:"req lenmin:2 lenmax:50"`
Age int `mytag:"req valmin:18 valmax:150"`
Price int `mytag:"req valmin:0 valmax:9999"`
PostCode string `mytag:"req" mytag_regexp:"^[0-9][0-9]-[0-9][0-9][0-9]$"`
Email string `mytag:"req email"`
BelowZero int `mytag:"valmin:-6 valmax:-2"`
DiscountPrice int `mytag:"valmin:0 valmax:8000"`
Country string `mytag_regexp:"^[A-Z][A-Z]$"`
County string `mytag:"lenmax:40"`
}
type Test3 struct {
ZeroMin int `mytag:"valmin:0 valmax:5"`
ZeroMax int `mytag:"valmax:0"`
ZeroBoth int `mytag:"valmin:0 valmax:0"`
NotZero int `mytag:"valmin:4 valmax:6"`
OnlyMin int `mytag:"valmin:3"`
OnlyMax int `mytag:"valmax:7"`
}
type Test4 struct {
PrimaryEmail string ``
}
func TestWithDefaultValues(t *testing.T) {
s := Test1{}
expectedBool := false
expectedFailedFields := map[string]int{
"FirstName": FailEmpty,
"LastName": FailEmpty,
"Age": FailValMin,
"PostCode": FailEmpty,
"Email": FailEmpty,
"Country": FailRegexp,
"BelowZero": FailValMax,
}
opts := &ValidationOptions{}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestWithInvalidValues(t *testing.T) {
s := Test1{
FirstName: "123456789012345678901234567890",
LastName: "b",
Age: 15,
Price: 0,
PostCode: "AA123",
Email: "invalidEmail",
BelowZero: 8,
DiscountPrice: 9999,
Country: "Tokelau",
County: "",
}
expectedBool := false
expectedFailedFields := map[string]int{
"FirstName": FailLenMax,
"LastName": FailLenMin,
"Age": FailValMin,
"PostCode": FailRegexp,
"Email": FailEmail,
"BelowZero": FailValMax,
"DiscountPrice": FailValMax,
"Country": FailRegexp,
}
opts := &ValidationOptions{}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestWithValidValues(t *testing.T) {
s := Test1{
FirstName: "Johnny",
LastName: "Smith",
Age: 35,
Price: 0,
PostCode: "43-155",
Email: "[email protected]",
BelowZero: -4,
DiscountPrice: 8000,
Country: "GB",
County: "Enfield",
}
expectedBool := true
expectedFailedFields := map[string]int{}
opts := &ValidationOptions{}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestWithInvalidValuesAndFieldRestriction(t *testing.T) {
s := Test1{
FirstName: "123456789012345678901234567890",
LastName: "b",
Age: 15,
Price: 0,
PostCode: "AA123",
Email: "invalidEmail",
BelowZero: 8,
DiscountPrice: 9999,
Country: "Tokelau",
County: "",
}
expectedBool := false
expectedFailedFields := map[string]int{
"FirstName": FailLenMax,
"LastName": FailLenMin,
}
opts := &ValidationOptions{
RestrictFields: map[string]bool{
"FirstName": true,
"LastName": true,
},
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestWithInvalidValuesAndFieldRestrictionAndOverwrittenFieldTags(t *testing.T) {
s := Test1{
FirstName: "123456789012345678901234567890",
LastName: "b",
Age: 15,
Price: 0,
PostCode: "AA123",
Email: "invalidEmail",
BelowZero: 8,
DiscountPrice: 9999,
Country: "Tokelau",
County: "",
}
expectedBool := false
expectedFailedFields := map[string]int{
"LastName": FailLenMin,
}
opts := &ValidationOptions{
RestrictFields: map[string]bool{
"FirstName": true,
"LastName": true,
},
OverwriteFieldTags: map[string]map[string]string{
"FirstName": map[string]string{
"validation": "req lenmin:4 lenmax:100",
},
},
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestWithInvalidValuesAndOverwrittenTagName(t *testing.T) {
s := Test2{
FirstName: "123456789012345678901234567890",
LastName: "b",
Age: 15,
Price: 0,
PostCode: "AA123",
Email: "invalidEmail",
BelowZero: 8,
DiscountPrice: 9999,
Country: "Tokelau",
County: "",
}
expectedBool := false
expectedFailedFields := map[string]int{
"FirstName": FailLenMax,
"LastName": FailLenMin,
"Age": FailValMin,
"PostCode": FailRegexp,
"Email": FailEmail,
"BelowZero": FailValMax,
"DiscountPrice": FailValMax,
"Country": FailRegexp,
}
opts := &ValidationOptions{
OverwriteTagName: "mytag",
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestValMinMaxWithDefault(t *testing.T) {
s := Test3{}
expectedBool := false
expectedFailedFields := map[string]int{
"NotZero": FailValMin,
"OnlyMin": FailValMin,
}
opts := &ValidationOptions{
OverwriteTagName: "mytag",
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestValMinMaxWithValid(t *testing.T) {
s := Test3{
NotZero: 4,
OnlyMin: 3,
OnlyMax: 7,
}
expectedBool := true
expectedFailedFields := map[string]int{}
opts := &ValidationOptions{
OverwriteTagName: "mytag",
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestValMinMaxWithInvalid(t *testing.T) {
s := Test3{
ZeroMin: -4,
ZeroMax: -6,
ZeroBoth: -6,
NotZero: 2,
OnlyMin: -5,
OnlyMax: -6,
}
expectedBool := false
expectedFailedFields := map[string]int{
"ZeroMin": FailValMin,
"ZeroBoth": FailValMin,
"NotZero": FailValMin,
"OnlyMin": FailValMin,
}
opts := &ValidationOptions{
OverwriteTagName: "mytag",
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestWithInvalidValuesWithSuffixValidation(t *testing.T) {
s := Test4{
PrimaryEmail: "invalidemail",
}
expectedBool := false
expectedFailedFields := map[string]int{
"PrimaryEmail": FailEmail,
}
opts := &ValidationOptions{
ValidateWhenSuffix: true,
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestWithInvalidValuesWithoutSuffixValidation(t *testing.T) {
s := Test4{
PrimaryEmail: "invalidemail",
}
expectedBool := true
expectedFailedFields := map[string]int{}
opts := &ValidationOptions{
ValidateWhenSuffix: false,
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func TestWithOverwrittenValues(t *testing.T) {
s := Test1{
FirstName: "123456789012345678901234567890",
LastName: "b",
Age: 15,
Price: 0,
PostCode: "AA123",
Email: "invalidEmail",
BelowZero: 8,
DiscountPrice: 9999,
Country: "Tokelau",
County: "",
}
expectedBool := false
expectedFailedFields := map[string]int{
"Age": FailValMax,
}
opts := &ValidationOptions{
RestrictFields: map[string]bool{
"FirstName": true,
"LastName": true,
"Age": true,
},
OverwriteFieldValues: map[string]interface{}{
"FirstName": "123456",
"LastName": "123",
"Age": 300,
},
}
compare(&s, expectedBool, expectedFailedFields, opts, t)
}
func compare(s interface{}, expectedBool bool, expectedFailedFields map[string]int, options *ValidationOptions, t *testing.T) {
valid, failedFields := Validate(s, options)
if valid != expectedBool {
t.Fatalf("Validate returned invalid boolean value")
}
compareFailedFields(failedFields, expectedFailedFields, t)
}
func compareFailedFields(failedFields map[string]int, expectedFailedFields map[string]int, t *testing.T) {
if len(failedFields) != len(expectedFailedFields) {
for k, v := range failedFields {
log.Printf("%s %d", k, v)
}
t.Fatalf("Validate returned invalid number of failed fields %d where it should be %d", len(failedFields), len(expectedFailedFields))
}
for k, v := range expectedFailedFields {
if failedFields[k] != v {
t.Fatalf("Validate returned invalid failure flag of %d where it should be %d for %s", failedFields[k], v, k)
}
}
}