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 4 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 @@ -28,6 +28,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 @@ -76,6 +77,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
38 changes: 25 additions & 13 deletions validator_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,22 @@ type internalValidationFuncWrapper struct {

// Validate contains the validator settings and cache
type Validate struct {
tagName string
pool *sync.Pool
tagNameFunc TagNameFunc
structLevelFuncs map[reflect.Type]StructLevelFuncCtx
customFuncs map[reflect.Type]CustomTypeFunc
aliases map[string]string
validations map[string]internalValidationFuncWrapper
transTagFunc map[ut.Translator]map[string]TranslationFunc // map[<locale>]map[<tag>]TranslationFunc
rules map[reflect.Type]map[string]string
tagCache *tagCache
structCache *structCache
hasCustomFuncs bool
hasTagNameFunc bool
tagName string
pool *sync.Pool
tagNameFunc TagNameFunc
structLevelFuncs map[reflect.Type]StructLevelFuncCtx
customFuncs map[reflect.Type]CustomTypeFunc
aliases map[string]string
validations map[string]internalValidationFuncWrapper
transTagFunc map[ut.Translator]map[string]TranslationFunc // map[<locale>]map[<tag>]TranslationFunc
rules map[reflect.Type]map[string]string
tagCache *tagCache
structCache *structCache
hasCustomFuncs bool
hasTagNameFunc bool
requiredStructEnabled bool
privateFieldValidation bool
isFailFast bool
}

// New returns a new instance of 'validate' with sane defaults.
Expand Down Expand Up @@ -156,6 +157,11 @@ func New(options ...Option) *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 @@ -391,6 +397,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 @@ -437,6 +444,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 @@ -487,6 +495,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 @@ -577,6 +586,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 @@ -648,6 +658,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 @@ -700,6 +711,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
118 changes: 117 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 @@ -13955,6 +13955,122 @@ 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)
}
}

func TestTimeRequired(t *testing.T) {
validate := New()
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
Expand Down