-
Notifications
You must be signed in to change notification settings - Fork 7
/
slices_test.go
247 lines (206 loc) · 5.71 KB
/
slices_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
package zog
import (
"reflect"
"strings"
"testing"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
// !STRUCTS
type User struct {
Name string
}
type Team struct {
Users []User
}
func TestSliceOfStructs(t *testing.T) {
var userSchema = Struct(Schema{
"name": String().Required(),
})
var teamSchema = Struct(Schema{
"users": Slice(userSchema),
})
var data = map[string]interface{}{
"users": []interface{}{
map[string]interface{}{
"name": "Jane",
},
map[string]interface{}{
"name": "John",
},
},
}
var team Team
errsMap := teamSchema.Parse(NewMapDataProvider(data), &team)
assert.Nil(t, errsMap)
assert.Len(t, team.Users, 2)
assert.Equal(t, team.Users[0].Name, "Jane")
assert.Equal(t, team.Users[1].Name, "John")
data = map[string]interface{}{
"users": []interface{}{
map[string]interface{}{},
map[string]interface{}{},
},
}
errsMap = teamSchema.Parse(NewMapDataProvider(data), &team)
assert.Len(t, errsMap["users[0].name"], 1)
assert.Len(t, errsMap["users[1].name"], 1)
}
func TestSliceOptionalSlice(t *testing.T) {
s := []string{}
schema := Slice(String()) // should be optional by default
errs := schema.Parse(nil, &s)
assert.Nil(t, errs)
assert.Len(t, s, 0)
schema.Required().Optional() // should override required
errs = schema.Parse(nil, &s)
assert.Nil(t, errs)
assert.Len(t, s, 0)
}
func TestSliceRequired(t *testing.T) {
s := []string{}
customMsg := "This slice is required and cannot be empty"
schema := Slice(String()).Required(Message(customMsg))
// Test with nil value
errs := schema.Parse(nil, &s)
assert.NotNil(t, errs)
assert.Equal(t, customMsg, errs["$root"][0].Message())
// Test with empty slice
errs = schema.Parse([]string{}, &s)
assert.Nil(t, errs)
assert.Len(t, s, 0)
}
func TestSliceDefaultCoercing(t *testing.T) {
s := []string{}
schema := Slice(String())
errs := schema.Parse("a", &s)
assert.Nil(t, errs)
assert.Len(t, s, 1)
assert.Equal(t, s[0], "a")
}
func TestSliceDefault(t *testing.T) {
schema := Slice(String()).Default([]string{"a", "b", "c"})
s := []string{}
err := schema.Parse(nil, &s)
assert.Nil(t, err)
assert.Len(t, s, 3)
assert.Equal(t, s[0], "a")
assert.Equal(t, s[1], "b")
assert.Equal(t, s[2], "c")
}
func TestSlicePassSchema(t *testing.T) {
s := []string{}
schema := Slice(String().Required())
errs := schema.Parse([]any{"a", "b", "c"}, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
assert.Equal(t, s[0], "a")
assert.Equal(t, s[1], "b")
assert.Equal(t, s[2], "c")
}
func TestSliceErrors(t *testing.T) {
s := []string{}
schema := Slice(String().Required().Min(2))
errs := schema.Parse([]any{"a", "b"}, &s)
assert.Len(t, errs, 3)
assert.NotEmpty(t, errs["[0]"])
assert.NotEmpty(t, errs["[1]"])
assert.Empty(t, errs["[2]"])
}
func TestSlicePostTransform(t *testing.T) {
s := []string{}
schema := Slice(String()).PostTransform(func(dataPtr any, ctx ParseCtx) error {
s := dataPtr.(*[]string)
for i := 0; i < len(*s); i++ {
(*s)[i] = strings.ToUpper((*s)[i])
}
return nil
})
errs := schema.Parse([]string{"a", "b", "c"}, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
assert.Equal(t, []string{"A", "B", "C"}, s)
}
func TestSlicePreTransform(t *testing.T) {
s := []string{}
schema := Slice(String()).PreTransform(func(data any, ctx ParseCtx) (any, error) {
s := data.([]string)
for i := 0; i < len(s); i++ {
s[i] = strings.ToUpper(s[i])
}
return s, nil
})
errs := schema.Parse([]string{"a", "b", "c"}, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
assert.Equal(t, s[0], "A")
assert.Equal(t, s[1], "B")
assert.Equal(t, s[2], "C")
}
// VALIDATORS
func TestSliceLen(t *testing.T) {
s := []string{}
els := []string{"a", "b", "c", "d", "e"}
schema := Slice(String().Required()).Len(2, Message("custom"))
errs := schema.Parse(els[:2], &s)
assert.Len(t, s, 2)
assert.Nil(t, errs)
errs = schema.Parse(els[:1], &s)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom", errs["$root"][0].Message())
// min
schema = Slice(String().Required()).Min(2, Message("custom"))
errs = schema.Parse(els[:4], &s)
assert.Nil(t, errs)
errs = schema.Parse(els[:1], &s)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom", errs["$root"][0].Message())
// max
schema = Slice(String().Required()).Max(3, Message("custom"))
errs = schema.Parse(els[:1], &s)
assert.Nil(t, errs)
errs = schema.Parse(els[:4], &s)
assert.NotNil(t, errs)
assert.Equal(t, "custom", errs["$root"][0].Message())
}
func TestSliceContains(t *testing.T) {
s := []string{}
items := []string{"a", "b", "c"}
schema := Slice(String()).Contains("a")
errs := schema.Parse(items, &s)
assert.Nil(t, errs)
assert.Len(t, s, 3)
schema = Slice(String()).Contains("d", Message("custom"))
errs = schema.Parse(items, &s)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom", errs["$root"][0].Message())
}
func TestSliceCustomTest(t *testing.T) {
input := []string{"abc", "defg", "hijkl"}
s := []string{}
schema := Slice(String()).Test(TestFunc("custom_test", func(val any, ctx ParseCtx) bool {
// Custom test logic here
x := val.(*[]string)
return reflect.DeepEqual(input, *x)
}), Message("custom"))
errs := schema.Parse(input, &s)
assert.Empty(t, errs)
assert.Equal(t, input, s)
errs = schema.Parse(input[1:], &s)
assert.NotEmpty(t, errs)
assert.Equal(t, "custom", errs["$root"][0].Message())
assert.Equal(t, "custom_test", errs["$root"][0].Code())
}
func TestSliceSchemaOption(t *testing.T) {
s := Slice(String(), WithCoercer(func(original any) (value any, err error) {
return []string{"coerced"}, nil
}))
var result []string
err := s.Parse(123, &result)
assert.Nil(t, err)
assert.Equal(t, []string{"coerced"}, result)
}
func TestSliceGetType(t *testing.T) {
s := Slice(String())
assert.Equal(t, zconst.TypeSlice, s.getType())
}