Skip to content

Commit

Permalink
fix: call rule.beforeFunc position is error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 25, 2019
1 parent 5ca096e commit de506ab
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
18 changes: 7 additions & 11 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion validation_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions validation_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"testing"

"github.com/gookit/filter"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de506ab

Please sign in to comment.