-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
284 lines (280 loc) · 6.02 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
package govalidator
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidate(t *testing.T) {
type args struct {
v any
}
tests := []struct {
name string
args args
wantErr bool
checkErr func(err error) bool
}{
{
name: "invalid struct: interface",
args: args{
v: new(any),
},
wantErr: true,
checkErr: func(err error) bool {
return errors.Is(err, ErrNotStruct)
},
},
{
name: "invalid struct: map",
args: args{
v: map[string]string{},
},
wantErr: true,
checkErr: func(err error) bool {
return errors.Is(err, ErrNotStruct)
},
},
{
name: "invalid struct: string",
args: args{
v: "some string",
},
wantErr: true,
checkErr: func(err error) bool {
return errors.Is(err, ErrNotStruct)
},
},
{
name: "valid struct with no fields",
args: args{
v: struct{}{},
},
wantErr: false,
},
{
name: "valid struct with untagged fields",
args: args{
v: struct {
f1 string
f2 string
}{},
},
wantErr: false,
},
{
name: "valid struct with unexported fields",
args: args{
v: struct {
foo string `validate:"len:10"`
}{},
},
wantErr: true,
checkErr: func(err error) bool {
e := &ValidationErrors{}
return errors.As(err, e) && e.Error() == ErrValidateForUnexportedFields.Error()
},
},
{
name: "invalid validators syntax",
args: args{
v: struct {
Foo string `validate:"len:abcdef"`
}{},
},
wantErr: true,
checkErr: func(err error) bool {
e := &ValidationErrors{}
return errors.As(err, e) && e.Error() == ErrInvalidValidatorSyntax.Error()
},
},
{
name: "valid struct with tagged fields",
args: args{
v: struct {
Len string `validate:"len:20"`
LenZ string `validate:"len:0"`
InInt int `validate:"in:20,25,30"`
InNeg int `validate:"in:-20,-25,-30"`
InStr string `validate:"in:foo,bar"`
MinInt int `validate:"min:10"`
MinIntNeg int `validate:"min:-10"`
MinStr string `validate:"min:10"`
MinStrNeg string `validate:"min:-1"`
MaxInt int `validate:"max:20"`
MaxIntNeg int `validate:"max:-2"`
MaxStr string `validate:"max:20"`
}{
Len: "abcdefghjklmopqrstvu",
LenZ: "",
InInt: 25,
InNeg: -25,
InStr: "bar",
MinInt: 15,
MinIntNeg: -9,
MinStr: "abcdefghjkl",
MinStrNeg: "abc",
MaxInt: 16,
MaxIntNeg: -3,
MaxStr: "abcdefghjklmopqrst",
},
},
wantErr: false,
},
{
name: "wrong length",
args: args{
v: struct {
Lower string `validate:"len:24"`
Higher string `validate:"len:5"`
Zero string `validate:"len:3"`
BadSpec string `validate:"len:%12"`
Negative string `validate:"len:-6"`
}{
Lower: "abcdef",
Higher: "abcdef",
Zero: "",
BadSpec: "abc",
Negative: "abcd",
},
},
wantErr: true,
checkErr: func(err error) bool {
assert.Len(t, err.(ValidationErrors), 5)
return true
},
},
{
name: "wrong in",
args: args{
v: struct {
InA string `validate:"in:ab,cd"`
InB string `validate:"in:aa,bb,cd,ee"`
InC int `validate:"in:-1,-3,5,7"`
InD int `validate:"in:5-"`
InEmpty string `validate:"in:"`
}{
InA: "ef",
InB: "ab",
InC: 2,
InD: 12,
InEmpty: "",
},
},
wantErr: true,
checkErr: func(err error) bool {
assert.Len(t, err.(ValidationErrors), 5)
return true
},
},
{
name: "wrong min",
args: args{
v: struct {
MinA string `validate:"min:12"`
MinB int `validate:"min:-12"`
MinC int `validate:"min:5-"`
MinD int `validate:"min:"`
MinE string `validate:"min:"`
}{
MinA: "ef",
MinB: -22,
MinC: 12,
MinD: 11,
MinE: "abc",
},
},
wantErr: true,
checkErr: func(err error) bool {
assert.Len(t, err.(ValidationErrors), 5)
return true
},
},
{
name: "wrong max",
args: args{
v: struct {
MaxA string `validate:"max:2"`
MaxB string `validate:"max:-7"`
MaxC int `validate:"max:-12"`
MaxD int `validate:"max:5-"`
MaxE int `validate:"max:"`
MaxF string `validate:"max:"`
}{
MaxA: "efgh",
MaxB: "ab",
MaxC: 22,
MaxD: 12,
MaxE: 11,
MaxF: "abc",
},
},
wantErr: true,
checkErr: func(err error) bool {
assert.Len(t, err.(ValidationErrors), 6)
return true
},
},
{
name: "valid struct with slices",
args: args{
v: struct {
SliceMax []int `validate:"max:10"`
SliceMin []int `validate:"min:10"`
SliceIn []int `validate:"in:10,20,30"`
SliceLen []string `validate:"len:2"`
}{
SliceMax: []int{1, 5, 7, 6, 2},
SliceMin: []int{10, 13, 11, 12, 13},
SliceIn: []int{10, 10, 30, 20, 30, 10},
SliceLen: []string{"ab", "ca", "de", "ff"},
},
},
wantErr: false,
},
{
name: "wrong in slice",
args: args{
v: struct {
SliceInStr []string `validate:"in:ab,ca,de"`
SliceInInt []int `validate:"in:1,3,5"`
}{
SliceInStr: []string{"ce, ca, de"},
SliceInInt: []int{2, 3, 5},
},
},
wantErr: true,
checkErr: func(err error) bool {
assert.Len(t, err.(ValidationErrors), 2)
return true
},
},
{
name: "wrong max slice",
args: args{
v: struct {
SliceMaxStr []string `validate:"max:3"`
SliceMaxInt []int `validate:"max:3"`
}{
SliceMaxStr: []string{"ce, ca, deve"},
SliceMaxInt: []int{2, 3, 5},
},
},
wantErr: true,
checkErr: func(err error) bool {
assert.Len(t, err.(ValidationErrors), 2)
return true
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Validate(tt.args.v)
if tt.wantErr {
assert.Error(t, err)
assert.True(t, tt.checkErr(err), "test expect an error, but got wrong error type")
} else {
assert.NoError(t, err)
}
})
}
}