-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Labels
Description
What happened?
When using required_if with the same field and multiple comparison values, the validator unexpectedly passes, it should expect error
Example:
type Request struct {
Type string `validate:"required"`
Value string `validate:"required_if=Type A Type B"`
}
Type
value cannot be A and B at the same time, so the validator will pass in anycase
Minimal Repro (failing test)
package demo
import (
"testing"
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/require"
)
type Request struct {
Type string `validate:"required"`
Value string `validate:"required_if=Type A Type B"`
}
func TestValidateRequiredIf(t *testing.T) {
v := validator.New()
// Type A, Value empty -> should fire panic error
r := Request{Type: "A", Value: ""}
err := v.Struct(r)
require.Error(t, err, "expected error: Value must be required when Type is A or B")
// Type B, Value empty -> should fire panic error
r = Request{Type: "B", Value: ""}
err = v.Struct(r)
require.Error(t, err, "expected error: Value must be required when Type is A or B")
}
Expected behavior
This tag validate:"required_if=Type A Type B"
should output panic error, and only accept unique field name
Actual behavior
Validation passes (no error) in the above cases
If this makes sense, I’d be happy to help contribute an implementation
Version
v10.12.0
If this makes sense, I’d be happy to help contribute fix