Compare commits

...

3 Commits

Author SHA1 Message Date
6942b984c1
Fix inconsistent test name 2021-11-11 10:44:47 +01:00
e810582264
Add go.mod 2021-11-11 10:44:34 +01:00
7405c824fe Add lte and gte
Co-authored-by: Crow Crowcrow <crow@fuyu.moe>
Co-committed-by: Crow Crowcrow <crow@fuyu.moe>
2021-11-11 10:43:42 +01:00
3 changed files with 77 additions and 22 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.fuyu.moe/Fuyu/validate
go 1.17

View File

@ -90,6 +90,28 @@ var funcs = map[string]listFuncInfo{
}}, }},
// Integers // Integers
`gte`: {InputSame, Kinds{
reflect.Int: func(rv reflect.Value, val interface{}) bool {
return rv.Int() >= val.(int64)
},
reflect.Uint: func(rv reflect.Value, val interface{}) bool {
return rv.Uint() >= val.(uint64)
},
reflect.Float64: func(rv reflect.Value, val interface{}) bool {
return rv.Float() >= val.(float64)
},
}},
`lte`: {InputSame, Kinds{
reflect.Int: func(rv reflect.Value, val interface{}) bool {
return rv.Int() <= val.(int64)
},
reflect.Uint: func(rv reflect.Value, val interface{}) bool {
return rv.Uint() <= val.(uint64)
},
reflect.Float64: func(rv reflect.Value, val interface{}) bool {
return rv.Float() <= val.(float64)
},
}},
`gt`: {InputSame, Kinds{ `gt`: {InputSame, Kinds{
reflect.Int: func(rv reflect.Value, val interface{}) bool { reflect.Int: func(rv reflect.Value, val interface{}) bool {
return rv.Int() > val.(int64) return rv.Int() > val.(int64)

View File

@ -16,9 +16,9 @@ func TestAddRule(t *testing.T) {
}, },
}) })
var pass = s{`custom`} pass := s{`custom`}
var fail = s{`somethingelse`} fail := s{`somethingelse`}
check(t, pass, 0) check(t, pass, 0)
check(t, fail, 1) check(t, fail, 1)
@ -38,9 +38,9 @@ func TestRuleRequired(t *testing.T) {
} }
str := `` str := ``
var pass = s{&str, make([]int, 1), make([]int, 1), ` `, -1, 1, 0.01, ``, map[int]int{0: 1}} pass := s{&str, make([]int, 1), make([]int, 1), ` `, -1, 1, 0.01, ``, map[int]int{0: 1}}
var fail = s{nil, nil, make([]int, 0), ``, 0, 0, 0.000, nil, nil} fail := s{nil, nil, make([]int, 0), ``, 0, 0, 0.000, nil, nil}
check(t, pass, 0) check(t, pass, 0)
check(t, fail, 9) check(t, fail, 9)
@ -52,9 +52,9 @@ func TestRulePrefixSuffix(t *testing.T) {
B string `validate:"suffix=@"` B string `validate:"suffix=@"`
} }
var pass = s{`#a`, `a@`} pass := s{`#a`, `a@`}
var fail = s{`a#`, `@a`} fail := s{`a#`, `@a`}
check(t, pass, 0) check(t, pass, 0)
check(t, fail, 2) check(t, fail, 2)
@ -65,12 +65,12 @@ func TestRuleContains(t *testing.T) {
A string `validate:"contains=%"` A string `validate:"contains=%"`
} }
var pass1 = s{`a%`} pass1 := s{`a%`}
var pass2 = s{`%a`} pass2 := s{`%a`}
var pass3 = s{`%`} pass3 := s{`%`}
var pass4 = s{`a%a`} pass4 := s{`a%a`}
var fail = s{`aa`} fail := s{`aa`}
check(t, pass1, 0) check(t, pass1, 0)
check(t, pass2, 0) check(t, pass2, 0)
@ -84,11 +84,11 @@ func TestRuleRegexp(t *testing.T) {
A string `validate:"regexp=^[0-9]$"` A string `validate:"regexp=^[0-9]$"`
} }
var pass1 = s{`0`} pass1 := s{`0`}
var pass2 = s{`7`} pass2 := s{`7`}
var fail1 = s{`A`} fail1 := s{`A`}
var fail2 = s{`11`} fail2 := s{`11`}
check(t, pass1, 0) check(t, pass1, 0)
check(t, pass2, 0) check(t, pass2, 0)
@ -103,27 +103,57 @@ func TestRuleEqGtLt(t *testing.T) {
C uint `validate:"lt=1"` C uint `validate:"lt=1"`
} }
var pass = s{3, 100001, 0} pass := s{3, 100001, 0}
var fail1 = s{2, 1e5, 1} fail1 := s{2, 1e5, 1}
var fail2 = s{4, 9999, 2} fail2 := s{4, 9999, 2}
check(t, pass, 0) check(t, pass, 0)
check(t, fail1, 3) check(t, fail1, 3)
check(t, fail2, 3) check(t, fail2, 3)
} }
func TestLenMinMax(t *testing.T) { 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)
}
func TestRuleLenMinMax(t *testing.T) {
type s struct { type s struct {
A string `validate:"len=3"` A string `validate:"len=3"`
B []int `validate:"min=2"` B []int `validate:"min=2"`
C map[int]string `validate:"max=1"` C map[int]string `validate:"max=1"`
} }
var pass = s{`abc`, []int{1, 2}, nil} pass := s{`abc`, []int{1, 2}, nil}
var fail1 = s{`ab`, []int{1}, map[int]string{1: `a`, 2: `b`}} fail1 := s{`ab`, []int{1}, map[int]string{1: `a`, 2: `b`}}
var fail2 = s{`abcd`, nil, nil} fail2 := s{`abcd`, nil, nil}
check(t, pass, 0) check(t, pass, 0)
check(t, fail1, 3) check(t, fail1, 3)