Skip to content

Commit 386c339

Browse files
Change to allow multiple errors be returned from single field when option "WithMultipleErrorsReturned" is set.
1 parent a947377 commit 386c339

File tree

4 files changed

+114
-4
lines changed

4 files changed

+114
-4
lines changed

options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ func WithPrivateFieldValidation() Option {
2424
v.privateFieldValidation = true
2525
}
2626
}
27+
28+
func WithMultipleErrorsReturned() Option {
29+
return func(v *Validate) {
30+
v.multipleErrorsReturned = true
31+
}
32+
}

validator.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
138138
kind: kind,
139139
},
140140
)
141-
return
141+
if !v.Validator().multipleErrorsReturned {
142+
return
143+
}
142144
}
143145

144146
v.str1 = string(append(ns, cf.altName...))
@@ -163,7 +165,9 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
163165
typ: current.Type(),
164166
},
165167
)
166-
return
168+
if !v.Validator().multipleErrorsReturned {
169+
return
170+
}
167171
}
168172
}
169173

@@ -439,7 +443,9 @@ OUTER:
439443
)
440444
}
441445

442-
return
446+
if !v.Validator().multipleErrorsReturned {
447+
return
448+
}
443449
}
444450

445451
ct = ct.next
@@ -478,7 +484,9 @@ OUTER:
478484
},
479485
)
480486

481-
return
487+
if !v.Validator().multipleErrorsReturned {
488+
return
489+
}
482490
}
483491
ct = ct.next
484492
}

validator_instance.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type Validate struct {
9595
hasTagNameFunc bool
9696
requiredStructEnabled bool
9797
privateFieldValidation bool
98+
multipleErrorsReturned bool
9899
}
99100

100101
// New returns a new instance of 'validate' with sane defaults.

validator_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14001,3 +14001,98 @@ func TestPrivateFieldsStruct(t *testing.T) {
1400114001
Equal(t, len(errs), tc.errorNum)
1400214002
}
1400314003
}
14004+
14005+
func TestMultipleErrorsOption(t *testing.T) {
14006+
type tc struct {
14007+
stct interface{}
14008+
errorNum int
14009+
}
14010+
14011+
tcs := []tc{
14012+
{
14013+
stct: &struct {
14014+
F1 int8 `validate:"eq=10,gte=10"`
14015+
F2 int16 `validate:"eq=10,gte=10"`
14016+
F3 int32 `validate:"eq=10,gte=10"`
14017+
F4 int64 `validate:"eq=10,gte=10"`
14018+
}{},
14019+
errorNum: 8,
14020+
},
14021+
{
14022+
stct: &struct {
14023+
F1 uint8 `validate:"eq=10,gte=10"`
14024+
F2 uint16 `validate:"eq=10,gte=10"`
14025+
F3 uint32 `validate:"eq=10,gte=10"`
14026+
F4 uint64 `validate:"eq=10,gte=10"`
14027+
}{},
14028+
errorNum: 8,
14029+
},
14030+
{
14031+
stct: &struct {
14032+
F1 string `validate:"eq=10,gte=10"`
14033+
F2 string `validate:"eq=10,gte=10"`
14034+
}{},
14035+
errorNum: 4,
14036+
},
14037+
{
14038+
stct: &struct {
14039+
F1 float32 `validate:"eq=10,gte=10"`
14040+
F2 float64 `validate:"eq=10,gte=10"`
14041+
}{},
14042+
errorNum: 4,
14043+
},
14044+
{
14045+
stct: struct {
14046+
F1 int8 `validate:"eq=10,gte=10"`
14047+
F2 int16 `validate:"eq=10,gte=10"`
14048+
F3 int32 `validate:"eq=10,gte=10"`
14049+
F4 int64 `validate:"eq=10,gte=10"`
14050+
}{},
14051+
errorNum: 8,
14052+
},
14053+
{
14054+
stct: struct {
14055+
F1 uint8 `validate:"eq=10,gte=10"`
14056+
F2 uint16 `validate:"eq=10,gte=10"`
14057+
F3 uint32 `validate:"eq=10,gte=10"`
14058+
F4 uint64 `validate:"eq=10,gte=10"`
14059+
}{},
14060+
errorNum: 8,
14061+
},
14062+
{
14063+
stct: struct {
14064+
F1 float32 `validate:"eq=10,gte=10"`
14065+
F2 float64 `validate:"eq=10,gte=10"`
14066+
}{},
14067+
errorNum: 4,
14068+
},
14069+
{
14070+
stct: struct {
14071+
F1 int `validate:"eq=10,gte=10"`
14072+
F2 struct {
14073+
F3 int `validate:"eq=10,gte=10"`
14074+
}
14075+
}{},
14076+
errorNum: 4,
14077+
},
14078+
{
14079+
stct: &struct {
14080+
F1 int `validate:"eq=10,gte=10"`
14081+
F2 struct {
14082+
F3 int `validate:"eq=10,gte=10"`
14083+
}
14084+
}{},
14085+
errorNum: 4,
14086+
},
14087+
}
14088+
14089+
validate := New(WithMultipleErrorsReturned())
14090+
14091+
for _, tc := range tcs {
14092+
err := validate.Struct(tc.stct)
14093+
NotEqual(t, err, nil)
14094+
14095+
errs := err.(ValidationErrors)
14096+
Equal(t, len(errs), tc.errorNum)
14097+
}
14098+
}

0 commit comments

Comments
 (0)