Skip to content
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

excluded_unless bug fix #1307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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: 74.291%. remained the same
when pulling ca3636f on chargraves85:issue-1306-excluded_unless-expectation-parity-with-required_unless
into a947377 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
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
2 participants