2018-10-16 20:18:39 +02:00
|
|
|
package validate
|
|
|
|
|
|
|
|
import (
|
2019-04-26 10:33:41 +02:00
|
|
|
"reflect"
|
2018-10-16 20:18:39 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-04-26 10:33:41 +02:00
|
|
|
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`
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
pass := s{`custom`}
|
2019-04-26 10:33:41 +02:00
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
fail := s{`somethingelse`}
|
2019-04-26 10:33:41 +02:00
|
|
|
|
|
|
|
check(t, pass, 0)
|
|
|
|
check(t, fail, 1)
|
|
|
|
}
|
|
|
|
|
2018-10-16 20:18:39 +02:00
|
|
|
func TestRuleRequired(t *testing.T) {
|
|
|
|
type s struct {
|
|
|
|
A *string `validate:"required"`
|
|
|
|
B []int `validate:"required"`
|
|
|
|
C []int `validate:"required"`
|
|
|
|
D string `validate:"required"`
|
|
|
|
E int `validate:"required"`
|
|
|
|
F uint `validate:"required"`
|
|
|
|
G float64 `validate:"required"`
|
|
|
|
H interface{} `validate:"required"`
|
|
|
|
I map[int]int `validate:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
str := ``
|
2021-11-11 10:43:42 +01:00
|
|
|
pass := s{&str, make([]int, 1), make([]int, 1), ` `, -1, 1, 0.01, ``, map[int]int{0: 1}}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
fail := s{nil, nil, make([]int, 0), ``, 0, 0, 0.000, nil, nil}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
|
|
|
check(t, pass, 0)
|
|
|
|
check(t, fail, 9)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRulePrefixSuffix(t *testing.T) {
|
|
|
|
type s struct {
|
|
|
|
A string `validate:"prefix=#"`
|
|
|
|
B string `validate:"suffix=@"`
|
|
|
|
}
|
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
pass := s{`#a`, `a@`}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
fail := s{`a#`, `@a`}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
|
|
|
check(t, pass, 0)
|
|
|
|
check(t, fail, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRuleContains(t *testing.T) {
|
|
|
|
type s struct {
|
|
|
|
A string `validate:"contains=%"`
|
|
|
|
}
|
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
pass1 := s{`a%`}
|
|
|
|
pass2 := s{`%a`}
|
|
|
|
pass3 := s{`%`}
|
|
|
|
pass4 := s{`a%a`}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
fail := s{`aa`}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
|
|
|
check(t, pass1, 0)
|
|
|
|
check(t, pass2, 0)
|
|
|
|
check(t, pass3, 0)
|
|
|
|
check(t, pass4, 0)
|
|
|
|
check(t, fail, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRuleRegexp(t *testing.T) {
|
|
|
|
type s struct {
|
|
|
|
A string `validate:"regexp=^[0-9]$"`
|
|
|
|
}
|
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
pass1 := s{`0`}
|
|
|
|
pass2 := s{`7`}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
fail1 := s{`A`}
|
|
|
|
fail2 := s{`11`}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
|
|
|
check(t, pass1, 0)
|
|
|
|
check(t, pass2, 0)
|
|
|
|
check(t, fail1, 1)
|
|
|
|
check(t, fail2, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRuleEqGtLt(t *testing.T) {
|
|
|
|
type s struct {
|
|
|
|
A int `validate:"eq=3"`
|
|
|
|
B float64 `validate:"gt=1e5"`
|
|
|
|
C uint `validate:"lt=1"`
|
|
|
|
}
|
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
pass := s{3, 100001, 0}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
fail1 := s{2, 1e5, 1}
|
|
|
|
fail2 := s{4, 9999, 2}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
|
|
|
check(t, pass, 0)
|
|
|
|
check(t, fail1, 3)
|
|
|
|
check(t, fail2, 3)
|
|
|
|
}
|
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
func TestRuleGteLte(t *testing.T) {
|
|
|
|
type s struct {
|
|
|
|
U uint `validate:"gte=0,lte=10"`
|
|
|
|
I int `validate:"gte=-10,lte=0"`
|
|
|
|
F float64 `validate:"gte=0,lte=10"`
|
|
|
|
}
|
|
|
|
|
|
|
|
pass1 := s{0, -10, 0}
|
|
|
|
pass2 := s{10, 0, 10}
|
|
|
|
|
|
|
|
// Uint
|
|
|
|
fail1 := s{11, -10, 0}
|
|
|
|
|
|
|
|
// Int
|
|
|
|
fail2 := s{0, -11, 0}
|
|
|
|
fail3 := s{0, 1, 0}
|
|
|
|
|
|
|
|
// Float
|
|
|
|
fail4 := s{0, -10, -0.0001}
|
|
|
|
fail5 := s{0, -10, 10.0001}
|
|
|
|
|
|
|
|
check(t, pass1, 0)
|
|
|
|
check(t, pass2, 0)
|
|
|
|
check(t, fail1, 1)
|
|
|
|
check(t, fail2, 1)
|
|
|
|
check(t, fail3, 1)
|
|
|
|
check(t, fail4, 1)
|
|
|
|
check(t, fail5, 1)
|
|
|
|
}
|
|
|
|
|
2018-10-16 20:18:39 +02:00
|
|
|
func TestLenMinMax(t *testing.T) {
|
|
|
|
type s struct {
|
|
|
|
A string `validate:"len=3"`
|
|
|
|
B []int `validate:"min=2"`
|
|
|
|
C map[int]string `validate:"max=1"`
|
|
|
|
}
|
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
pass := s{`abc`, []int{1, 2}, nil}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
2021-11-11 10:43:42 +01:00
|
|
|
fail1 := s{`ab`, []int{1}, map[int]string{1: `a`, 2: `b`}}
|
|
|
|
fail2 := s{`abcd`, nil, nil}
|
2018-10-16 20:18:39 +02:00
|
|
|
|
|
|
|
check(t, pass, 0)
|
|
|
|
check(t, fail1, 3)
|
|
|
|
check(t, fail2, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(t *testing.T, c interface{}, errCount int) {
|
2020-04-24 13:01:25 +02:00
|
|
|
t.Helper()
|
2018-10-16 20:18:39 +02:00
|
|
|
errs := Validate(c)
|
|
|
|
if len(errs) != errCount {
|
|
|
|
t.Errorf(`Case %T(%v) should get %d errors, but got %v`, c, c, errCount, errs)
|
|
|
|
}
|
|
|
|
}
|