Skip to content

feat: new option for multiple error returned #1450

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -135,7 +135,9 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
kind: kind,
},
)
return
if !v.v.multipleErrorsReturned {
return
}
}

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

Expand Down Expand Up @@ -453,7 +457,9 @@ OUTER:
)
}

return
if !v.v.multipleErrorsReturned {
return
}
}

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

return
if !v.v.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 @@ -96,6 +96,7 @@ type Validate struct {
hasTagNameFunc bool
requiredStructEnabled bool
privateFieldValidation bool
multipleErrorsReturned bool
}

// New returns a new instance of 'validate' with sane defaults.
Expand Down
97 changes: 97 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14975,3 +14975,100 @@ func TestRequiredIfWithArrays(t *testing.T) {
Equal(t, err, nil) // No error - Text has value
})
}

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"`
}{
F1: 100,
},
errorNum: 7,
},
{
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)
}
}