From 6d2313895315a0404d08502bbde8253fec4d006d Mon Sep 17 00:00:00 2001 From: xielong Date: Sat, 12 Mar 2022 12:05:49 +0800 Subject: [PATCH] Result is wrong while there are multiple group of OR operators #910 --- validator.go | 9 +++++++++ validator_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/validator.go b/validator.go index 2a4fad02..c2e03583 100644 --- a/validator.go +++ b/validator.go @@ -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 { @@ -368,6 +372,11 @@ OUTER: if ct.typeof != typeOr { continue OUTER } + + if ct.isBlockEnd { + ct = ct.next + continue OUTER + } } } diff --git a/validator_test.go b/validator_test.go index 3730fb9d..02866930 100644 --- a/validator_test.go +++ b/validator_test.go @@ -11460,7 +11460,7 @@ func TestSemverFormatValidation(t *testing.T) { } } } - + func TestRFC1035LabelFormatValidation(t *testing.T) { tests := []struct { value string `validate:"dns_rfc1035_label"` @@ -11611,3 +11611,28 @@ func TestPostCodeByIso3166Alpha2Field_InvalidKind(t *testing.T) { _ = New().Struct(test{"ABC", 123, false}) t.Errorf("Didn't panic as expected") } + +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) + } + } + } + +}