Skip to content

Align required_without with the contract stated in the documentation #1422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jmfrees
Copy link
Contributor

@jmfrees jmfrees commented Apr 24, 2025

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.

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

@jmfrees jmfrees requested a review from a team as a code owner April 24, 2025 19:05
@coveralls
Copy link

Coverage Status

coverage: 73.655% (+0.002%) from 73.653%
when pulling 6605bf7 on jmfrees:jmfrees/fix-required_without
into c3fc72e on go-playground:master.

Copy link
Contributor

@nodivbyzero nodivbyzero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!
Everything looks good to me.

@jmfrees
Copy link
Contributor Author

jmfrees commented Apr 25, 2025

Thank you for your contribution! Everything looks good to me.

Are there any other steps I need to take to help this fix be applied? If so, just let me know :) @nodivbyzero

@nodivbyzero nodivbyzero merged commit 5b31512 into go-playground:master Apr 28, 2025
12 checks passed
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