1 Star 0 Fork 13

chobits81/validate

forked from gookit/validate 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
validate_test.go 3.72 KB
一键复制 编辑 原始数据 按行查看 历史
inhere 提交于 2020-06-21 22:39 . add some method and more tests
package validate
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
v := New(map[string][]string{
"age": {"12"},
"name": {"inhere"},
})
v.StringRules(MS{
"age": "required|strInt",
"name": "required|string:3|strLen:4,6",
})
assert.True(t, v.Validate())
}
func TestRule_Apply(t *testing.T) {
is := assert.New(t)
mp := M{
"name": "inhere",
"code": "2363",
}
v := Map(mp)
v.ConfigRules(MS{
"name": `regex:\w+`,
})
v.AddRule("name", "stringLength", 3)
v.StringRule("code", `required|regex:\d{4,6}`)
is.True(v.Validate())
}
func TestStructUseDefault(t *testing.T) {
is := assert.New(t)
type user struct {
Name string `validate:"required|default:tom" filter:"trim|upper"`
Age int `validate:"uint|default:23"`
}
u := &user{Age: 90}
v := New(u)
is.True(v.Validate())
is.Equal("tom", u.Name)
u = &user{Name: "inhere"}
v = New(u)
is.True(v.Validate())
// fmt.Println(v.SafeData())
// check/filter default value
u = &user{Age: 90}
v = New(u)
v.CheckDefault = true
is.True(v.Validate())
is.Equal("TOM", u.Name)
}
func TestValidation_RequiredIf(t *testing.T) {
v := New(M{
"name": "lee",
"age": "12",
})
v.StringRules(MS{
"age": "required_if:name,lee",
"nothing": "required_if:age,12,13,14",
})
v.Validate()
assert.Equal(t, "nothing is required when age is [12 13 14]", v.Errors.One())
}
func TestValidation_RequiredUnless(t *testing.T) {
v := New(M{
"age": "18",
"name": "lee",
"nothing": "",
})
v.StringRules(MS{
"age": "required_unless:name,lee",
"nothing": "required_unless:age,12,13,14",
})
v.Validate()
assert.Equal(t, v.Errors.One(), "nothing field is required unless age is in [12 13 14]")
}
func TestValidation_RequiredWith(t *testing.T) {
v := New(M{
"age": "18",
"name": "test",
})
v.StringRules(MS{
"age": "required_with:name,city",
"anything": "required_with:sex,city",
"nothing": "required_with:age,name",
})
v.Validate()
assert.Equal(t, v.Errors.One(), "nothing field is required when [age name] is present")
}
func TestValidation_RequiredWithAll(t *testing.T) {
v := New(M{
"age": "18",
"name": "test",
"sex": "man",
"nothing": "",
})
v.StringRules(MS{
"age": "required_with_all:name,sex,city",
"anything": "required_with_all:school,city",
"nothing": "required_with_all:age,name,sex",
})
v.Validate()
// fmt.Println(v.Errors)
assert.Equal(t, v.Errors.One(), "nothing field is required when [age name sex] is present")
}
func TestValidation_RequiredWithout(t *testing.T) {
v := New(M{
"age": "18",
"name": "test",
})
v.StringRules(MS{
"age": "required_without:city",
"anything": "required_without:age,name",
"nothing": "required_without:sex,name",
})
v.Validate()
assert.Equal(t, v.Errors.One(), "nothing field is required when [sex name] is not present")
}
func TestValidation_RequiredWithoutAll(t *testing.T) {
v := New(M{
"age": "18",
"name": "test",
"nothing": "",
})
v.StringRules(MS{
"age": "required_without_all:name,city",
"anything": "required_without:age,name",
"nothing": "required_without_all:sex,city",
})
v.Validate()
assert.Equal(t, "nothing field is required when none of [sex city] are present", v.Errors.One())
}
func TestVariadicArgs(t *testing.T) {
// use custom validator
v := New(M{
"age": 2,
})
v.AddValidator("checkAge", func(val interface{}, ints ...int) bool {
return Enum(val, ints)
})
v.StringRule("age", "required|checkAge:1,2,3,4")
assert.True(t, v.Validate())
v = New(M{
"age": 2,
})
v.AddValidator("checkAge", func(val interface{}, ints ...interface{}) bool {
return Enum(val, ints)
})
v.StringRule("age", "required|checkAge:1,2,3,4")
ok := v.Validate()
assert.True(t, ok)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/chobits81/validate.git
git@gitee.com:chobits81/validate.git
chobits81
validate
validate
master

搜索帮助