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

Result is wrong while there are multiple group of OR operators #910 #911

Merged
merged 2 commits into from May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions validator.go
Expand Up @@ -355,6 +355,10 @@ OUTER:
v.ct = ct

if ct.fn(ctx, v) {
if ct.isBlockEnd {
ct = ct.next
continue OUTER
}

// drain rest of the 'or' values, then continue or leave
for {
Expand All @@ -368,6 +372,11 @@ OUTER:
if ct.typeof != typeOr {
continue OUTER
}

if ct.isBlockEnd {
ct = ct.next
continue OUTER
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions validator_test.go
Expand Up @@ -11790,3 +11790,27 @@ func TestCreditCardFormatValidation(t *testing.T) {
}
}
}

func TestMultiOrOperatorGroup(t *testing.T) {
tests := []struct {
Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
expected bool
}{
{1, true}, {2, false}, {5, true}, {6, true}, {8, false},
}

validate := New()

for i, test := range tests {
errs := validate.Struct(test)
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
}
}
}
}