@@ -11099,6 +11099,49 @@ func TestRequiredIf(t *testing.T) {
1109911099 _ = validate .Struct (test3 )
1110011100}
1110111101
11102+ func TestRequiredIfDuplicateParams (t * testing.T ) {
11103+ validate := New ()
11104+
11105+ PanicMatches (t , func () {
11106+ type TestStruct struct {
11107+ Field1 string `validate:"required_if=Field2 value1 Field2 value2"`
11108+ Field2 string
11109+ }
11110+ test := TestStruct {
11111+ Field1 : "" ,
11112+ Field2 : "value1" ,
11113+ }
11114+ _ = validate .Struct (test )
11115+ }, "Duplicate param Field2 for required_if Field1" )
11116+
11117+ PanicMatches (t , func () {
11118+ type TestStruct struct {
11119+ Field1 string `validate:"required_if=Field2 val1 Field3 val2 Field2 val3"`
11120+ Field2 string
11121+ Field3 string
11122+ }
11123+ test := TestStruct {
11124+ Field1 : "" ,
11125+ Field2 : "val1" ,
11126+ Field3 : "val2" ,
11127+ }
11128+ _ = validate .Struct (test )
11129+ }, "Duplicate param Field2 for required_if Field1" )
11130+
11131+ type TestStruct struct {
11132+ Field1 string `validate:"required_if=Field2 val1 Field3 val2"`
11133+ Field2 string
11134+ Field3 string
11135+ }
11136+ test := TestStruct {
11137+ Field1 : "" ,
11138+ Field2 : "val1" ,
11139+ Field3 : "val2" ,
11140+ }
11141+ errs := validate .Struct (test )
11142+ NotEqual (t , errs , nil )
11143+ }
11144+
1110211145func TestRequiredUnless (t * testing.T ) {
1110311146 type Inner struct {
1110411147 Field * string
@@ -11169,19 +11212,40 @@ func TestRequiredUnless(t *testing.T) {
1116911212 AssertError (t , errs , "Field10" , "Field10" , "Field10" , "Field10" , "required_unless" )
1117011213 AssertError (t , errs , "Field11" , "Field11" , "Field11" , "Field11" , "required_unless" )
1117111214
11172- defer func () {
11173- if r := recover (); r == nil {
11174- 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 },
1117511221 }
11176- }()
11222+ _ = validate .Struct (test3 )
11223+ }, "Bad param number for required_unless Field1" )
1117711224
11178- test3 := struct {
11179- Inner * Inner
11180- Field1 string `validate:"required_unless=Inner.Field" json:"field_1"`
11181- }{
11182- Inner : & Inner {Field : & fieldVal },
11225+ type DuplicateStruct struct {
11226+ Field1 string `validate:"required_unless=Field2 value1 Field2 value2"`
11227+ Field2 string
1118311228 }
11184- _ = 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 )
1118511249}
1118611250
1118711251func TestSkipUnless (t * testing.T ) {
0 commit comments