-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathslices_validate_test.go
238 lines (207 loc) · 5.89 KB
/
slices_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
package zog
import (
"strings"
"testing"
"github.com/Oudwins/zog/tutils"
"github.com/stretchr/testify/assert"
)
func TestValidateSliceRequired(t *testing.T) {
validator := Slice(String())
dest := []string{"test"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, []string{"test"}, dest)
validator = validator.Required()
dest = []string{}
errs = validator.Validate(&dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
tutils.VerifyDefaultIssueMessagesMap(t, errs)
}
func TestValidateSliceOptional(t *testing.T) {
validator := Slice(String()).Optional()
dest := []string{"test"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
dest = []string{}
errs = validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
}
func TestValidateSliceDefault(t *testing.T) {
validator := Slice(String()).Default([]string{"default"})
dest := []string{}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, []string{"default"}, dest)
}
func TestValidateSlicePreTransform(t *testing.T) {
preTransform := func(val any, ctx Ctx) (any, error) {
if v, ok := val.([]string); ok {
out := make([]string, len(v))
copy(out, v)
for i := range out {
out[i] = strings.ToUpper(out[i])
}
return out, nil
}
return val, nil
}
validator := Slice(String()).PreTransform(preTransform)
dest := []string{"test", "example"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, []string{"TEST", "EXAMPLE"}, dest)
}
func TestValidateSlicePostTransform(t *testing.T) {
postTransform := func(val any, ctx Ctx) error {
if v, ok := val.(*[]string); ok {
for i := range *v {
(*v)[i] = strings.ToUpper((*v)[i])
}
}
return nil
}
validator := Slice(String()).PostTransform(postTransform)
dest := []string{"test", "example"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, []string{"TEST", "EXAMPLE"}, dest)
}
func TestValidateSliceLen(t *testing.T) {
validator := Slice(String()).Len(2)
dest := []string{"test", "example"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
dest = []string{"test"}
errs = validator.Validate(&dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
tutils.VerifyDefaultIssueMessagesMap(t, errs)
}
func TestValidateSliceMin(t *testing.T) {
validator := Slice(String()).Min(2)
dest := []string{"test", "example", "extra"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
dest = []string{"test"}
errs = validator.Validate(&dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
tutils.VerifyDefaultIssueMessagesMap(t, errs)
}
func TestValidateSliceMax(t *testing.T) {
validator := Slice(String()).Max(2)
dest := []string{"test", "example"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
dest = []string{"test", "example", "extra"}
errs = validator.Validate(&dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
tutils.VerifyDefaultIssueMessagesMap(t, errs)
}
func TestValidateSliceContains(t *testing.T) {
validator := Slice(String()).Contains("test")
dest := []string{"test", "example"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
dest = []string{"example", "sample"}
errs = validator.Validate(&dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
tutils.VerifyDefaultIssueMessagesMap(t, errs)
}
func TestValidateSliceCustomTest(t *testing.T) {
validator := Slice(String()).TestFunc(func(val any, ctx Ctx) bool {
if v, ok := val.(*[]string); ok {
return len(*v) > 0 && (*v)[0] == "test"
}
return false
}, Message("custom"))
dest := []string{"test", "example"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
dest = []string{"wrong", "example"}
errs = validator.Validate(&dest)
if len(errs) == 0 {
t.Errorf("Expected errors, got none")
}
assert.Equal(t, "custom", errs["$root"][0].Message)
// assert.Equal(t, "custom_test", errs["$root"][0].Code())
}
// TODO not yet supported
// func TestValidateSliceNestedValidation(t *testing.T) {
// type User struct {
// Name string
// }
// userSchema := Struct(Schema{
// "name": String().Required(),
// })
// validator := Slice(userSchema)
// dest := []User{
// {Name: "John"},
// {Name: "Jane"},
// }
// errs := validator.Validate(&dest)
// if len(errs) > 0 {
// t.Errorf("Expected no errors, got %v", errs)
// }
// dest = []User{
// {Name: ""},
// {Name: ""},
// }
// errs = validator.Validate(&dest)
// assert.NotEmpty(t, errs)
// assert.NotEmpty(t, errs["[0].name"])
// assert.NotEmpty(t, errs["[1].name"])
// }
func TestValidateSliceMultipleValidators(t *testing.T) {
validator := Slice(String()).
Min(2, Message("too short")).
Max(4, Message("too long")).
Contains("test", Message("must contain test"))
dest := []string{"test", "example"}
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
dest = []string{"wrong"}
errs = validator.Validate(&dest)
assert.NotEmpty(t, errs)
assert.Contains(t, Issues.SanitizeList(errs["$root"]), "too short")
assert.Contains(t, Issues.SanitizeList(errs["$root"]), "must contain test")
assert.Len(t, errs["$root"], 2)
dest = []string{"a", "b", "c", "d", "e"}
errs = validator.Validate(&dest)
assert.NotEmpty(t, errs)
assert.Contains(t, Issues.SanitizeList(errs["$root"]), "too long")
assert.Contains(t, Issues.SanitizeList(errs["$root"]), "must contain test")
assert.Len(t, errs["$root"], 2)
}