Skip to content

Conversation

@chargraves85
Copy link

@chargraves85 chargraves85 commented Aug 16, 2024

Fixes Or Enhances

The verbiage in the documentation for both excluded_unless and required_unless is very similar. One would expect the opposite behavior for each when using multiple values of the same field that is being validated against.

For example:

This fails in all 3 cases in sample code

package main

import (
	"fmt"
	"github.com/go-playground/validator/v10"
)


type TestStruct struct {
	Foo int
	Bar string `validate:"excluded_unless=Foo 1 Foo 2"`
}

func main() {

	validator := validator.New()

	testA := TestStruct{
		Foo: 1,
		Bar: "test",
	}
	testB := TestStruct{
		Foo: 2,
		Bar: "test",
	}
	testC := TestStruct{
		Foo: 3,
		Bar: "test",
	}

	err := validator.Struct(testA)
	if err != nil {
		fmt.Println(err.Error())
	}

	err = validator.Struct(testB)
	if err != nil {
		fmt.Println(err.Error())
	}

	err = validator.Struct(testC)
	if err != nil {
		fmt.Println(err.Error())
	}

}

However, the equivilant opposite of this only fails in TestC

type TestStruct struct {
	Foo int
	Bar string `validate:"required_unless=Foo 1 Foo 2"`
}

func main() {

	validator := validator.New()

	testA := TestStruct{
		Foo: 1,
	}
	testB := TestStruct{
		Foo: 2,
	}
	
	err := validator.Struct(testA)
	if err != nil {
		fmt.Println(err.Error())
	}

	err = validator.Struct(testB)
	if err != nil {
		fmt.Println(err.Error())
	}

}

resolves #1306

@chargraves85 chargraves85 requested a review from a team as a code owner August 16, 2024 18:02
@coveralls
Copy link

coveralls commented Aug 16, 2024

Coverage Status

coverage: 73.749%. remained the same
when pulling 2cb9f1d on chargraves85:issue-1306-excluded_unless-expectation-parity-with-required_unless
into 0c1335d on go-playground:master.

@chargraves85 chargraves85 force-pushed the issue-1306-excluded_unless-expectation-parity-with-required_unless branch from 6c54727 to ca3636f Compare August 16, 2024 18:08
@khoirulamri
Copy link

any update for this issue?

@chargraves85
Copy link
Author

any update for this issue?

Looks like we have a conflict. I can update the PR but had no momentum on it when I first posted.

@nodivbyzero
Copy link
Contributor

Please resolve the conflicts, and I’ll proceed with the approval

@chargraves85 chargraves85 force-pushed the issue-1306-excluded_unless-expectation-parity-with-required_unless branch 2 times, most recently from 65df2bb to 0066799 Compare December 9, 2025 13:54
@chargraves85
Copy link
Author

@nodivbyzero thank you. We should be ready.

{"ABCDDE1-", "bic", false}, // hyphen in location
{"ABCDDE1_", "bic", false}, // underscore in location
{"ABCDDE١٢", "bic", false}, // non-ASCII digits in location
{"DEUTDEFF", "bic", true}, // 8-char classic (Germany)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this change related to the excluded_unless?

Copy link
Author

Choose a reason for hiding this comment

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

sorry, moving back to command line, IDEA driving me nuts with go fmt

Copy link
Author

Choose a reason for hiding this comment

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

formatting and history cleaned up

@chargraves85 chargraves85 force-pushed the issue-1306-excluded_unless-expectation-parity-with-required_unless branch 6 times, most recently from e9fd119 to 1798595 Compare December 9, 2025 19:02
@chargraves85 chargraves85 force-pushed the issue-1306-excluded_unless-expectation-parity-with-required_unless branch from 1798595 to 2cb9f1d Compare December 9, 2025 19:07
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.

[Bug] required_unless and expected_unless syntax does not work the same way

4 participants