Add support for custom validation rules

This commit is contained in:
Nise Void 2019-04-26 10:33:41 +02:00
parent 922297b368
commit f79b7c6649
Signed by: NiseVoid
GPG key ID: FBA14AC83EA602F3
4 changed files with 54 additions and 20 deletions

View file

@ -6,15 +6,21 @@ import (
"strconv"
)
func inputInt(kind reflect.Kind, value string) interface{} {
return int(inputSame(reflect.Int, value).(int64))
// InputFunc is a function that converts the parameter of a validation rule to the desired type
type InputFunc func(reflect.Kind, string) interface{}
// InputInt always returns an int
func InputInt(kind reflect.Kind, value string) interface{} {
return int(InputSame(reflect.Int, value).(int64))
}
func inputRegexp(kind reflect.Kind, value string) interface{} {
// InputRegexp always returns a compiled regular expression
func InputRegexp(kind reflect.Kind, value string) interface{} {
return regexp.MustCompile(value)
}
func inputSame(kind reflect.Kind, value string) interface{} {
// InputSame returns the type matching the field
func InputSame(kind reflect.Kind, value string) interface{} {
var val interface{}
var err error