Repository navigation
Expand file tree
/
Copy pathissues_x2_test.go
More file actions
962 lines (854 loc) · 36.8 KB
/
Copy pathissues_x2_test.go
File metadata and controls
962 lines (854 loc) · 36.8 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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
package validate_test
import (
"mime/multipart"
"net/url"
"regexp"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/x/assert"
"github.com/gookit/validate/v2"
)
// https://github.com/gookit/validate/v2/issues/227
// 一个 key 包含多个上传文件时,除了第一个文件,其他文件被丢弃,导致 BindSafeData 行为非预期
func Test_Issue227(t *testing.T) {
type UserForm struct {
Name string
File []*multipart.FileHeader
}
d := validate.FromURLValues(url.Values{
"name": {"inhere"},
"age": {"30"},
})
// add files
d.AddFile("File", &multipart.FileHeader{Filename: "test1.txt"}, &multipart.FileHeader{Filename: "test2.txt"})
v := d.Create()
v.AddRule("File", "min_len", 1)
r := v.ValidateR()
assert.True(t, r.IsOK())
dump.P(r.Errors)
assert.Nil(t, r.Errors.ErrOrNil())
u := &UserForm{}
err := r.BindStruct(u)
assert.NoError(t, err)
dump.P(u)
}
// https://github.com/gookit/validate/v2/issues/259 Embedded structs are not validated properly #259
//
// not-a-bug in v2.0(核心问题已修): issue 标题"嵌入结构体未被正确验证"在 v1.5.2 下
// 的根因是匿名嵌入结构体的字段根本没有被级联验证。v2.0(T10)起匿名嵌入结构体始终级联
// (豁免 CheckSubOnParentMarked), 因此 NameField.Name / BranchField.Branch 上的
// `required` 规则现在会被求值 —— 嵌入字段已被正确验证。
//
// 报告者期望 perm2(Type=remove)整体通过, 其心智模型是: 嵌入字段 UserData 上的
// `required_if:Type,give` 不满足时, 应"门控"住其内部所有 required 规则。但 gookit
// 的设计是: 嵌入字段被展平后, 内部 `required` 规则是独立求值的, 不受外层
// required_if 门控。所以 perm2 仍会因 name/branch 为空而失败 —— 这是设计行为, 不是
// 缺陷; 正确写法应在内部字段上用条件规则。下面断言 v2.0 的真实行为。
func TestIssue_259_v2(t *testing.T) {
type NameField struct {
Name string `json:"name" validate:"required|max_len:5000"`
}
type BranchField struct {
Branch string `json:"branch" validate:"required|min_len:32|max_len:32"`
}
type UserData struct {
NameField `json:",inline"`
BranchField `json:",inline"`
}
type Permission struct {
UserData `json:",inline" validate:"required_if:Type,give"`
Type string `json:"type" validate:"required|in:give,remove"`
Access string `json:"access" validate:"required_if:Type,remove"`
}
t.Run("embedded fields are now cascaded and validated (give+empty fails)", func(t *testing.T) {
perm1 := Permission{UserData: UserData{}, Type: "give"}
v1 := validate.Struct(perm1)
v1.StopOnError = false
// give 场景下 Name/Branch 必填, 空值应失败(证明嵌入字段确实被验证了)
assert.False(t, v1.Validate())
assert.ErrSubMsg(t, v1.Errors, "name is required")
})
t.Run("inner required is not gated by outer required_if (design behavior)", func(t *testing.T) {
perm2 := Permission{Type: "remove", Access: "change_types"}
v2 := validate.Struct(&perm2)
v2.StopOnError = false
// 嵌入字段被展平, 内部 required 独立求值, 不受外层 required_if 门控,
// 因此 name/branch 为空仍报错 —— 这是设计行为, 非缺陷。
assert.False(t, v2.Validate())
assert.ErrSubMsg(t, v2.Errors, "name is required")
assert.ErrSubMsg(t, v2.Errors, "branch is required")
})
}
// https://github.com/gookit/validate/v2/issues/272 eqField对于指针类型数据无法正确校验
func Test_Issue272(t *testing.T) {
type T272 struct {
FieldA *string `validate:"required"`
FieldB *string `validate:"required|eqField:FieldA"`
}
// test eqField
var str = "abc"
var str1 = "bcd"
v := validate.Struct(&T272{
FieldA: &str,
FieldB: &str1,
})
assert.False(t, v.Validate())
assert.Len(t, v.Errors, 1)
assert.ErrSubMsg(t, v.Errors, "FieldB value must be equal the field FieldA")
var str2 = "abc"
v = validate.Struct(&T272{
FieldA: &str,
FieldB: &str2,
})
assert.True(t, v.Validate())
assert.Nil(t, v.Errors.ErrOrNil())
// nil value
v = validate.Struct(&T272{
FieldA: nil,
FieldB: nil,
})
assert.False(t, v.Validate())
assert.Len(t, v.Errors, 1)
assert.ErrSubMsg(t, v.Errors, "FieldA is required")
}
// https://github.com/gookit/validate/v2/issues/316
// The int validator failed to validate a number exceeds the range of int64
func Test_Issue316(t *testing.T) {
data := []byte(`{"value": 9223372036854775807}`)
t.Run("not use filter", func(t *testing.T) {
dataFace, err := validate.FromJSONBytes(data)
assert.NoErr(t, err)
v := dataFace.Create()
v.StringRule("value", "int")
assert.False(t, v.Validate())
dump.P(v.Errors)
assert.Err(t, v.Errors.ErrOrNil())
assert.Equal(t, "value value must be an integer", v.Errors.One())
})
t.Run("use filter", func(t *testing.T) {
dataFace, err := validate.FromJSONBytes(data)
assert.NoErr(t, err)
v := dataFace.Create()
v.FilterRule("value", "int64")
v.StringRule("value", "int")
r := v.ValidateR()
assert.True(t, r.IsOK())
assert.Nil(t, r.Errors.ErrOrNil())
dump.P(r.SafeData())
})
}
// https://github.com/gookit/validate/v2/issues/217 Nested resources are evaluated differently
//
// FIXED: Nested.Samples 是 []Sample, Sample.Val 是 *bool 且带 required。某元素的 Val
// 指向 false(非 nil 指针)时, 切片内子结构体的 required 曾把它误判为"空"而报错; 现已与
// 顶层路径一致, *bool->false 视为存在、正确通过。
//
// 根因: data_source.go 子结构体取值路径(tryGet 的 fieldAtSubStruct 分支)在导航循环里
// 把叶子 *bool 一并解引用成 bool(false), required 的 IsEmpty(bool(false))==true 误判为
// 空; 而顶层路径保留非 nil 指针(IsEmpty(非nil指针)==false), 两路径不一致 —— 这正是
// issue 标题 "Nested resources are evaluated differently" 所指。
//
// 修复: 子结构体导航循环对**叶子节点**保留非 nil 指针(不再解引用), 与顶层路径对齐;
// 中间节点仍解引用以便继续导航; nil 叶子指针仍按"不存在"处理。
func TestIssue_217_v2(t *testing.T) {
type Sample struct {
Val *bool `validate:"required"`
}
type Nested struct {
Samples []Sample `validate:"slice"`
}
t.Run("top-level *bool->false passes required (correct)", func(t *testing.T) {
val := false
v := validate.Struct(Sample{Val: &val})
// 顶层路径把 bool 视为存在, *bool->false 正确通过
assert.True(t, v.Validate())
})
t.Run("nil *bool fails required (correct)", func(t *testing.T) {
v := validate.Struct(Sample{Val: nil})
assert.False(t, v.Validate())
})
t.Run("slice element *bool->false passes required (fixed)", func(t *testing.T) {
val, val2 := false, true
data := Nested{Samples: []Sample{{Val: &val}, {Val: &val2}}}
v := validate.Struct(data)
v.StopOnError = false
// 两个元素的 Val 都非 nil, 应全部通过, 不再误报 Samples.0.Val 为空。
ok := v.Validate()
assert.True(t, ok)
assert.Empty(t, v.Errors)
})
t.Run("slice element nil *bool still fails required (correct)", func(t *testing.T) {
val := true
data := Nested{Samples: []Sample{{Val: nil}, {Val: &val}}}
v := validate.Struct(data)
v.StopOnError = false
ok := v.Validate()
assert.False(t, ok)
assert.ErrSubMsg(t, v.Errors, "Samples.0.Val is required")
assert.NotContains(t, v.Errors.String(), "Samples.1.Val")
})
}
// --- #235 support types (custom-validator method must be on a named pkg-level type) ---
type issue235Node struct {
Name string
Location string
}
// issue235Config 用 issue 报告里推荐的"自定义校验器"写法验证 slice-of-struct。
// 注意 v2.0 下需要给 Nodes 字段加 validate tag(此处 customFunction)才会被收集。
type issue235Config struct {
Nodes []issue235Node `validate:"customFunction"`
}
// CustomFunction 在父结构体上校验整个切片: 任一元素若有 Location 但缺 Name 则失败。
func (c issue235Config) CustomFunction(nodes []issue235Node) bool {
for k := range nodes {
if nodes[k].Location != "" && nodes[k].Name == "" {
return false
}
}
return true
}
func (c issue235Config) Messages() map[string]string {
return validate.MS{"customFunction": "each {field} needs `Name` set if `Location` is set"}
}
// https://github.com/gookit/validate/v2/issues/235 How do I validate slice of struct inside a struct?
//
// partial in v2.0:
// - 报告者最初的写法 `required_with:Nodes..Location`(跨切片元素的路径式 required_with)
// 仍不能按预期工作: 即便第 1 个元素没有 Location, 它仍报 Nodes.1.Name 必填。这条
// 路径式跨元素引用方式 v2.0 依旧不支持(still-broken, 见 t.Run "path-based...")。
// - issue 自带的"在切片字段上挂自定义校验器"workaround 在 v2.0 下完全可用
// (resolved): 给 Nodes 加 `validate:"customFunction"` tag 即可触发并逐元素校验,
// 这也是推荐解法。
func TestIssue_235_v2(t *testing.T) {
t.Run("path-based required_with across slice elements still misbehaves", func(t *testing.T) {
type Node struct {
Name string `validate:"required_with:Nodes..Location"`
Location string
}
type Config struct {
Nodes []Node `validate:""`
}
// 第 2 个元素既无 Location 也无 Name, 按 required_with 语义不该报错,
// 但 v2.0 仍把 Nodes.1.Name 判为必填 —— 路径式跨元素引用未生效。
data := &Config{Nodes: []Node{{Name: "node1", Location: "A"}, {}}}
v := validate.Struct(data)
v.StopOnError = false
ok := v.Validate()
t.Logf("v2.0 path-based required_with #235: ok=%v errors=%v", ok, v.Errors)
assert.False(t, ok)
assert.ErrSubMsg(t, v.Errors, "Nodes.1.Name")
})
t.Run("custom-validator workaround works (recommended)", func(t *testing.T) {
// good: 每个有 Location 的元素都有 Name
good := &issue235Config{Nodes: []issue235Node{{Name: "n1", Location: "A"}, {}}}
vg := validate.Struct(good)
vg.StopOnError = false
assert.True(t, vg.Validate())
// bad: 有元素含 Location 但缺 Name
bad := &issue235Config{Nodes: []issue235Node{{Location: "B"}}}
vb := validate.Struct(bad)
vb.StopOnError = false
assert.False(t, vb.Validate())
assert.ErrSubMsg(t, vb.Errors, "needs `Name` set if `Location` is set")
})
}
// https://github.com/gookit/validate/v2/issues/232
// 在校验 struct A 时, 其成员 a 是另一个 struct B 的 slice, 如何让 A.a 的 tag 触发对
// 每个 B 元素的字段规则(如 B 的 validateb)进行校验。
//
// resolved in v2.0: 只要给切片字段 a 加上 `validate` tag(空 tag `validate:""` 也算),
// v2.0 就会下探到每个 B 元素并应用 B 自身的字段规则(CheckSubOnParentMarked 行为)。
// 这正是该提问的答案: 给 A.a 加 validate tag 即可触发对 B 的级联验证; 不加 tag 则
// 不级联。
func TestIssue_232_v2(t *testing.T) {
type B struct {
Bval string `validate:"required|min_len:3"`
}
t.Run("slice field WITH validate tag cascades into each B", func(t *testing.T) {
type A struct {
Items []B `validate:"required"`
}
// 元素 Bval 太短, 应触发 B 的 min_len 规则
v := validate.Struct(A{Items: []B{{Bval: "x"}}})
v.StopOnError = false
assert.False(t, v.Validate())
assert.ErrSubMsg(t, v.Errors, "Items.0.Bval min length is 3")
// 合法元素应通过
ok := validate.Struct(A{Items: []B{{Bval: "hello"}}})
assert.True(t, ok.Validate())
})
t.Run("slice field WITHOUT validate tag does not cascade (v2.0 design)", func(t *testing.T) {
type A struct {
Items []B
}
// 无 validate tag, v2.0 不下探, 即便元素非法也通过
v := validate.Struct(A{Items: []B{{Bval: "x"}}})
v.StopOnError = false
assert.True(t, v.Validate())
})
}
// --- #283 support type (ConfigValidation/CustomValidator methods need pkg-level type) ---
type issue283Tag struct {
Id string `validate:"required"`
Name string `validate:"required"`
Date string `validate:"required"`
}
type issue283Form struct {
Name string `validate:"required|min_len:7"`
Code string `validate:"required|customValidator"`
Tags []issue283Tag `validate:"required"`
Test int `validate:"required|greaterThan:1"`
}
func (f issue283Form) CustomValidator(val string) bool { return len(val) == 4 }
func (f issue283Form) ConfigValidation(v *validate.Validation) {
v.WithScenes(validate.SValues{
// 报告者写法: 用不带索引的 "Tags.Id" 想校验切片元素
"update": []string{"Name", "Tags.Id", "Test"},
"updateStar": []string{"Tags.*.Id"},
"updateIdx": []string{"Tags.0.Id"},
})
}
// https://github.com/gookit/validate/v2/issues/283 Scenes does not work in slices
//
// FIXED(通配 .*): 级联为切片元素生成带索引的规则名 "Tags.0.Id", 场景现支持用通配
// "Tags.*.Id" 命中它 —— 把字段名的数字索引段规范化为 "*" 后与场景通配项比对。这正是
// issue 标题 "Scenes does not work in slices" 的核心诉求(选择性校验切片元素的某个字段)。
//
// 范围: 仅支持 ".*" 通配写法。**无索引** "Tags.Id" 仍不命中(与真实嵌套字段 "Tags.Id"
// 有语义歧义, 按设计不做), 推荐统一用 "Tags.*.Id"。显式索引 "Tags.0.Id" 仍可用。
func TestIssue_283_v2(t *testing.T) {
newForm := func() *issue283Form {
return &issue283Form{
Name: "inhere", Code: "asd", Test: 1,
Tags: []issue283Tag{{Id: "", Name: "", Date: ""}},
}
}
t.Run("scene 'Tags.Id' index-less still does NOT match (by design, use .*)", func(t *testing.T) {
v := validate.Struct(newForm(), "update")
v.StopOnError = false
ok := v.Validate("update")
// Name(太短)与 Test 报错, 但 Tags.0.Id 不被无索引 "Tags.Id" 命中(无 Tags 错误)
assert.False(t, ok)
assert.NotContains(t, v.Errors.String(), "Tags")
})
t.Run("scene 'Tags.*.Id' wildcard validates slice element (fixed)", func(t *testing.T) {
v := validate.Struct(newForm(), "updateStar")
v.StopOnError = false
ok := v.Validate("updateStar")
// 通配命中 Tags.0.Id, 空 Id 正确报错; Name/Date 不在场景内, 不被校验
assert.False(t, ok)
assert.ErrSubMsg(t, v.Errors, "Tags.0.Id is required")
assert.NotContains(t, v.Errors.String(), "Tags.0.Name")
assert.NotContains(t, v.Errors.String(), "Tags.0.Date")
// Name 太短(min_len:7)但不在 updateStar 场景内, 不应报错
assert.NotContains(t, v.Errors.String(), "Name")
})
t.Run("scene 'Tags.0.Id' explicit index DOES validate (workaround still valid)", func(t *testing.T) {
v := validate.Struct(newForm(), "updateIdx")
v.StopOnError = false
ok := v.Validate("updateIdx")
// 显式数字索引可命中, 空 Id 正确报错
assert.False(t, ok)
assert.ErrSubMsg(t, v.Errors, "Tags.0.Id is required")
})
}
// --- #314 support types (ConfigValidation needs pkg-level type) ---
type issue314Sub struct {
A string
}
type issue314Struct struct {
SubStruct *issue314Sub `validate:"required"`
}
func (issue314Struct) ConfigValidation(v *validate.Validation) {
v.WithScenes(validate.SValues{
"SubStruct": []string{"SubStruct"},
"None": []string{""}, // 期望: 此场景下什么都不校验
})
}
// https://github.com/gookit/validate/v2/issues/314 Scene with empty validation rules now fails
//
// FIXED: 场景 "None": []string{""} 表示"不校验任何字段", 现已恢复 v1.4.5 行为, 不再误报
// "SubStruct is required"。
//
// 根因: sceneFieldMap() 曾把空字符串 "" 当作普通场景字段键, 得到 sceneFields={"":1}; 随后
// isNotNeedToCheck("SubStruct") 里 `strings.Join(fields[0:0], ".")` 得到 "" 恰好命中,
// 于是所有字段反而都被纳入校验。
//
// 修复: sceneFieldMap() 跳过空字段(但保留非 nil map 以区分"无场景"); isNotNeedToCheck()
// 用 nil/非nil 区分"未设场景(校验全部)"与"场景已激活但无字段(什么都不校验)"。
func TestIssue_314_v2(t *testing.T) {
t.Run("empty-rule scene 'None' validates nothing (fixed)", func(t *testing.T) {
foo := issue314Struct{}
err := validate.Struct(&foo).ValidateE("None")
// 期望: err 为空(此场景什么都不校验)。
assert.Empty(t, err)
})
t.Run("scene 'SubStruct' validates SubStruct (correct)", func(t *testing.T) {
foo := issue314Struct{}
err := validate.Struct(&foo).ValidateE("SubStruct")
assert.NotEmpty(t, err)
assert.StrContains(t, err.String(), "SubStruct is required")
})
}
// https://github.com/gookit/validate/v2/issues/327 未指定 required 时没有获取到对应字段的值
//
// not-a-bug in v2.0(设计行为, 已可正常工作): 报告者抱怨"未填写 required 的字段
// (如 bbb)取不到绑定值"。实际规则是: 只有"参与了校验且通过"的字段才会进入 safeData,
// 进而能被 BindStruct 绑定。
// - bbb 加了非 required 规则(如 string)且有非空值 -> 正常进入 safeData 并绑定(OK)。
// - bbb 完全没有规则, 或值为空被 SkipOnEmpty 跳过 -> 不进入 safeData, 自然取不到。
//
// 这与 required 无关, 是"safeData 只含校验过的数据"的设计。报告者把它当 BUG 实为
// 用法误解。AddMessages 不写 required 项也不会影响其它字段的消息(下面一并验证)。
func TestIssue_327_v2(t *testing.T) {
t.Run("non-required rule with value IS captured & bound", func(t *testing.T) {
data := validate.FromMap(map[string]any{"aaa": "hello", "bbb": "world"})
v := data.Create()
v.StringRule("aaa", "required|string|minLen:4")
v.StringRule("bbb", "string") // 非 required 规则
r := v.ValidateR()
assert.True(t, r.IsOK())
var reqData struct {
Aaa string `json:"aaa"`
Bbb string `json:"bbb"`
}
assert.NoErr(t, r.BindStruct(&reqData))
// 非 required 的 bbb 同样被绑定到了
assert.Eq(t, "hello", reqData.Aaa)
assert.Eq(t, "world", reqData.Bbb)
assert.Eq(t, "world", r.SafeData()["bbb"])
})
t.Run("field WITHOUT any rule is not in safeData (design)", func(t *testing.T) {
data := validate.FromMap(map[string]any{"aaa": "hello", "bbb": "world"})
v := data.Create()
v.StringRule("aaa", "required|string|minLen:4")
// bbb 没有任何规则
r := v.ValidateR()
assert.True(t, r.IsOK())
// 没有规则的字段不进入 safeData -> 取不到, 这是设计而非缺陷
_, ok := r.SafeData()["bbb"]
assert.False(t, ok)
})
t.Run("AddMessages without 'required' entry does not break other field messages", func(t *testing.T) {
data := validate.FromMap(map[string]any{"aaa": "x", "bbb": "world"})
v := data.Create()
v.StringRule("aaa", "required|string|minLen:4")
v.StringRule("bbb", "string")
// 故意不提供 aaa.required 的自定义消息
v.AddMessages(map[string]string{
"aaa.minLen": "长度不少于 4 个字符",
"bbb.string": "格式不正确",
})
assert.False(t, v.Validate())
// aaa.minLen 的自定义消息仍生效, 其它字段消息未被吞掉
assert.StrContains(t, v.Errors.String(), "长度不少于 4 个字符")
})
}
// https://github.com/gookit/validate/v2/issues/262 filter 无法对切片中的元素应用
//
// FIXED: 通配路径 "ports.*.container_start" 给切片元素挂 filter, 现已逐元素应用,
// 不再报 `convert value type error`。
//
// 根因: 通配路径经 GetByPath 取到整个切片 []any{80}(而非逐个标量), 旧逻辑把 filter("int")
// 应用到整个 slice -> 当单个值转 int 失败。filter 链路缺少验证器侧 validateWildcardSlice
// 那样的 ".*" 逐元素展开。
//
// 修复: FilterRule.Apply 检测到字段含 ".*" 且取值为切片时, 逐元素 apply filter, 结果作为
// 收集切片写回 filteredData[通配key]; 验证器经 tryGet 优先读 filteredData, 形态天然对齐,
// 逐元素校验直接跑通。范围: 先支持 Map/JSON 源、单级 ".*"。
func TestIssue_262_v2(t *testing.T) {
jsonStr := `{"ports":[{"container_start":80,"container_end":81,"protocol":"tcp"},{"container_start":90,"container_end":91,"protocol":"udp"}]}`
t.Run("wildcard filter on slice elements applies per-element (fixed)", func(t *testing.T) {
v, err := validate.FromJSON(jsonStr)
assert.NoErr(t, err)
vv := v.Create()
vv.FilterRule("ports.*.container_start", "int")
vv.FilterRule("ports.*.container_end", "int")
vv.AddRule("ports.*.container_start", "int")
vv.AddRule("ports.*.container_end", "int")
vv.AddRule("ports.*.protocol", "string")
rr := vv.ValidateR()
assert.True(t, rr.IsOK())
assert.Empty(t, rr.Errors)
// 逐元素转为 int 后, 收集切片写回到通配 key
starts, hasStart := rr.SafeData()["ports.*.container_start"]
assert.True(t, hasStart)
assert.Eq(t, []any{80, 90}, starts)
})
t.Run("explicit index filter works (workaround still valid)", func(t *testing.T) {
v, err := validate.FromJSON(jsonStr)
assert.NoErr(t, err)
vv := v.Create()
// 显式数字索引可命中标量值, filter 正常
vv.FilterRule("ports.0.container_start", "int")
vv.AddRule("ports.0.container_start", "int")
rr := vv.ValidateR()
assert.True(t, rr.IsOK())
assert.Eq(t, 80, rr.SafeData()["ports.0.container_start"])
})
t.Run("wildcard custom filter applies per-element (fixed)", func(t *testing.T) {
v, err := validate.FromJSON(jsonStr)
assert.NoErr(t, err)
vv := v.Create()
// 自定义 filter 走 callCustomFilter 分支, 同样应逐元素生效
vv.AddFilter("plus10", func(val any) (int, error) {
n, e := mathutil.ToInt(val)
return n + 10, e
})
vv.FilterRule("ports.*.container_start", "plus10")
vv.AddRule("ports.*.container_start", "int")
rr := vv.ValidateR()
assert.True(t, rr.IsOK())
assert.Eq(t, []any{90, 100}, rr.SafeData()["ports.*.container_start"])
})
}
// https://github.com/gookit/validate/v2/issues/138 FullUrl regex needs improvement
//
// FIXED: 旧 fullUrl 正则 `^...://[\w.\/#=?&-_%]+$` 字符类过宽, 把非法 URL 判为合法。
// 新正则要求 scheme + 结构化 host(domain.tld 或 IPv4) + 可选 port + 可选 path/query,
// issue 三个反例现已被拒:
// - "https://www.googl_?e.com/testme"(host 含下划线/非法结构)
// - "https://www"(无 TLD)
// - "https://not%23"(host 含非法字符且无 TLD)
//
// IsURL 是宽松的 URI-reference 检查(其自身测试要求 "123"、"/users/profile/1" 合法,
// 故保持宽松); 仅补一个空白字符护栏, 让 "not a url" 这种含空格的串被拒。
func TestIssue_138_v2(t *testing.T) {
t.Run("fullUrl rejects invalid URLs (fixed)", func(t *testing.T) {
invalid := []string{
"https://www.googl_?e.com/testme",
"https://www",
"https://not%23",
}
for _, s := range invalid {
assert.False(t, validate.IsFullURL(s), "expected %q to be rejected now", s)
}
})
t.Run("fullUrl still accepts valid URLs (no regression)", func(t *testing.T) {
valid := []string{
"http://example.com",
"https://www.google.com/testme",
"https://www.google.com/test_me", // 下划线在 path 里仍合法
"http://www.google.com/test?a=2%25a&b=c",
"ftp://files.example.com/a",
"http://192.168.0.1/path", // IPv4 host
"http://a.com:8080/x", // 带端口
}
for _, s := range valid {
assert.True(t, validate.IsFullURL(s), "expected %q to stay valid", s)
}
})
t.Run("IsURL stays loose but rejects whitespace (fixed)", func(t *testing.T) {
// 含空格的串不再被误判为合法
assert.False(t, validate.IsURL("not a url"))
// 宽松语义保持: 相对引用/路径/裸串仍合法
assert.True(t, validate.IsURL("a.com?p=1"))
assert.True(t, validate.IsURL("/users/profile/1"))
assert.True(t, validate.IsURL("123"))
})
}
// --- #324 support types (nested form-data binding) ---
type issue324Address struct {
Street string `form:"street" json:"street"`
City string `form:"city" json:"city"`
}
type issue324Member struct {
Name string `form:"name" json:"name"`
Address issue324Address `form:"address" json:"address"`
}
// https://github.com/gookit/validate/v2/issues/324 Nested Form Data Binding Fails for multipart/form-data
//
// FIXED(对象嵌套): multipart/form-data 的嵌套字段(bracket "address[street]" 或 dot
// "address.street")现可绑定到嵌套 struct。
//
// 根因: FromURLValues 把表单键原样平铺进 d.Form; BindSafeData 把 safeData(平铺 map)直接
// marshal, 平铺键不变成嵌套对象。
//
// 修复: ① FromURLValues 用 normalizeFormKey 把 bracket 归一为点路径(address[street]->
// address.street); ② BindSafeData 用 maputil.SetByPath 把点路径 safeData 展开成嵌套 map
// 再绑定。前提: nested 字段要有点路径规则才进 safeData(与 map/form 既有语义一致)。
// 范围: 仅对象嵌套; 数组下标 bracket(items[0][name])留后续。
func TestIssue_324_v2(t *testing.T) {
run := func(t *testing.T, street, city string) issue324Member {
t.Helper()
form := url.Values{}
form.Set("name", "John")
form.Set(street, "Main St")
form.Set(city, "New York")
data := validate.FromURLValues(form)
v := data.Create()
v.StringRule("name", "required")
// nested 字段用点路径声明规则, 才会进入 safeData
v.StringRule("address.street", "required")
v.StringRule("address.city", "required")
r := v.ValidateR()
assert.True(t, r.IsOK())
var req issue324Member
assert.NoErr(t, r.BindSafeData(&req))
return req
}
t.Run("bracket notation address[street] binds nested (fixed)", func(t *testing.T) {
req := run(t, "address[street]", "address[city]")
assert.Eq(t, "John", req.Name)
assert.Eq(t, "Main St", req.Address.Street)
assert.Eq(t, "New York", req.Address.City)
})
t.Run("dot notation address.street binds nested (fixed)", func(t *testing.T) {
req := run(t, "address.street", "address.city")
assert.Eq(t, "John", req.Name)
assert.Eq(t, "Main St", req.Address.Street)
assert.Eq(t, "New York", req.Address.City)
})
t.Run("multi-level bracket a[b][c] binds (fixed)", func(t *testing.T) {
type inner struct {
No string `json:"no"`
}
type outer struct {
Addr struct {
Street inner `json:"street"`
} `json:"addr"`
}
form := url.Values{}
form.Set("addr[street][no]", "12")
v := validate.FromURLValues(form).Create()
v.StringRule("addr.street.no", "required")
r := v.ValidateR()
assert.True(t, r.IsOK())
var got outer
assert.NoErr(t, r.BindSafeData(&got))
assert.Eq(t, "12", got.Addr.Street.No)
})
t.Run("nested field without a rule is not bound (safeData semantics kept)", func(t *testing.T) {
form := url.Values{}
form.Set("name", "John")
form.Set("address[street]", "Main St")
v := validate.FromURLValues(form).Create()
v.StringRule("name", "required") // 不给 address.street 规则
r := v.ValidateR()
assert.True(t, r.IsOK())
var req issue324Member
assert.NoErr(t, r.BindSafeData(&req))
assert.Eq(t, "John", req.Name)
assert.Eq(t, "", req.Address.Street) // 未校验 -> 不进 safeData -> 不绑定
})
}
// https://github.com/gookit/validate/v2/issues/277 Identifying the First Failed Field (StopOnError=true)
//
// answered(非 bug): StopOnError=true(默认)时 Validate 失败后, v.Errors 仅含**第一个**出错
// 字段; 可直接取字段名 + 校验器(tag) + 消息。这就是提问的答案。
func TestIssue_277_v2(t *testing.T) {
type SomeStruct struct {
Name string `validate:"required"`
Email string `validate:"required|email"`
}
// Name 合法(required 通过), Email 非空但非邮箱 -> email 校验失败
v := validate.Struct(&SomeStruct{Name: "John", Email: "not an email"})
assert.True(t, v.StopOnError) // 默认开启
assert.False(t, v.Validate())
// 仅第一个出错字段 Email 进入 Errors, Name 不在
assert.True(t, v.Errors.HasField("Email"))
assert.False(t, v.Errors.HasField("Name"))
// 取该字段失败的校验器(tag)与消息
fe := v.Errors.Field("Email") // map[validator]message
_, byEmail := fe["email"]
assert.True(t, byEmail)
assert.StrContains(t, v.Errors.FieldOne("Email"), "Email")
}
// https://github.com/gookit/validate/v2/issues/266 Can `in` be used with slice in tag validation?
//
// tag 里的 in(enum) 对切片是**逐元素**校验: 每个元素都在枚举中才通过(自 #266 修复起)。
// 只需要校验个别元素时, 仍可用程序化的 "S.*" 规则(StringRule)。
func TestIssue_266_v2(t *testing.T) {
t.Run("tag 'in' on []string checks every element", func(t *testing.T) {
type A struct {
S []string `validate:"required|in:a,b"`
}
// 两个元素都合法
v := validate.Struct(&A{S: []string{"a", "b"}})
v.StopOnError = false
assert.True(t, v.Validate())
// 有一个元素不合法 -> 整体失败
v = validate.Struct(&A{S: []string{"a", "x"}})
v.StopOnError = false
assert.False(t, v.Validate())
assert.ErrSubMsg(t, v.Errors, "must be in the enum")
})
t.Run("workaround: programmatic 'S.*' validates each element", func(t *testing.T) {
ok := validate.Map(map[string]any{"S": []string{"a", "b"}})
ok.StringRule("S.*", "in:a,b")
assert.True(t, ok.Validate())
bad := validate.Map(map[string]any{"S": []string{"a", "x"}})
bad.StringRule("S.*", "in:a,b")
assert.False(t, bad.Validate())
assert.ErrSubMsg(t, bad.Errors, "must be in the enum")
})
}
// https://github.com/gookit/validate/v2/issues/265 How CheckZero config flag works.
//
// finding -> Deprecated: GlobalOption.CheckZero 已声明但无任何消费方(no-op), 切换它不会
// 改变任何校验行为。已在 v2.0 标记为 Deprecated(见 validate.go), 后续移除。零值是否参与
// 校验请用 Rule.SetSkipEmpty(false) / SkipOnEmpty=false / required, 与 CheckZero 无关。
//
// 本用例**有意**设置该 deprecated 字段, 以证明其为 no-op(切换前后结果一致)。
func TestIssue_265_v2(t *testing.T) {
type Foo struct {
Age int `validate:"min:18"`
}
run := func() bool { return validate.Struct(&Foo{Age: 0}).Validate() }
got1 := run() // CheckZero=false(默认)
validate.Config(func(opt *validate.GlobalOption) { opt.CheckZero = true }) //nolint:staticcheck // intentionally exercise deprecated no-op
defer validate.ResetOption()
got2 := run() // CheckZero=true
// 切换 CheckZero 前后结果完全一致 -> 该 flag 当前无效果
assert.Eq(t, got1, got2)
}
// https://github.com/gookit/validate/v2/issues/162 Combine required_if with a validation rule
//
// answered(可实现): required_if 可与后续规则链式组合。"required_if:Type,B|uuid4" 表示
// 当 Type==B 时 ID 必填且须为 uuid4; Type!=B 时 ID 可空(按 SkipOnEmpty 跳过), 但若提供
// 非空值仍按 uuid4 校验(uuid4 不被条件门控, 仅"是否必填"被门控)。
func TestIssue_162_v2(t *testing.T) {
type Form struct {
Type string `validate:"in:B,C"`
ID string `validate:"required_if:Type,B|uuid4"`
}
good := "94e48bd3-e990-405e-bd10-304e767cd3fd"
t.Run("Type=B + valid uuid -> pass", func(t *testing.T) {
assert.True(t, validate.Struct(&Form{Type: "B", ID: good}).Validate())
})
t.Run("Type=B + empty -> fail (required)", func(t *testing.T) {
v := validate.Struct(&Form{Type: "B", ID: ""})
v.StopOnError = false
assert.False(t, v.Validate())
assert.ErrSubMsg(t, v.Errors, "ID is required")
})
t.Run("Type=C + empty -> pass (not required)", func(t *testing.T) {
assert.True(t, validate.Struct(&Form{Type: "C", ID: ""}).Validate())
})
t.Run("Type=B + invalid uuid -> fail (uuid4)", func(t *testing.T) {
v := validate.Struct(&Form{Type: "B", ID: "notauuid"})
v.StopOnError = false
assert.False(t, v.Validate())
assert.ErrSubMsg(t, v.Errors, "UUID4")
})
t.Run("Type=C + non-empty invalid uuid -> still fail uuid4 (rule not gated by condition)", func(t *testing.T) {
v := validate.Struct(&Form{Type: "C", ID: "notauuid"})
v.StopOnError = false
assert.False(t, v.Validate())
assert.ErrSubMsg(t, v.Errors, "UUID4")
})
}
// --- #203 support types ---
type issue203Foo struct {
Name int `validate:"required|int|between:1,999" label:"name"`
}
type issue203Bar struct {
Code string `validate:"customCheck" filter:"upper"`
}
// CustomCheck 仅接受 "OK"(经 upper filter 后)。
func (issue203Bar) CustomCheck(val string) bool { return val == "OK" }
// https://github.com/gookit/validate/v2/issues/203 An explanation of how `required` works.
//
// 核查结论(评论楼最后一条担忧"去掉 required 后 filter/validator 不跑、非法数据被放过"):
// v2.0 下**无问题**, 属对 SkipOnEmpty 语义的误解。
// - 非 required 字段对**非空**值, filter/validator **照常运行**, 非法值被拒;
// - 仅当值为**空(零值/"")**时, 非 required 规则按 SkipOnEmpty 跳过(视为"可选未填");
// - 若空也要拒, 加 required(或 Rule.SetSkipEmpty(false) / 关 SkipOnEmpty)。
//
// 另: 楼主原始 bug(required|int|between 对 Name=4 误报 "required to not be empty")在 v2.0
// 已修复 —— Name=4 正常通过。
func TestIssue_203_v2(t *testing.T) {
t.Run("required on non-zero int passes (original report's bug is fixed)", func(t *testing.T) {
// v1.4.6 曾对 Name=4 误报 required; v2.0 正常通过
assert.True(t, validate.Struct(&issue203Foo{Name: 4}).Validate())
})
t.Run("non-required + NON-empty invalid value IS validated and rejected", func(t *testing.T) {
// Code="bad" -> upper -> "BAD" -> customCheck 失败 -> 被拒(validator 确实跑了)
v := validate.Struct(&issue203Bar{Code: "bad"})
assert.False(t, v.Validate())
assert.StrContains(t, v.Errors.String(), "Code")
})
t.Run("non-required + custom filter runs for non-empty value", func(t *testing.T) {
// Code="ok" -> upper -> "OK" -> customCheck 通过; filter 确实跑了
v := validate.Struct(&issue203Bar{Code: "ok"})
r := v.ValidateR()
assert.True(t, r.IsOK())
assert.Eq(t, "OK", r.SafeData()["Code"])
})
t.Run("non-required + EMPTY value is skipped (SkipOnEmpty: optional, by design)", func(t *testing.T) {
// Code="" -> 空值被跳过, customCheck 不运行 -> 通过(这正是楼主当年遇到的"放过")
assert.True(t, validate.Struct(&issue203Bar{Code: ""}).Validate())
})
t.Run("add 'required' to reject empty (the resolution)", func(t *testing.T) {
// 若空也要拒: 加 required, 空值不再被跳过
v := validate.Map(map[string]any{"code": ""})
v.StringRule("code", "required")
assert.False(t, v.Validate())
})
}
// --- #244 support: stand-in HTML/JS sanitizer ---
//
// issue244HTMLTagRe 匹配任意 HTML 标签(含 <script>/<img ...> 等)。
// 这是一个**最小可用的 stand-in 清洗实现**, 仅用于在测试中演示"自定义 filter
// 在绑定前清洗字符串"的用法。**真实场景请勿用正则去 HTML**(无法防御所有 XSS),
// 应换成 bluemonday: `policy := bluemonday.UGCPolicy(); ... policy.Sanitize(s)`。
var issue244HTMLTagRe = regexp.MustCompile(`<[^>]*>`)
// sanitizeHTMLStandIn 去掉字符串里的 HTML 标签(stand-in)。
// 真实使用时整体替换为 bluemonday 的 `policy.Sanitize(s)` 即可, filter 接线方式不变。
func sanitizeHTMLStandIn(s string) string {
return issue244HTMLTagRe.ReplaceAllString(s, "")
}
// https://github.com/gookit/validate/v2/issues/244
// [Question] How to sanitize all data for v.sanitize() with bluemonday
//
// 核查结论: **answered(非 bug, 用法问题)**。提问者想在数据绑定到 struct 字段**之前**,
// 用 bluemonday 把字符串里的 HTML/JS 清掉, 再走 BindSafeData。本库自带的自定义 filter
// 机制(`AddFilter` + `filter:"xxx"` tag / 程序化 `FilterRule`)正好满足: filter 在
// 校验/绑定**之前**运行, 把 bluemonday 的 `policy.Sanitize` 包成一个 filter 挂到字符串
// 字段上即可 —— 无需任何新依赖, 也无需改库。
//
// 本测试用一个**最小 stand-in 清洗函数** sanitizeHTMLStandIn(简单正则去标签)演示
// 同一套 filter 接线; **真实使用时只需把它换成 `bluemondayPolicy.Sanitize`**, 其余
// 不变。覆盖两种接线方式: ① struct tag `filter:"sanitizeHTML"`; ② 程序化 FilterRule。
func TestIssue_244_v2(t *testing.T) {
const dirty = `Hello <script>alert('xss')</script><b>World</b>`
const clean = `Hello alert('xss')World`
t.Run("struct tag filter sanitizes before BindSafeData", func(t *testing.T) {
// 字符串字段挂 filter:"sanitizeHTML"; filter 在校验/绑定前运行
type UserForm struct {
Bio string `json:"bio" filter:"sanitizeHTML" validate:"string"`
}
f := &UserForm{Bio: dirty}
v := validate.New(f)
// 真实场景: v.AddFilter("sanitizeHTML", bluemondayPolicy.Sanitize)
v.AddFilter("sanitizeHTML", sanitizeHTMLStandIn)
r := v.ValidateR()
assert.True(t, r.IsOK())
// 清洗后的值已进入 safeData(键为字段名 Bio)
assert.Eq(t, clean, r.SafeData()["Bio"])
out := &UserForm{}
assert.NoErr(t, r.BindSafeData(out))
assert.Eq(t, clean, out.Bio)
assert.StrNotContains(t, out.Bio, "<script>")
assert.StrNotContains(t, out.Bio, "<b>")
})
t.Run("programmatic FilterRule sanitizes before BindSafeData", func(t *testing.T) {
type UserForm struct {
Bio string `json:"bio"`
}
v := validate.Map(map[string]any{"bio": dirty})
// 真实场景: v.AddFilter("sanitizeHTML", bluemondayPolicy.Sanitize)
v.AddFilter("sanitizeHTML", sanitizeHTMLStandIn)
// 先清洗, 再加一条校验规则 —— 只有"参与校验"的字段才会进 safeData 供绑定
v.FilterRule("bio", "sanitizeHTML")
v.StringRule("bio", "string")
r := v.ValidateR()
assert.True(t, r.IsOK())
assert.Eq(t, clean, r.SafeData()["bio"])
out := &UserForm{}
assert.NoErr(t, r.BindSafeData(out))
assert.Eq(t, clean, out.Bio)
assert.StrNotContains(t, out.Bio, "script")
})
}