Add support for custom validation rules
This commit is contained in:
parent
922297b368
commit
f79b7c6649
4 changed files with 54 additions and 20 deletions
|
@ -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"`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue