Add support for custom validation rules
This commit is contained in:
parent
922297b368
commit
f79b7c6649
4 changed files with 54 additions and 20 deletions
38
rules.go
38
rules.go
|
@ -6,17 +6,25 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
type listFunc func(reflect.Value, interface{}) bool
|
||||
// ValidationFunc is a function to validate a field
|
||||
type ValidationFunc func(reflect.Value, interface{}) bool
|
||||
|
||||
// Kinds is a map with validation funcs for each reflect.Kind
|
||||
type Kinds map[reflect.Kind]ValidationFunc
|
||||
|
||||
type listFuncInfo struct {
|
||||
inputFunc func(reflect.Kind, string) interface{}
|
||||
kinds _kinds
|
||||
inputFunc InputFunc
|
||||
kinds Kinds
|
||||
}
|
||||
|
||||
type _kinds map[reflect.Kind]listFunc
|
||||
// AddRule adds a rule to the list of validation functions
|
||||
func AddRule(name string, inputFunc InputFunc, kinds Kinds) {
|
||||
funcs[name] = listFuncInfo{inputFunc, kinds}
|
||||
}
|
||||
|
||||
// nolint: dupl
|
||||
var funcs = map[string]listFuncInfo{
|
||||
`required`: {nil, _kinds{
|
||||
`required`: {nil, Kinds{
|
||||
reflect.Ptr: func(rv reflect.Value, _ interface{}) bool {
|
||||
return !rv.IsNil()
|
||||
},
|
||||
|
@ -44,29 +52,29 @@ var funcs = map[string]listFuncInfo{
|
|||
}},
|
||||
|
||||
// Strings
|
||||
`prefix`: {inputSame, _kinds{
|
||||
`prefix`: {InputSame, Kinds{
|
||||
reflect.String: func(rv reflect.Value, val interface{}) bool {
|
||||
return strings.HasPrefix(rv.String(), val.(string))
|
||||
},
|
||||
}},
|
||||
`suffix`: {inputSame, _kinds{
|
||||
`suffix`: {InputSame, Kinds{
|
||||
reflect.String: func(rv reflect.Value, val interface{}) bool {
|
||||
return strings.HasSuffix(rv.String(), val.(string))
|
||||
},
|
||||
}},
|
||||
`contains`: {inputSame, _kinds{
|
||||
`contains`: {InputSame, Kinds{
|
||||
reflect.String: func(rv reflect.Value, val interface{}) bool {
|
||||
return strings.Contains(rv.String(), val.(string))
|
||||
},
|
||||
}},
|
||||
`regexp`: {inputRegexp, _kinds{
|
||||
`regexp`: {InputRegexp, Kinds{
|
||||
reflect.String: func(rv reflect.Value, val interface{}) bool {
|
||||
return val.(*regexp.Regexp).MatchString(rv.String())
|
||||
},
|
||||
}},
|
||||
|
||||
// Comparisons
|
||||
`eq`: {inputSame, _kinds{
|
||||
`eq`: {InputSame, Kinds{
|
||||
reflect.String: func(rv reflect.Value, val interface{}) bool {
|
||||
return rv.String() == val.(string)
|
||||
},
|
||||
|
@ -82,7 +90,7 @@ var funcs = map[string]listFuncInfo{
|
|||
}},
|
||||
|
||||
// Integers
|
||||
`gt`: {inputSame, _kinds{
|
||||
`gt`: {InputSame, Kinds{
|
||||
reflect.Int: func(rv reflect.Value, val interface{}) bool {
|
||||
return rv.Int() > val.(int64)
|
||||
},
|
||||
|
@ -93,7 +101,7 @@ var funcs = map[string]listFuncInfo{
|
|||
return rv.Float() > val.(float64)
|
||||
},
|
||||
}},
|
||||
`lt`: {inputSame, _kinds{
|
||||
`lt`: {InputSame, Kinds{
|
||||
reflect.Int: func(rv reflect.Value, val interface{}) bool {
|
||||
return rv.Int() < val.(int64)
|
||||
},
|
||||
|
@ -106,7 +114,7 @@ var funcs = map[string]listFuncInfo{
|
|||
}},
|
||||
|
||||
// Slices, maps & strings
|
||||
`len`: {inputInt, _kinds{
|
||||
`len`: {InputInt, Kinds{
|
||||
reflect.Slice: func(rv reflect.Value, val interface{}) bool {
|
||||
return rv.Len() == val.(int)
|
||||
},
|
||||
|
@ -117,7 +125,7 @@ var funcs = map[string]listFuncInfo{
|
|||
return rv.Len() == val.(int)
|
||||
},
|
||||
}},
|
||||
`min`: {inputInt, _kinds{
|
||||
`min`: {InputInt, Kinds{
|
||||
reflect.Slice: func(rv reflect.Value, val interface{}) bool {
|
||||
return rv.Len() >= val.(int)
|
||||
},
|
||||
|
@ -128,7 +136,7 @@ var funcs = map[string]listFuncInfo{
|
|||
return rv.Len() >= val.(int)
|
||||
},
|
||||
}},
|
||||
`max`: {inputInt, _kinds{
|
||||
`max`: {InputInt, Kinds{
|
||||
reflect.Slice: func(rv reflect.Value, val interface{}) bool {
|
||||
return rv.Len() <= val.(int)
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue