Skip to content

feat: add option early return in case of error #1374

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 2 commits 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
}
}

// WithEarlyExit configures the validator to immediately stop validation as soon as the first error is encountered
//
// This feature could be an opt-in behavior, allowing to opt into "early exit" validation, without breaking current workflows
// Early exit on the first failure would save time by avoiding unnecessary checks on the remaining fields
func WithEarlyExit() Option {
return func(v *Validate) {
v.earlyExit = true
}
}
4 changes: 3 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func (v *validate) validateStruct(ctx context.Context, parent reflect.Value, cur
var f *cField

for i := 0; i < len(cs.fields); i++ {

if v.v.earlyExit && len(v.errs) > 0 {
return
}
f = cs.fields[i]

if v.isPartial {
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
earlyExit bool
}

// New returns a new instance of 'validate' with sane defaults.
Expand Down
71 changes: 70 additions & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12080,7 +12080,7 @@ func TestExcludedIf(t *testing.T) {

test11 := struct {
Field1 bool
Field2 *string `validate:"excluded_if=Field1 false"`
Field2 *string `validate:"excluded_if=Field1 false"`
}{
Field1: false,
Field2: nil,
Expand Down Expand Up @@ -14123,3 +14123,72 @@ func TestPrivateFieldsStruct(t *testing.T) {
Equal(t, len(errs), tc.errorNum)
}
}

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

tcs := []tc{
{
stct: &struct {
f1 int8 `validate:"gt=0"`
f2 int16 `validate:"gt=0"`
f3 int32 `validate:"gt=0"`
f4 int64 `validate:"gt=0"`
}{},
errorNum: 1,
},
{
stct: &struct {
f1 string `validate:"min=3"`
f2 string `validate:"min=3"`
}{},
errorNum: 1,
},
{
stct: &struct {
f1 struct {
f2 string `validate:"min=3"`
}
f3 int8 `validate:"gt=3"`
}{},
errorNum: 1,
},
{
stct: &struct {
f1 string `validate:"min=3"`
f2 struct {
f3 string `validate:"min=3"`
}
}{},
errorNum: 1,
},
{
stct: &struct {
f1 *int `validate:"gt=0"`
f2 struct {
f3 string `validate:"min=3"`
}
}{},
errorNum: 1,
},
{
stct: &struct {
f1 []string `validate:"required,dive"`
f2 []int8 `validate:"required,dive"`
}{},
errorNum: 1,
},
}

validate := New(WithPrivateFieldValidation(), WithEarlyExit())

for _, tc := range tcs {
err := validate.Struct(tc.stct)
NotEqual(t, err, nil)
errs := err.(ValidationErrors)
Equal(t, len(errs), tc.errorNum)
}
}
Loading