Skip to content

Commit e3c2d81

Browse files
committed
add doc & fix & add testcase check dup param required_unless
1 parent 52ecdf6 commit e3c2d81

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ Examples:
275275
// require the field if the Field1 and Field2 is equal to the value respectively:
276276
Usage: required_if=Field1 foo Field2 bar
277277
278+
Note: Duplicate field names in the parameters will cause a panic error issue
279+
278280
# Required Unless
279281
280282
The field under validation must be present and not empty unless all

validator_test.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11212,19 +11212,40 @@ func TestRequiredUnless(t *testing.T) {
1121211212
AssertError(t, errs, "Field10", "Field10", "Field10", "Field10", "required_unless")
1121311213
AssertError(t, errs, "Field11", "Field11", "Field11", "Field11", "required_unless")
1121411214

11215-
defer func() {
11216-
if r := recover(); r == nil {
11217-
t.Errorf("test3 should have panicked!")
11215+
PanicMatches(t, func() {
11216+
test3 := struct {
11217+
Inner *Inner
11218+
Field1 string `validate:"required_unless=Inner.Field" json:"field_1"`
11219+
}{
11220+
Inner: &Inner{Field: &fieldVal},
1121811221
}
11219-
}()
11222+
_ = validate.Struct(test3)
11223+
}, "Bad param number for required_unless Field1")
1122011224

11221-
test3 := struct {
11222-
Inner *Inner
11223-
Field1 string `validate:"required_unless=Inner.Field" json:"field_1"`
11224-
}{
11225-
Inner: &Inner{Field: &fieldVal},
11225+
type DuplicateStruct struct {
11226+
Field1 string `validate:"required_unless=Field2 value1 Field2 value2"`
11227+
Field2 string
1122611228
}
11227-
_ = validate.Struct(test3)
11229+
test4 := DuplicateStruct{
11230+
Field1: "",
11231+
Field2: "value1",
11232+
}
11233+
errs = validate.Struct(test4)
11234+
Equal(t, errs, nil)
11235+
11236+
test5 := DuplicateStruct{
11237+
Field1: "",
11238+
Field2: "value2",
11239+
}
11240+
errs = validate.Struct(test5)
11241+
Equal(t, errs, nil)
11242+
11243+
test6 := DuplicateStruct{
11244+
Field1: "",
11245+
Field2: "value3",
11246+
}
11247+
errs = validate.Struct(test6)
11248+
NotEqual(t, errs, nil)
1122811249
}
1122911250

1123011251
func TestSkipUnless(t *testing.T) {

0 commit comments

Comments
 (0)