Skip to content

Commit 5b31512

Browse files
authored
Align required_without with the contract stated in the documentation (#1422)
## Fixes Or Enhances Resolves #617 Fixes `required_without` so that it aligns with the stated contract and usage examples. The [docs](https://pkg.go.dev/github.com/go-playground/validator/v10#hdr-Required_Without) valid usage to be: ``` // require the field if the Field1 is not present: Usage: required_without=Field1 // require the field if the Field1 or Field2 is not present: Usage: required_without=Field1 Field2 ``` However, as show in this issue: #617, you can only use `required_without` on multiple fields like: ```golang type Test struct { Field1 string `validator:required_without=Field2,required_without=Field3" ... } ``` This PR aligns the behavior of the `required_without` validator such that you can also use it as shown in the example: ```golang type Test struct { Field1 string `validator:required_without=Field2 Field3" ... } ``` **Make sure that you've checked the boxes below before you submit PR:** - [x] Tests exist or have been written that cover this particular change. Note: Sorry for the duplication of #1324. I was cleaning up my profile and deleted my fork before it could be merged. @go-playground/validator-maintainers
1 parent c3fc72e commit 5b31512

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

baked_in.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -2004,8 +2004,11 @@ func excludedWithout(fl FieldLevel) bool {
20042004
// requiredWithout is the validation function
20052005
// The field under validation must be present and not empty only when any of the other specified fields are not present.
20062006
func requiredWithout(fl FieldLevel) bool {
2007-
if requireCheckFieldKind(fl, strings.TrimSpace(fl.Param()), true) {
2008-
return hasValue(fl)
2007+
params := parseOneOfParam2(fl.Param())
2008+
for _, param := range params {
2009+
if requireCheckFieldKind(fl, param, true) {
2010+
return hasValue(fl)
2011+
}
20092012
}
20102013
return true
20112014
}

validator_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -11865,6 +11865,32 @@ func TestRequiredWithout(t *testing.T) {
1186511865

1186611866
errs = validate.Struct(&test3)
1186711867
Equal(t, errs, nil)
11868+
11869+
test4 := struct {
11870+
Field1 string `validate:"required_without=Field2 Field3,omitempty,min=1" json:"field_1"`
11871+
Field2 string `json:"field_2"`
11872+
Field3 string `json:"field_3"`
11873+
}{
11874+
Field1: "test",
11875+
}
11876+
11877+
errs = validate.Struct(&test4)
11878+
Equal(t, errs, nil)
11879+
11880+
test5 := struct {
11881+
Field1 string `validate:"required_without=Field2 Field3,omitempty,min=1" json:"field_1"`
11882+
Field2 string `json:"field_2"`
11883+
Field3 string `json:"field_3"`
11884+
}{
11885+
Field3: "test",
11886+
}
11887+
11888+
errs = validate.Struct(&test5)
11889+
NotEqual(t, errs, nil)
11890+
11891+
ve = errs.(ValidationErrors)
11892+
Equal(t, len(ve), 1)
11893+
AssertError(t, errs, "Field1", "Field1", "Field1", "Field1", "required_without")
1186811894
}
1186911895

1187011896
func TestRequiredWithoutAll(t *testing.T) {

0 commit comments

Comments
 (0)