-
Notifications
You must be signed in to change notification settings - Fork 116
/
morecase_test.go
75 lines (65 loc) · 1.52 KB
/
morecase_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package validate_test
import (
"bytes"
"fmt"
"reflect"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/validate"
)
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 any) 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 any) 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())
}