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

@ -1,9 +1,29 @@
package validate
import (
"reflect"
"testing"
)
func TestAddRule(t *testing.T) {
type s struct {
A string `validate:"custom"`
}
AddRule(`custom`, nil, Kinds{
reflect.String: func(rv reflect.Value, _ interface{}) bool {
return rv.String() == `custom`
},
})
var pass = s{`custom`}
var fail = s{`somethingelse`}
check(t, pass, 0)
check(t, fail, 1)
}
func TestRuleRequired(t *testing.T) {
type s struct {
A *string `validate:"required"`