代码拉取完成,页面将自动刷新
package validate_test
import (
"bytes"
"fmt"
"reflect"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/validate"
"github.com/stretchr/testify/assert"
)
func TestValidation_custom_type(t *testing.T) {
type someMode string
var m1 someMode = "abc"
v := validate.New(validate.M{
"mode": m1,
})
v.StringRules(validate.MS{
"mode": "required|in:abc,def",
})
assert.True(t, v.Validate())
dump.Println(
reflect.ValueOf(m1).Kind().String(),
validate.Enum(m1, []string{"abc", "def"}),
)
}
// func TestValidation_custom_required1(t *testing.T) {
// type Data struct {
// Name string `validate:"required"`
// Age int `validate:"required" message:"age is required"`
// }
//
// v := validate.New(&Data{
// Name: "tom",
// Age: 0,
// })
// v.AddValidator("required", func(val interface{}) bool {
// dump.V(val)
// // do something ...
// return false
// })
//
// err := v.ValidateE()
// assert.Error(t, err)
// assert.Equal(t, "age is required", err.One())
// }
func TestValidation_custom_required2(t *testing.T) {
type Data struct {
Age int `validate:"required_custom" message:"age is required"`
Name string `validate:"required"`
}
v := validate.New(&Data{
Name: "tom",
Age: 0,
})
buf := new(bytes.Buffer)
v.AddValidator("required_custom", func(val interface{}) bool {
buf.WriteString("value:")
buf.WriteString(fmt.Sprint(val))
return false
})
ok := v.Validate()
assert.False(t, ok)
assert.Equal(t, "age is required", v.Errors.One())
assert.Equal(t, "value:0", buf.String())
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。