-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathregression_compat_test.go
More file actions
823 lines (753 loc) · 29.1 KB
/
Copy pathregression_compat_test.go
File metadata and controls
823 lines (753 loc) · 29.1 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
package validate
import (
"fmt"
"sort"
"strings"
"testing"
"github.com/gookit/goutil/x/assert"
)
// dumpRuleSet serializes the rule-collection result of a Validation into a
// stable, comparable string. It is the golden-regression snapshot used by P3
// to guarantee the build/instantiate refactor produces a byte-identical rule
// set, translation tables and optional/filter state.
//
// Captured dimensions:
// - rules: each *Rule as "path | realName | validator | nameNotRequired |
// optional | skipEmpty | args(%#v)"; sorted for stable output.
// NOTE: args use the Go-syntax verb %#v (not %v) so that argument
// element TYPES are part of the snapshot. This is load-bearing for
// P3b (rule-parse refactor): the collection phase stores rule args
// as STRINGS (e.g. "5" not int 5), and the enum/notIn branch stores
// a single []string element, while regexp stores one raw string.
// %v would print string "5" and int 5 identically; %#v makes the
// difference visible so an accidental type change is caught.
// - filters: each *FilterRule as "fields | filters | filterArgs"; sorted.
// - optionals: field -> flag (v.optionals), sorted by field.
// - labels: v.trans.labelMap (field -> label), sorted by field.
// - fieldMap: v.trans.fieldMap (field -> output name), sorted by field.
// - messages: ONLY the entries that differ from the builtin defaults (i.e.
// custom messages added during rule collection), sorted by key.
// The builtin baseline (~150 entries copied by Translator.Reset)
// is intentionally excluded so the snapshot focuses on what rule
// collection produced.
// - defValues: v.defValues (field -> default), sorted by field.
//
// All collections are sorted before emission; map iteration order, map-of-struct
// element path order, etc. are non-deterministic in Go, so sorting is mandatory
// to keep the snapshot reproducible across runs (verified with -count=3).
func dumpRuleSet(v *Validation) string {
var b strings.Builder
// --- rules ---
ruleLines := make([]string, 0, len(v.rules))
for _, r := range v.rules {
// fields may contain multiple comma-joined names; join with "," for a
// single stable path token (rule-collection from tags always yields one
// field per rule, but be defensive).
path := strings.Join(r.fields, ",")
ruleLines = append(ruleLines, fmt.Sprintf(
"%s | real=%s | validator=%s | notRequired=%v | optional=%v | skipEmpty=%v | args=%#v",
path, r.realName, r.validator, r.nameNotRequired, r.optional, r.skipEmpty, r.arguments,
))
}
sort.Strings(ruleLines)
b.WriteString("== rules ==\n")
for _, ln := range ruleLines {
b.WriteString(ln)
b.WriteByte('\n')
}
// --- filter rules ---
filterLines := make([]string, 0, len(v.filterRules))
for _, fr := range v.filterRules {
// filterArgs is map[int]string; emit sorted by index for stability.
idxs := make([]int, 0, len(fr.filterArgs))
for i := range fr.filterArgs {
idxs = append(idxs, i)
}
sort.Ints(idxs)
args := make([]string, 0, len(idxs))
for _, i := range idxs {
args = append(args, fmt.Sprintf("%d:%s", i, fr.filterArgs[i]))
}
filterLines = append(filterLines, fmt.Sprintf(
"%s | filters=%s | args=[%s]",
strings.Join(fr.fields, ","), strings.Join(fr.filters, ","), strings.Join(args, ","),
))
}
sort.Strings(filterLines)
b.WriteString("== filters ==\n")
for _, ln := range filterLines {
b.WriteString(ln)
b.WriteByte('\n')
}
// --- optionals ---
b.WriteString("== optionals ==\n")
for _, line := range sortedMapInt8(v.optionals) {
b.WriteString(line)
b.WriteByte('\n')
}
// --- labels (trans.labelMap) ---
b.WriteString("== labels ==\n")
for _, line := range sortedMapStr(v.trans.labelMap) {
b.WriteString(line)
b.WriteByte('\n')
}
// --- field output map (trans.fieldMap) ---
b.WriteString("== fieldMap ==\n")
for _, line := range sortedMapStr(v.trans.fieldMap) {
b.WriteString(line)
b.WriteByte('\n')
}
// --- custom messages (trans.messages minus builtin defaults) ---
b.WriteString("== messages ==\n")
custom := make(map[string]string)
for k, val := range v.trans.messages {
if base, ok := builtinMessages[k]; !ok || base != val {
custom[k] = val
}
}
for _, line := range sortedMapStr(custom) {
b.WriteString(line)
b.WriteByte('\n')
}
// --- default values ---
b.WriteString("== defValues ==\n")
if len(v.defValues) > 0 {
keys := make([]string, 0, len(v.defValues))
for k := range v.defValues {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
b.WriteString(fmt.Sprintf("%s=%v\n", k, v.defValues[k]))
}
}
return b.String()
}
func sortedMapStr(m map[string]string) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
out := make([]string, 0, len(keys))
for _, k := range keys {
out = append(out, fmt.Sprintf("%s=%s", k, m[k]))
}
return out
}
func sortedMapInt8(m map[string]int8) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
out := make([]string, 0, len(keys))
for _, k := range keys {
out = append(out, fmt.Sprintf("%s=%d", k, m[k]))
}
return out
}
// ---- struct fixtures used by the golden cases ----
type rcFlat struct {
Name string `validate:"required|minLen:3"`
Email string `validate:"email"`
Age int `validate:"int|min:1|max:120"`
}
type rcInner struct {
City string `validate:"required"`
Zip string `validate:"required|minLen:3"`
}
type rcNested struct {
Name string `validate:"required"`
Addr rcInner `validate:"required"`
}
type rcPtrNested struct {
Name string `validate:"required"`
Sub *rcInner `validate:"required"`
}
type rcSliceOfStruct struct {
Name string `validate:"required"`
Items []rcInner `validate:"required"`
}
type rcMapOfStruct struct {
Name string `validate:"required"`
Items map[string]rcInner `validate:"required"`
}
// RcEmbedBase is intentionally EXPORTED so the embedded field name starts with
// an uppercase letter and is NOT skipped as unexported. This exercises the real
// anonymous-embed recursion path in parseRulesFromTag.
type RcEmbedBase struct {
BaseID int `validate:"required|min:1"`
}
type rcEmbed struct {
RcEmbedBase
Name string `validate:"required"`
}
// rcEmbedUnexported embeds an UNEXPORTED-named type; its embed field name starts
// lowercase, so (with ValidatePrivateFields=false) the whole embed and its inner
// fields are skipped. Captures the contrasting v1 behavior.
type rcEmbedBaseLower struct {
BaseID int `validate:"required|min:1"`
}
type rcEmbedUnexported struct {
rcEmbedBaseLower
Name string `validate:"required"`
}
type rcPrivate struct {
Name string `validate:"required"`
age int `validate:"required|min:1"`
}
type rcFilter struct {
Name string `validate:"required" filter:"trim|lower"`
}
type rcMsgSingle struct {
Name string `validate:"required" message:"name is required"`
}
type rcMsgNamed struct {
Name string `validate:"required|minLen:3" message:"required:name must not be empty"`
}
type rcMsgMulti struct {
Name string `validate:"required|minLen:3" message:"required:name is required|minLen:name min len is %d"`
}
type rcLabelJSON struct {
Name string `validate:"required" label:"用户名" json:"user_name"`
}
type rcDefault struct {
Name string `validate:"default:foo"`
}
type rcSafe struct {
Name string `validate:"required"`
Tmp string `validate:"safe"`
Skip string `validate:"-"`
}
type rcScene struct {
Name string `validate:"required"`
Email string `validate:"email"`
Age int `validate:"int|min:1"`
}
// ---- fixtures for the parameter-parsing (arg-form) golden cases ----
//
// These pin the EXACT shape of *Rule.arguments produced by the collection phase
// (StringRule's `switch realName`). P3b will move rule parsing to a type-level
// cache; these snapshots guarantee the arg shapes are reproduced byte-for-byte.
// single scalar args: collected as STRING "5"/"10" (not int) — the default
// (comma-split) branch runs them through parseArgString+strings2Args.
type rcArgSingle struct {
A int `validate:"min:5"`
B int `validate:"max:10"`
}
// two scalar args via between + its alias range; default branch comma-splits
// into two STRING args.
type rcArgBetween struct {
A int `validate:"between:1,10"`
B int `validate:"range:1,10"`
}
// enum / its alias in: realName=="enum" → args MERGED into a single []string
// element (one arg whose value is the []string slice).
type rcArgEnum struct {
A string `validate:"enum:a,b,c"`
B int `validate:"in:1,2,3"`
}
// notIn / its alias not_in: realName=="notIn" → same merge-into-one-[]string.
type rcArgNotIn struct {
A string `validate:"notIn:x,y,z"`
B string `validate:"not_in:p,q"`
}
// regexp / its alias regex: realName=="regexp" → args NOT comma-split; the whole
// pattern is one raw STRING arg. Use raw-string (backtick) tags so the backslash
// is literal.
type rcArgRegexp struct {
A string `validate:"regexp:\\d{4,6}"`
B string `validate:"regex:^a,b$"`
}
// length / minLen / maxLen: single int param collected as STRING via default
// branch, then PRE-CONVERTED to int by the STATIC template build (P3a), since
// the validators take a concrete `int` arg. The golden args are typed ints.
type rcArgLen struct {
A string `validate:"length:6"`
B string `validate:"minLen:3"`
C string `validate:"maxLen:20"`
}
// requiredIf: requiredXX validator → nameNotRequired=false; multi string args via
// default branch (comma-split into two STRINGs).
type rcArgRequiredIf struct {
Name string `validate:"requiredIf:other,value"`
}
// requiredWith: variadic validator; default branch comma-splits into N STRING args.
type rcArgRequiredWith struct {
Name string `validate:"requiredWith:a,b,c"`
}
// mixed pipeline on one field: exercises every arg form coexisting on one field.
type rcArgMixed struct {
Age int `validate:"required|int|min:1|max:120|enum:1,2,3"`
}
func TestRuleCompat_golden(t *testing.T) {
t.Run("flat", func(t *testing.T) {
v := Struct(&rcFlat{})
want := `== rules ==
Age | real=isInt | validator=int | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}(nil)
Age | real=max | validator=max | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"120"}
Age | real=min | validator=min | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"1"}
Email | real=isEmail | validator=email | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}(nil)
Name | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{3}
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("nested", func(t *testing.T) {
v := Struct(&rcNested{})
want := `== rules ==
Addr | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Addr.City | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Addr.Zip | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{3}
Addr.Zip | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("ptr_nested_nil", func(t *testing.T) {
v := Struct(&rcPtrNested{})
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Sub | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Sub.City | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Sub.Zip | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"3"}
Sub.Zip | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("ptr_nested_nonnil", func(t *testing.T) {
v := Struct(&rcPtrNested{Sub: &rcInner{}})
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Sub | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Sub.City | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Sub.Zip | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"3"}
Sub.Zip | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("slice_of_struct", func(t *testing.T) {
v := Struct(&rcSliceOfStruct{Items: []rcInner{{}, {}, {}}})
want := `== rules ==
Items | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.0.City | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.0.Zip | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"3"}
Items.0.Zip | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.1.City | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.1.Zip | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"3"}
Items.1.Zip | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.2.City | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.2.Zip | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"3"}
Items.2.Zip | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("map_of_struct", func(t *testing.T) {
v := Struct(&rcMapOfStruct{Items: map[string]rcInner{"home": {}, "work": {}}})
want := `== rules ==
Items | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.home.City | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.home.Zip | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"3"}
Items.home.Zip | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.work.City | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Items.work.Zip | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"3"}
Items.work.Zip | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("embed_anonymous", func(t *testing.T) {
v := Struct(&rcEmbed{})
// NOTE: an anonymous embed of an EXPORTED-named type recurses and the
// embedded field rules are collected under a dotted path
// "RcEmbedBase.BaseID" (NOT flattened to "BaseID"). Captures v1 behavior.
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
RcEmbedBase.BaseID | real=min | validator=min | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"1"}
RcEmbedBase.BaseID | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("embed_anonymous_unexported", func(t *testing.T) {
v := Struct(&rcEmbedUnexported{})
// NOTE: an embed whose field name starts lowercase is skipped as
// unexported (ValidatePrivateFields=false), so the embedded BaseID rule
// is NOT collected. Contrasts with the exported-embed case above.
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("private_fields", func(t *testing.T) {
defer func() {
ResetOption()
ResetTypeCache()
}()
Config(func(opt *GlobalOption) { opt.ValidatePrivateFields = true })
v := Struct(&rcPrivate{})
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
age | real=min | validator=min | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"1"}
age | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("filter", func(t *testing.T) {
v := Struct(&rcFilter{})
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
Name | filters=trim,lower | args=[]
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("message_single", func(t *testing.T) {
v := Struct(&rcMsgSingle{})
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
Name.required=name is required
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("message_named", func(t *testing.T) {
v := Struct(&rcMsgNamed{})
want := `== rules ==
Name | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{3}
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
Name.required=name must not be empty
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("message_multi", func(t *testing.T) {
v := Struct(&rcMsgMulti{})
want := `== rules ==
Name | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{3}
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
Name.minLen=name min len is %d
Name.required=name is required
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("label_json", func(t *testing.T) {
v := Struct(&rcLabelJSON{})
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
Name=用户名
== fieldMap ==
Name=user_name
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("default", func(t *testing.T) {
v := Struct(&rcDefault{})
// "default:foo" registers a default value (not a rule); no rule is added.
want := `== rules ==
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
Name=foo
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("safe_and_dash", func(t *testing.T) {
v := Struct(&rcSafe{})
// "safe" and "-" are collected as ordinary rules (validator name = literal
// "safe" / "-"); their semantics (skip / no-op) are handled at validate time.
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
Skip | real=- | validator=- | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}(nil)
Tmp | real=safe | validator=safe | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("scene", func(t *testing.T) {
v := Struct(&rcScene{}).WithScenes(SValues{
"create": []string{"Name", "Email"},
"update": []string{"Name"},
}).AtScene("create")
// scene does not change the collected rule set itself; it only filters at
// validate time. Capture the full collected rules + scene name.
want := `== rules ==
Age | real=isInt | validator=int | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}(nil)
Age | real=min | validator=min | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"1"}
Email | real=isEmail | validator=email | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}(nil)
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
scene=create
`
assert.Eq(t, want, dumpRuleSet(v)+"scene="+v.Scene()+"\n")
})
t.Run("check_sub_on_parent_marked", func(t *testing.T) {
defer func() {
ResetOption()
ResetTypeCache()
}()
Config(func(opt *GlobalOption) { opt.CheckSubOnParentMarked = true })
// Addr has NO validate rule on the parent field, so with
// CheckSubOnParentMarked=true the sub-struct rules must NOT be collected.
type parent struct {
Name string `validate:"required"`
Addr rcInner // no rule on parent field
}
v := Struct(&parent{})
want := `== rules ==
Name | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
}
// TestRuleCompat_argForms pins the exact shape of *Rule.arguments produced by the
// collection phase for every special / general parameter form handled by
// StringRule's `switch realName`. Golden args use %#v so element TYPES are part of
// the snapshot (see dumpRuleSet doc). This is the most P3b-sensitive snapshot.
func TestRuleCompat_argForms(t *testing.T) {
t.Run("single_scalar", func(t *testing.T) {
// min:5 / max:10 — default branch: arg collected as STRING "5"/"10", NOT int.
v := Struct(&rcArgSingle{})
want := `== rules ==
A | real=min | validator=min | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"5"}
B | real=max | validator=max | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"10"}
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("between_and_range_alias", func(t *testing.T) {
// between:1,10 and alias range:1,10 — default branch: two STRING args.
// validator keeps the input name (between / range); realName resolves both
// to "between".
v := Struct(&rcArgBetween{})
want := `== rules ==
A | real=between | validator=between | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"1", "10"}
B | real=between | validator=range | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"1", "10"}
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("enum_and_in_alias_merged", func(t *testing.T) {
// enum:a,b,c / in:1,2,3 — realName=="enum": args MERGED into ONE element
// whose value is a []string slice.
v := Struct(&rcArgEnum{})
want := `== rules ==
A | real=enum | validator=enum | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{[]string{"a", "b", "c"}}
B | real=enum | validator=in | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{[]string{"1", "2", "3"}}
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("notIn_and_not_in_alias_merged", func(t *testing.T) {
// notIn:x,y,z / not_in:p,q — realName=="notIn": same single []string merge.
v := Struct(&rcArgNotIn{})
want := `== rules ==
A | real=notIn | validator=notIn | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{[]string{"x", "y", "z"}}
B | real=notIn | validator=not_in | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{[]string{"p", "q"}}
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("regexp_and_regex_alias_nosplit", func(t *testing.T) {
// regexp / regex — realName=="regexp": args NOT comma-split; the WHOLE
// pattern is one raw STRING arg (note B's pattern contains a comma which is
// preserved verbatim).
v := Struct(&rcArgRegexp{})
want := `== rules ==
A | real=regexp | validator=regexp | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"\\d{4,6}"}
B | real=regexp | validator=regex | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"^a,b$"}
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("length_variants", func(t *testing.T) {
// length:6 / minLen:3 / maxLen:20 — default branch collects STRING args,
// then the STATIC template build pre-converts them to typed int (P3a).
v := Struct(&rcArgLen{})
want := `== rules ==
A | real=length | validator=length | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{6}
B | real=minLength | validator=minLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{3}
C | real=maxLength | validator=maxLen | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{20}
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("requiredIf_multi_string_args", func(t *testing.T) {
// requiredIf:other,value — requiredXX → nameNotRequired=false; default branch
// comma-splits into two STRING args.
v := Struct(&rcArgRequiredIf{})
want := `== rules ==
Name | real=requiredIf | validator=requiredIf | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}{"other", "value"}
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("requiredWith_variadic", func(t *testing.T) {
// requiredWith:a,b,c — variadic validator; default branch: N STRING args.
v := Struct(&rcArgRequiredWith{})
want := `== rules ==
Name | real=requiredWith | validator=requiredWith | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}{"a", "b", "c"}
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
t.Run("mixed_pipeline_one_field", func(t *testing.T) {
// required|int|min:1|max:120|enum:1,2,3 on one field — verifies every arg
// form coexists correctly under a single field path.
v := Struct(&rcArgMixed{})
want := `== rules ==
Age | real=enum | validator=enum | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{[]string{"1", "2", "3"}}
Age | real=isInt | validator=int | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}(nil)
Age | real=max | validator=max | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"120"}
Age | real=min | validator=min | notRequired=true | optional=false | skipEmpty=true | args=[]interface {}{"1"}
Age | real=required | validator=required | notRequired=false | optional=false | skipEmpty=true | args=[]interface {}(nil)
== filters ==
== optionals ==
== labels ==
== fieldMap ==
== messages ==
== defValues ==
`
assert.Eq(t, want, dumpRuleSet(v))
})
}