Skip to content
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

FastFail feature #1042 #1131

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
5 changes: 5 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type validate struct {
fldIsPointer bool // StructLevel & FieldLevel
isPartial bool
hasExcludes bool
isFailFast bool // Returns on first error encountered
}

// parent and current will be the same the first run of validateStruct
Expand Down Expand Up @@ -75,6 +76,10 @@ func (v *validate) validateStruct(ctx context.Context, parent reflect.Value, cur
}

v.traverseField(ctx, current, current.Field(f.idx), ns, structNs, f, f.cTags)

if v.isFailFast && len(v.errs) > 0 {
return
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions validator_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Validate struct {
pool *sync.Pool
hasCustomFuncs bool
hasTagNameFunc bool
isFailFast bool
tagNameFunc TagNameFunc
structLevelFuncs map[reflect.Type]StructLevelFuncCtx
customFuncs map[reflect.Type]CustomTypeFunc
Expand Down Expand Up @@ -149,6 +150,11 @@ func New() *Validate {
return v
}

// FailFast sets the error response to return the first validation error encountered
func (v *Validate) FailFast() {
v.isFailFast = true
}

// SetTagName allows for changing of the default tag name of 'validate'
func (v *Validate) SetTagName(name string) {
v.tagName = name
Expand Down Expand Up @@ -384,6 +390,7 @@ func (v *Validate) StructCtx(ctx context.Context, s interface{}) (err error) {
vd := v.pool.Get().(*validate)
vd.top = top
vd.isPartial = false
vd.isFailFast = v.isFailFast
// vd.hasExcludes = false // only need to reset in StructPartial and StructExcept

vd.validateStruct(ctx, top, val, val.Type(), vd.ns[0:0], vd.actualNs[0:0], nil)
Expand Down Expand Up @@ -430,6 +437,7 @@ func (v *Validate) StructFilteredCtx(ctx context.Context, s interface{}, fn Filt
vd.top = top
vd.isPartial = true
vd.ffn = fn
vd.isFailFast = v.isFailFast
// vd.hasExcludes = false // only need to reset in StructPartial and StructExcept

vd.validateStruct(ctx, top, val, val.Type(), vd.ns[0:0], vd.actualNs[0:0], nil)
Expand Down Expand Up @@ -480,6 +488,7 @@ func (v *Validate) StructPartialCtx(ctx context.Context, s interface{}, fields .
vd.ffn = nil
vd.hasExcludes = false
vd.includeExclude = make(map[string]struct{})
vd.isFailFast = v.isFailFast

typ := val.Type()
name := typ.Name()
Expand Down Expand Up @@ -570,6 +579,7 @@ func (v *Validate) StructExceptCtx(ctx context.Context, s interface{}, fields ..
vd.ffn = nil
vd.hasExcludes = true
vd.includeExclude = make(map[string]struct{})
vd.isFailFast = v.isFailFast

typ := val.Type()
name := typ.Name()
Expand Down Expand Up @@ -641,6 +651,7 @@ func (v *Validate) VarCtx(ctx context.Context, field interface{}, tag string) (e
vd := v.pool.Get().(*validate)
vd.top = val
vd.isPartial = false
vd.isFailFast = v.isFailFast
vd.traverseField(ctx, val, val, vd.ns[0:0], vd.actualNs[0:0], defaultCField, ctag)

if len(vd.errs) > 0 {
Expand Down Expand Up @@ -693,6 +704,7 @@ func (v *Validate) VarWithValueCtx(ctx context.Context, field interface{}, other
vd := v.pool.Get().(*validate)
vd.top = otherVal
vd.isPartial = false
vd.isFailFast = v.isFailFast
vd.traverseField(ctx, otherVal, reflect.ValueOf(field), vd.ns[0:0], vd.actualNs[0:0], defaultCField, ctag)

if len(vd.errs) > 0 {
Expand Down
116 changes: 116 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13487,3 +13487,119 @@ func TestNestedStructValidation(t *testing.T) {
})
}
}

type Value struct {
Street string `validate:"required"`
City string `validate:"required"`
}

func TestFailFastSettingStruct(t *testing.T) {

tests := []struct {
v Value
failFast bool
expectedErrLen int
}{
{
v: Value{
Street: "",
City: "",
},
failFast: true,
expectedErrLen: 1,
},
{
v: Value{
Street: "",
City: "",
},
failFast: false,
expectedErrLen: 2,
},
}

for _, t := range tests {
validate := New()
if t.failFast {
validate.FailFast()
}
errs := validate.Struct(t.v)

validationErrs := errs.(ValidationErrors)
IsEqual(len(validationErrs), t.expectedErrLen)
}
}

func TestFailFastSettingStructPartialCtx(t *testing.T) {

tests := []struct {
v Value
failFast bool
expectedErrLen int
}{
{
v: Value{
Street: "",
City: "",
},
failFast: true,
expectedErrLen: 1,
},
{
v: Value{
Street: "",
City: "",
},
failFast: false,
expectedErrLen: 2,
},
}

for _, t := range tests {
validate := New()
if t.failFast {
validate.FailFast()
}
errs := validate.StructPartialCtx(context.TODO(), t.v, "Street", "City")

validationErrs := errs.(ValidationErrors)
IsEqual(len(validationErrs), t.expectedErrLen)
}
}

func TestFailFastSettingStructExceptCtx(t *testing.T) {

tests := []struct {
v Value
failFast bool
expectedErrLen int
}{
{
v: Value{
Street: "",
City: "",
},
failFast: true,
expectedErrLen: 1,
},
{
v: Value{
Street: "",
City: "",
},
failFast: false,
expectedErrLen: 2,
},
}

for _, t := range tests {
validate := New()
if t.failFast {
validate.FailFast()
}
errs := validate.StructPartialCtx(context.TODO(), t.v, "Street", "City")

validationErrs := errs.(ValidationErrors)
IsEqual(len(validationErrs), t.expectedErrLen)
}
}