Repository navigation
Expand file tree
/
Copy pathcrossfield_test.go
More file actions
418 lines (398 loc) · 16.3 KB
/
Copy pathcrossfield_test.go
File metadata and controls
418 lines (398 loc) · 16.3 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package validator
import (
"context"
"errors"
"reflect"
"testing"
)
func TestCrossNumericBlankSibling(t *testing.T) {
gt := >FieldRule{}
blank := &fakeField{val: reflect.ValueOf(5), attrs: []string{"other"}, siblings: map[string]*Field{"other": &fakeField{val: reflect.ValueOf("")}}}
if gt.Passes(blank) {
t.Error("gtfield with blank-string sibling should fail (not comparable)")
}
real := &fakeField{val: reflect.ValueOf(5), attrs: []string{"other"}, siblings: map[string]*Field{"other": &fakeField{val: reflect.ValueOf(3)}}}
if !gt.Passes(real) {
t.Error("gtfield 5 > 3 should pass")
}
// numeric zero sibling is comparable, not empty
zero := &fakeField{val: reflect.ValueOf(5), attrs: []string{"other"}, siblings: map[string]*Field{"other": &fakeField{val: reflect.ValueOf(0)}}}
if !gt.Passes(zero) {
t.Error("gtfield 5 > 0 should pass (numeric zero sibling is comparable)")
}
}
// sib builds a siblings map for cross-field tests.
func sib(pairs map[string]any) map[string]*Field {
m := make(map[string]*Field, len(pairs))
for k, v := range pairs {
rv := reflect.Value{}
if v != nil {
rv = reflect.ValueOf(v)
}
m[k] = &fakeField{val: rv}
}
return m
}
func TestRequiredIf(t *testing.T) {
r := &requiredIfRule{}
cases := []struct {
name string
f *fakeField
want bool
}{
{"trigger absent fails", &fakeField{val: reflect.Value{}, attrs: []string{"type", "card"}, siblings: sib(map[string]any{"type": "card"})}, false},
{"trigger present passes", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"type", "card"}, siblings: sib(map[string]any{"type": "card"})}, true},
{"no trigger empty passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"type", "card"}, siblings: sib(map[string]any{"type": "cash"})}, true},
{"other missing passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"type", "card"}}, true},
{"too few args passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"type"}}, true},
}
for _, c := range cases {
if got := r.Passes(c.f); got != c.want {
t.Errorf("%s: Passes()=%v want %v", c.name, got, c.want)
}
}
if r.Signature() != "required_if" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestRequiredUnless(t *testing.T) {
r := &requiredUnlessRule{}
cases := []struct {
name string
f *fakeField
want bool
}{
{"not equal absent fails", &fakeField{val: reflect.Value{}, attrs: []string{"type", "admin"}, siblings: sib(map[string]any{"type": "user"})}, false},
{"not equal present passes", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"type", "admin"}, siblings: sib(map[string]any{"type": "user"})}, true},
{"equal empty passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"type", "admin"}, siblings: sib(map[string]any{"type": "admin"})}, true},
{"other missing absent fails", &fakeField{val: reflect.Value{}, attrs: []string{"type", "admin"}}, false},
{"too few args passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"type"}}, true},
}
for _, c := range cases {
if got := r.Passes(c.f); got != c.want {
t.Errorf("%s: Passes()=%v want %v", c.name, got, c.want)
}
}
if r.Signature() != "required_unless" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestRequiredWith(t *testing.T) {
r := &requiredWithRule{}
cases := []struct {
name string
f *fakeField
want bool
}{
{"sibling present self absent fails", &fakeField{val: reflect.Value{}, attrs: []string{"a", "b"}, siblings: sib(map[string]any{"a": "v", "b": ""})}, false},
{"sibling present self present passes", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"a"}, siblings: sib(map[string]any{"a": "v"})}, true},
{"all siblings empty passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"a", "b"}, siblings: sib(map[string]any{"a": "", "b": ""})}, true},
{"siblings missing passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"a"}}, true},
{"no args passes", &fakeField{val: reflect.ValueOf("")}, true},
}
for _, c := range cases {
if got := r.Passes(c.f); got != c.want {
t.Errorf("%s: Passes()=%v want %v", c.name, got, c.want)
}
}
if r.Signature() != "required_with" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestRequiredWithout(t *testing.T) {
r := &requiredWithoutRule{}
cases := []struct {
name string
f *fakeField
want bool
}{
{"sibling empty self absent fails", &fakeField{val: reflect.Value{}, attrs: []string{"a"}, siblings: sib(map[string]any{"a": ""})}, false},
{"sibling missing self absent fails", &fakeField{val: reflect.Value{}, attrs: []string{"a"}}, false},
{"sibling empty self present passes", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"a"}, siblings: sib(map[string]any{"a": ""})}, true},
{"all siblings present passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"a", "b"}, siblings: sib(map[string]any{"a": "v", "b": "w"})}, true},
{"one missing self absent fails", &fakeField{val: reflect.Value{}, attrs: []string{"a", "b"}, siblings: sib(map[string]any{"a": "v"})}, false},
{"no args passes", &fakeField{val: reflect.ValueOf("")}, true},
}
for _, c := range cases {
if got := r.Passes(c.f); got != c.want {
t.Errorf("%s: Passes()=%v want %v", c.name, got, c.want)
}
}
if r.Signature() != "required_without" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestSame(t *testing.T) {
r := &sameRule{}
cases := []struct {
name string
f *fakeField
want bool
}{
{"equal passes", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "x"})}, true},
{"numeric coerced equal passes", &fakeField{val: reflect.ValueOf(1), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "1"})}, true},
{"not equal fails", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "y"})}, false},
{"empty self passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "y"})}, true},
{"missing sibling fails", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}}, false},
{"no args fails", &fakeField{val: reflect.ValueOf("x")}, false},
}
for _, c := range cases {
if got := r.Passes(c.f); got != c.want {
t.Errorf("%s: Passes()=%v want %v", c.name, got, c.want)
}
}
if r.Signature() != "same" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestDifferent(t *testing.T) {
r := &differentRule{}
cases := []struct {
name string
f *fakeField
want bool
}{
{"different passes", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "y"})}, true},
{"same fails", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "x"})}, false},
{"empty self passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"o"}, siblings: sib(map[string]any{"o": ""})}, true},
{"missing sibling passes", &fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}}, true},
{"no args fails", &fakeField{val: reflect.ValueOf("x")}, false},
}
for _, c := range cases {
if got := r.Passes(c.f); got != c.want {
t.Errorf("%s: Passes()=%v want %v", c.name, got, c.want)
}
}
if r.Signature() != "different" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestEqField(t *testing.T) {
r := &eqFieldRule{}
if !r.Passes(&fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "x"})}) {
t.Error("equal should pass")
}
if r.Passes(&fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "y"})}) {
t.Error("not equal should fail")
}
if !r.Passes(&fakeField{val: reflect.ValueOf(""), attrs: []string{"o"}}) {
t.Error("empty self should pass")
}
if r.Passes(&fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}}) {
t.Error("missing sibling should fail")
}
if r.Signature() != "eqfield" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestNeField(t *testing.T) {
r := &neFieldRule{}
if !r.Passes(&fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "y"})}) {
t.Error("different should pass")
}
if r.Passes(&fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "x"})}) {
t.Error("same should fail")
}
if !r.Passes(&fakeField{val: reflect.ValueOf(""), attrs: []string{"o"}, siblings: sib(map[string]any{"o": ""})}) {
t.Error("empty self should pass")
}
if !r.Passes(&fakeField{val: reflect.ValueOf("x"), attrs: []string{"o"}}) {
t.Error("missing sibling should pass")
}
if r.Signature() != "nefield" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestGtField(t *testing.T) {
r := >FieldRule{}
cases := []struct {
name string
f *fakeField
want bool
}{
{"greater passes", &fakeField{val: reflect.ValueOf(5), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 3})}, true},
{"equal fails", &fakeField{val: reflect.ValueOf(5), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}, false},
{"less fails", &fakeField{val: reflect.ValueOf(2), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}, false},
{"numeric string passes", &fakeField{val: reflect.ValueOf("5"), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "3"})}, true},
{"non numeric other fails", &fakeField{val: reflect.ValueOf(5), attrs: []string{"o"}, siblings: sib(map[string]any{"o": "abc"})}, false},
{"empty self passes", &fakeField{val: reflect.ValueOf(""), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 3})}, true},
{"missing sibling fails", &fakeField{val: reflect.ValueOf(5), attrs: []string{"o"}}, false},
{"no args fails", &fakeField{val: reflect.ValueOf(5)}, false},
}
for _, c := range cases {
if got := r.Passes(c.f); got != c.want {
t.Errorf("%s: Passes()=%v want %v", c.name, got, c.want)
}
}
if r.Signature() != "gtfield" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestGteField(t *testing.T) {
r := >eFieldRule{}
if !r.Passes(&fakeField{val: reflect.ValueOf(5), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("equal should pass")
}
if !r.Passes(&fakeField{val: reflect.ValueOf(6), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("greater should pass")
}
if r.Passes(&fakeField{val: reflect.ValueOf(4), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("less should fail")
}
if r.Signature() != "gtefield" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestLtField(t *testing.T) {
r := <FieldRule{}
if !r.Passes(&fakeField{val: reflect.ValueOf(3), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("less should pass")
}
if r.Passes(&fakeField{val: reflect.ValueOf(5), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("equal should fail")
}
if r.Passes(&fakeField{val: reflect.ValueOf(7), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("greater should fail")
}
if r.Signature() != "ltfield" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestLteField(t *testing.T) {
r := <eFieldRule{}
if !r.Passes(&fakeField{val: reflect.ValueOf(5), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("equal should pass")
}
if !r.Passes(&fakeField{val: reflect.ValueOf(4), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("less should pass")
}
if r.Passes(&fakeField{val: reflect.ValueOf(6), attrs: []string{"o"}, siblings: sib(map[string]any{"o": 5})}) {
t.Error("greater should fail")
}
if r.Signature() != "ltefield" {
t.Errorf("Signature()=%q", r.Signature())
}
}
func TestConfirmed(t *testing.T) {
r := &confirmedRule{}
cases := []struct {
name string
f *fakeField
want bool
}{
{"match passes", &fakeField{name: "password", val: reflect.ValueOf("x"), siblings: sib(map[string]any{"password_confirmation": "x"})}, true},
{"mismatch fails", &fakeField{name: "password", val: reflect.ValueOf("x"), siblings: sib(map[string]any{"password_confirmation": "y"})}, false},
{"empty self passes", &fakeField{name: "password", val: reflect.ValueOf(""), siblings: sib(map[string]any{"password_confirmation": "y"})}, true},
{"missing confirmation fails", &fakeField{name: "password", val: reflect.ValueOf("x")}, false},
}
for _, c := range cases {
if got := r.Passes(c.f); got != c.want {
t.Errorf("%s: Passes()=%v want %v", c.name, got, c.want)
}
}
if r.Signature() != "confirmed" {
t.Errorf("Signature()=%q", r.Signature())
}
}
// TestCrossfieldContracts checks every rule has a non-empty signature and message.
func TestCrossfieldContracts(t *testing.T) {
rs := []Rule{
&requiredIfRule{}, &requiredUnlessRule{}, &requiredWithRule{}, &requiredWithoutRule{},
&requiredWithAllRule{}, &requiredWithoutAllRule{},
&excludedIfRule{}, &excludedUnlessRule{}, &excludedWithRule{}, &excludedWithoutRule{},
&sameRule{}, &differentRule{}, &eqFieldRule{}, &neFieldRule{},
>FieldRule{}, >eFieldRule{}, <FieldRule{}, <eFieldRule{}, &confirmedRule{},
}
for _, r := range rs {
if r.Signature() == "" {
t.Errorf("%T: empty signature", r)
}
if r.Message() == "" {
t.Errorf("%T: empty message", r)
}
}
}
func TestRequiredWithAll(t *testing.T) {
run := func(data map[string]any, rule string) bool {
vd := MustMap(data, map[string]string{"x": rule})
_ = vd.Validate(context.Background())
return !vd.Fails()
}
// required only when BOTH a and b are present
if run(map[string]any{"a": "1", "b": "2"}, "required_with_all:a,b") {
t.Error("x missing while a AND b present must fail")
}
if !run(map[string]any{"a": "1"}, "required_with_all:a,b") {
t.Error("only a present: x is not required")
}
// required_without_all: required only when BOTH absent
if run(map[string]any{}, "required_without_all:a,b") {
t.Error("a and b both absent: x is required")
}
if !run(map[string]any{"a": "1"}, "required_without_all:a,b") {
t.Error("a present: x is not required")
}
}
// Missing args make conditional rules pass vacuously, so arity is a compile-time error.
func TestConditionalRuleArity(t *testing.T) {
bad := []string{
"excluded_if:role", "excluded_unless:role", "excluded_with", "excluded_without",
"required_if:role", "required_unless:role", "required_with", "required_without",
"required_with_all", "required_without_all",
}
for _, rule := range bad {
if _, err := Map(map[string]any{"x": "v"}, map[string]string{"x": rule}); !errors.Is(err, ErrInvalidRules) {
t.Errorf("%q error = %v, want ErrInvalidRules", rule, err)
}
av := MustMap(map[string]any{}, map[string]string{})
if err := av.AddRules("x", rule); err == nil {
t.Errorf("AddRules must reject %q", rule)
}
}
// legal arity still compiles
ok := MustMap(map[string]any{"x": "v", "role": "admin"}, map[string]string{"x": "excluded_if:role,other"})
_ = ok.Validate(context.Background())
if ok.Fails() {
t.Errorf("legal excluded_if must compile and pass: %v", ok.Errors().All())
}
}
func TestExcludedFamily(t *testing.T) {
run := func(data map[string]any, rule string) bool {
vd := MustMap(data, map[string]string{"x": rule})
_ = vd.Validate(context.Background())
return !vd.Fails()
}
// excluded_if: x must be empty when mode=off
if run(map[string]any{"x": "v", "mode": "off"}, "excluded_if:mode,off") {
t.Error("x present while mode=off must fail excluded_if")
}
if !run(map[string]any{"x": "v", "mode": "on"}, "excluded_if:mode,off") {
t.Error("mode=on: x may be present")
}
if !run(map[string]any{"mode": "off"}, "excluded_if:mode,off") {
t.Error("empty x always passes excluded_if")
}
// excluded_unless: x must be empty unless mode=admin
if run(map[string]any{"x": "v", "mode": "user"}, "excluded_unless:mode,admin") {
t.Error("mode!=admin: x must be empty")
}
if !run(map[string]any{"x": "v", "mode": "admin"}, "excluded_unless:mode,admin") {
t.Error("mode=admin: x may be present")
}
if run(map[string]any{"x": "v"}, "excluded_unless:mode,admin") {
t.Error("absent sibling cannot match: exclusion applies")
}
// excluded_with / excluded_without
if run(map[string]any{"x": "v", "other": "y"}, "excluded_with:other") {
t.Error("other present: x must be empty")
}
if !run(map[string]any{"x": "v"}, "excluded_with:other") {
t.Error("other absent: x may be present")
}
if run(map[string]any{"x": "v"}, "excluded_without:other") {
t.Error("other absent: x must be empty")
}
if !run(map[string]any{"x": "v", "other": "y"}, "excluded_without:other") {
t.Error("other present: x may be present")
}
}