From de506abc94748afc154b7b74b280230a3222813e Mon Sep 17 00:00:00 2001 From: inhere Date: Sun, 25 Aug 2019 14:11:13 +0800 Subject: [PATCH] fix: call rule.beforeFunc position is error --- validate.go | 18 +++++++----------- validation_rule.go | 4 +++- validation_rule_test.go | 39 +++++++++++++++++++++++++++++++++++++++ validators.go | 2 +- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/validate.go b/validate.go index 4004944..b2582e1 100644 --- a/validate.go +++ b/validate.go @@ -38,6 +38,11 @@ func (r *Rule) Apply(v *Validation) (stop bool) { continue } + // has beforeFunc. if return false, skip validate + if r.beforeFunc != nil && !r.beforeFunc(field, v) { + continue + } + // uploaded file check if isFileValidator(name) { // build and collect error message @@ -85,18 +90,14 @@ func (r *Rule) Apply(v *Validation) (stop bool) { } func (r *Rule) fileValidate(field, name string, v *Validation) (ok bool) { - // beforeFunc return false, skip validate - if r.beforeFunc != nil && !r.beforeFunc(field, v) { - return false - } - + // check data source form, ok := v.data.(*FormData) if !ok { return } // skip on empty AND field not exist - if v.SkipOnEmpty && !form.HasFile(field) { + if r.skipEmpty && !form.HasFile(field) { return true } @@ -132,11 +133,6 @@ func (r *Rule) valueValidate(field, name string, val interface{}, v *Validation) return true } - // beforeFunc return false, skip validate - if r.beforeFunc != nil && !r.beforeFunc(field, v) { - return false - } - // empty value AND skip on empty. isNotRequired := name != "required" if r.skipEmpty && isNotRequired && IsEmpty(val) { diff --git a/validation_rule.go b/validation_rule.go index 81dd909..1b55c6a 100644 --- a/validation_rule.go +++ b/validation_rule.go @@ -33,8 +33,10 @@ type Rule struct { validator string // arguments for the validator arguments []interface{} - // some functions + // --- some hook functions + // has beforeFunc. if return false, skip validate current rule beforeFunc func(field string, v *Validation) bool // func (val interface{}) bool + // you can custom filter func filterFunc func(val interface{}) (interface{}, error) // custom check func's mate info checkFuncMeta *funcMeta diff --git a/validation_rule_test.go b/validation_rule_test.go index c7419f3..acc8b27 100644 --- a/validation_rule_test.go +++ b/validation_rule_test.go @@ -4,6 +4,7 @@ import ( "net/url" "testing" + "github.com/gookit/filter" "github.com/stretchr/testify/assert" ) @@ -49,5 +50,43 @@ func TestRule(t *testing.T) { } func TestRule_SetBeforeFunc(t *testing.T) { + is := assert.New(t) + mp := M{ + "name": "inhere", + "avatar": "/some/file", + } + + v := Map(mp) + v.AddRule("avatar", "isFile") + is.False(v.Validate()) + is.Equal("avatar must be an uploaded file", v.Errors.One()) + + // use SetBeforeFunc + v = Map(mp) + v. + AddRule("avatar", "isFile"). + SetBeforeFunc(func(field string, v *Validation) bool { + // return false for skip validate + return false + }) + + v.Validate() + is.True(v.IsOK()) +} + +func TestRule_SetFilterFunc(t *testing.T) { + is := assert.New(t) + v := Map(M{ + "name": "inhere", + "age": "abc", + }) + + v. + AddRule("age", "int", 1, 100). + SetFilterFunc(func(val interface{}) (i interface{}, e error) { + return filter.Int(val) + }) + is.False(v.Validate()) + is.Equal(`strconv.Atoi: parsing "abc": invalid syntax`, v.Errors.One()) } diff --git a/validators.go b/validators.go index 2349123..7112aa1 100644 --- a/validators.go +++ b/validators.go @@ -594,7 +594,7 @@ func IsInt(val interface{}, minAndMax ...int64) (ok bool) { return intVal >= minVal && intVal <= maxVal } -// IsString check, and support length check. +// IsString check and support length check. // Usage: // ok := IsString(val) // ok := IsString(val, 5) // with min len check