Skip to content

Commit

Permalink
Change to allow multiple errors be returned from single field when op…
Browse files Browse the repository at this point in the history
…tion "WithMultipleErrorsReturned" is set.
  • Loading branch information
d-wojciechowski committed Jul 3, 2024
1 parent a947377 commit 3ca5c65
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ func WithPrivateFieldValidation() Option {
v.privateFieldValidation = true
}
}

// WithMultipleErrorsReturned enables multi error return from a single struct field.
//
// By opting into this feature you are acknowledging that you are aware of the risks and accept any current or future
// consequences of using this feature.
func WithMultipleErrorsReturned() Option {
return func(v *Validate) {
v.multipleErrorsReturned = true
}
}
16 changes: 12 additions & 4 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
kind: kind,
},
)
return
if !v.Validator().multipleErrorsReturned {
return
}
}

v.str1 = string(append(ns, cf.altName...))
Expand All @@ -163,7 +165,9 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
typ: current.Type(),
},
)
return
if !v.Validator().multipleErrorsReturned {
return
}
}
}

Expand Down Expand Up @@ -439,7 +443,9 @@ OUTER:
)
}

return
if !v.Validator().multipleErrorsReturned {
return
}
}

ct = ct.next
Expand Down Expand Up @@ -478,7 +484,9 @@ OUTER:
},
)

return
if !v.Validator().multipleErrorsReturned {
return
}
}
ct = ct.next
}
Expand Down
1 change: 1 addition & 0 deletions validator_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type Validate struct {
hasTagNameFunc bool
requiredStructEnabled bool
privateFieldValidation bool
multipleErrorsReturned bool
}

// New returns a new instance of 'validate' with sane defaults.
Expand Down
95 changes: 95 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14001,3 +14001,98 @@ func TestPrivateFieldsStruct(t *testing.T) {
Equal(t, len(errs), tc.errorNum)
}
}

func TestMultipleErrorsOption(t *testing.T) {
type tc struct {
stct interface{}
errorNum int
}

tcs := []tc{
{
stct: &struct {
F1 int8 `validate:"eq=10,gte=10"`
F2 int16 `validate:"eq=10,gte=10"`
F3 int32 `validate:"eq=10,gte=10"`
F4 int64 `validate:"eq=10,gte=10"`
}{},
errorNum: 8,
},
{
stct: &struct {
F1 uint8 `validate:"eq=10,gte=10"`
F2 uint16 `validate:"eq=10,gte=10"`
F3 uint32 `validate:"eq=10,gte=10"`
F4 uint64 `validate:"eq=10,gte=10"`
}{},
errorNum: 8,
},
{
stct: &struct {
F1 string `validate:"eq=10,gte=10"`
F2 string `validate:"eq=10,gte=10"`
}{},
errorNum: 4,
},
{
stct: &struct {
F1 float32 `validate:"eq=10,gte=10"`
F2 float64 `validate:"eq=10,gte=10"`
}{},
errorNum: 4,
},
{
stct: struct {
F1 int8 `validate:"eq=10,gte=10"`
F2 int16 `validate:"eq=10,gte=10"`
F3 int32 `validate:"eq=10,gte=10"`
F4 int64 `validate:"eq=10,gte=10"`
}{},
errorNum: 8,
},
{
stct: struct {
F1 uint8 `validate:"eq=10,gte=10"`
F2 uint16 `validate:"eq=10,gte=10"`
F3 uint32 `validate:"eq=10,gte=10"`
F4 uint64 `validate:"eq=10,gte=10"`
}{},
errorNum: 8,
},
{
stct: struct {
F1 float32 `validate:"eq=10,gte=10"`
F2 float64 `validate:"eq=10,gte=10"`
}{},
errorNum: 4,
},
{
stct: struct {
F1 int `validate:"eq=10,gte=10"`
F2 struct {
F3 int `validate:"eq=10,gte=10"`
}
}{},
errorNum: 4,
},
{
stct: &struct {
F1 int `validate:"eq=10,gte=10"`
F2 struct {
F3 int `validate:"eq=10,gte=10"`
}
}{},
errorNum: 4,
},
}

validate := New(WithMultipleErrorsReturned())

for _, tc := range tcs {
err := validate.Struct(tc.stct)
NotEqual(t, err, nil)

errs := err.(ValidationErrors)
Equal(t, len(errs), tc.errorNum)
}
}

0 comments on commit 3ca5c65

Please sign in to comment.