Skip to content

Commit

Permalink
Result is wrong while there are multiple group of OR operators #910 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
XIELongDragon committed May 1, 2022
1 parent f2d3ff7 commit af72f63
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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)
}
}
}
}

0 comments on commit af72f63

Please sign in to comment.