-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator_slice_test.go
200 lines (162 loc) · 6.11 KB
/
validator_slice_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
package validation
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_SliceLen(t *testing.T) {
errs := SliceLen([]int{1, 2}, 2, 10).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceLen([]int{1, 2}, 3, 10).Validate(ctxBg)
assert.Equal(t, "len", errs[0].Type())
}
func Test_SliceUnique(t *testing.T) {
errs := SliceUnique([]int{1, 2, 3}).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceUnique([]int{1, 2, 3, 1}).Validate(ctxBg)
assert.Equal(t, "unique", errs[0].Type())
}
func Test_SliceUniqueBy(t *testing.T) {
errs := SliceUniqueBy([]any{1, 2, 3}, func(v any) int { return v.(int) }).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceUniqueBy([]any{1, 2, 3, 1}, func(v any) int { return v.(int) }).Validate(ctxBg)
assert.Equal(t, "unique", errs[0].Type())
}
func Test_SliceSorted(t *testing.T) {
errs := SliceSorted([]int{1, 2, 3}).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceSorted([]int{1, 3, 2}).Validate(ctxBg)
assert.Equal(t, "sorted", errs[0].Type())
}
func Test_SliceSortedBy(t *testing.T) {
errs := SliceSortedBy([]any{1, 2, 3}, func(v any) int { return v.(int) }).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceSortedBy([]any{1, 3, 2}, func(v any) int { return v.(int) }).Validate(ctxBg)
assert.Equal(t, "sorted", errs[0].Type())
}
func Test_SliceSortedDesc(t *testing.T) {
errs := SliceSortedDesc([]int{3, 2, 1}).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceSortedDesc([]int{3, 2, 3}).Validate(ctxBg)
assert.Equal(t, "sorted_desc", errs[0].Type())
}
func Test_SliceSortedDescBy(t *testing.T) {
errs := SliceSortedDescBy([]any{3, 2, 1}, func(v any) int { return v.(int) }).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceSortedDescBy([]any{3, 2, 3}, func(v any) int { return v.(int) }).Validate(ctxBg)
assert.Equal(t, "sorted_desc", errs[0].Type())
}
func Test_SliceHasElem(t *testing.T) {
errs := SliceHasElem([]int{3, 2, 1}, 1, 2, 3).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceHasElem([]int{3, 2, 1}, 1, 2, 4).Validate(ctxBg)
assert.Equal(t, "has_elem", errs[0].Type())
}
func Test_SliceHasElemBy(t *testing.T) {
errs := SliceHasElemBy([]any{3, 2, 1}, func(v any) bool { return v == 1 || v == 2 || v == 3 }).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceHasElemBy([]any{3, 2, 1}, func(v any) bool { return v == 3 || v == 2 }).Validate(ctxBg)
assert.Equal(t, "has_elem_by", errs[0].Type())
}
func Test_SliceNotHaveElem(t *testing.T) {
errs := SliceNotHaveElem([]int{3, 2, 1}, 0, 4, 5).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceNotHaveElem([]int{3, 2, 1}, 4, 5, 2).Validate(ctxBg)
assert.Equal(t, "not_have_elem", errs[0].Type())
}
func Test_SliceNotHaveElemBy(t *testing.T) {
errs := SliceNotHaveElemBy([]any{3, 2, 1}, func(v any) bool { return v == 4 || v == 5 }).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceNotHaveElemBy([]any{3, 2, 1}, func(v any) bool { return v == 1 || v == 2 }).Validate(ctxBg)
assert.Equal(t, "not_have_elem_by", errs[0].Type())
}
func Test_SliceElemIn(t *testing.T) {
errs := SliceElemIn([]int{3, 2, 1}, 1, 2, 3, 4, 5).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceElemIn([]int{3, 2, 1}, 1, 2).Validate(ctxBg)
assert.Equal(t, "elem_in", errs[0].Type())
}
func Test_SliceElemNotIn(t *testing.T) {
errs := SliceElemNotIn([]int{3, 2, 1}, 4, 5, 6).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceElemNotIn([]int{3, 2, 1}, 1, 4, 5).Validate(ctxBg)
assert.Equal(t, "elem_not_in", errs[0].Type())
}
func Test_SliceElemRange(t *testing.T) {
errs := SliceElemRange([]int{3, 2, 1}, 0, 3).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
errs = SliceElemRange([]int{3, 2, 1}, 1, 2).Validate(ctxBg)
assert.Equal(t, "elem_range", errs[0].Type())
}
func Test_SliceElemValidate(t *testing.T) {
t.Run("nil/empty slice", func(t *testing.T) {
// Nil slice
errs := Slice([]string(nil)).ForEach(func(elem string, index int, validator ItemValidator) {
validator.Validate(StrLen(&elem, 1, 10))
}).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
// Empty slice
errs = Slice([]string{}).ForEach(func(elem string, index int, validator ItemValidator) {
validator.Validate(StrLen(&elem, 1, 10))
}).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
})
t.Run("validate elements", func(t *testing.T) {
// Validate element
errs := Slice([]int{3, 2, 1}).ForEach(func(elem int, index int, validator ItemValidator) {
validator.Validate(
NumGTE(&elem, 1),
)
}).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
// Validate element with errors
errs = Slice([]int{3, 2, 1}).ForEach(func(elem int, index int, validator ItemValidator) {
validator.Validate(
NumGT(&elem, 2).OnError(
SetField(fmt.Sprintf("slice[%d]", index), nil),
SetCustomKey("ERR_VLD_SLICE_ELEMENT_INVALID"),
),
)
}).OnError().Validate(ctxBg)
assert.Equal(t, 2, len(errs))
assert.Equal(t, "gt", errs[0].Type())
assert.Equal(t, "gt", errs[1].Type())
})
t.Run("validate element with Group", func(t *testing.T) {
errs := Slice([]int{3, 2, 1}).ForEach(func(elem int, index int, validator ItemValidator) {
validator.Group(
NumGTE(&elem, 1),
NumLTE(&elem, 2),
)
}).Validate(ctxBg)
assert.Equal(t, 1, len(errs))
assert.Equal(t, "group", errs[0].Type())
})
t.Run("validate element with OneOf", func(t *testing.T) {
errs := Slice([]int{3, 2, 1}).ForEach(func(elem int, index int, validator ItemValidator) {
validator.OneOf(
NumLTE(&elem, 2),
NumGTE(&elem, 1),
)
}).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
})
t.Run("validate element with ExactOneOf", func(t *testing.T) {
errs := Slice([]int{3, 2, 1}).ForEach(func(elem int, index int, validator ItemValidator) {
validator.ExactOneOf(
NumGTE(&elem, 1),
NumLTE(&elem, 2),
)
}).Validate(ctxBg)
assert.Equal(t, 2, len(errs)) // slice[1] and slice[2] not satisfy
})
t.Run("validate element with NotOf", func(t *testing.T) {
errs := Slice([]int{3, 2, 1}).ForEach(func(elem int, index int, validator ItemValidator) {
validator.NotOf(
NumGTE(&elem, 1),
NumLTE(&elem, 2),
)
}).Validate(ctxBg)
assert.Equal(t, 3, len(errs))
})
}