Skip to content

Conversation

jmfrees
Copy link
Contributor

@jmfrees jmfrees commented Oct 3, 2024

Fixes Or Enhances

Resolves #617

Fixes required_without so that it aligns with the stated contract and usage examples.

The docs 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:

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:

type Test struct {
    Field1 string `validator:required_without=Field2 Field3"
    ...
}

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@jmfrees jmfrees requested a review from a team as a code owner October 3, 2024 23:50
@jmfrees jmfrees changed the title Align required_without with the contract stated in the documentation Align required_without with usage shown in the docs Oct 3, 2024
@coveralls
Copy link

Coverage Status

coverage: 74.295% (+0.003%) from 74.292%
when pulling 91f421e on jmfrees:jmfrees/fix-required_without
into f1939ee on go-playground:master.

@jmfrees jmfrees closed this by deleting the head repository Feb 19, 2025
@longing-esv
Copy link

Why is the request closed? As far as I understand, the error has not been fixed.

@jmfrees
Copy link
Contributor Author

jmfrees commented Apr 24, 2025

Why is the request closed? As far as I understand, the error has not been fixed.

I must've deleted my fork. Let me bring it back. Maybe we can actually get this merged 😅

@jmfrees
Copy link
Contributor Author

jmfrees commented Apr 24, 2025

Thanks for pointing this out @longing-esv. I brought it back in another PR since I was not allowed to re-open this one.

nodivbyzero pushed a commit that referenced this pull request Apr 28, 2025
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

required_without doesn't seem to work with multiple fields
3 participants