forked from gookit/validate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering_test.go
142 lines (125 loc) · 3.16 KB
/
filtering_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package validate
import (
"fmt"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilterOnStruct(t *testing.T) {
ris := assert.New(t)
u := &struct {
// Age int `filter:"uint" validate:"int"`
Tres string `filter:"upper" validate:"required|in:ONE,TWO,THREE" `
Name string `filter:"upper" validate:"string"`
}{
"one", "inhere",
}
v := New(u)
ris.True(v.Validate())
// since 1.1.4 filtered value will update to source
ris.Equal("ONE", u.Tres)
ris.Equal("INHERE", u.Name)
// bind filtering and validated data to struct
err := v.BindSafeData(u)
ris.Nil(err)
ris.Equal("ONE", u.Tres)
ris.Equal("INHERE", u.Name)
}
func TestAddFilter(t *testing.T) {
is := assert.New(t)
is.Panics(func() {
AddFilter("myFilter", "invalid")
})
is.Panics(func() {
AddFilter("myFilter", func() {})
})
is.Panics(func() {
AddFilter("bad-name", func() {})
})
is.Panics(func() {
AddFilter("", func() {})
})
is.Panics(func() {
AddFilter("myFilter", func(v string) (bool, int) { return false, 0 })
})
is.Panics(func() {
AddFilter("myFilter", func() interface{} { return nil })
})
AddFilters(M{
"myFilter0": func(val interface{}) string { return "myFilter0" },
})
AddFilter("myFilter1", func(val interface{}) string { return "myFilter1" })
v := New(map[string]interface{}{
"name": " inhere ",
"age": " 50 ",
"key0": "val0",
"key1": "val1",
"tags": "go,php",
})
v.AddFilters(M{
"myFilter2": func(val interface{}, a, b string) (string, error) { return "myFilter2:" + a + b, nil },
})
v.FilterRule("key0", "myFilter0")
v.FilterRules(MS{
"key1": "myFilter2:a,b",
"name": "trim|upper",
"tags": "str2arr:,",
//
"age, not-exist": "trim|int",
})
is.Panics(func() {
v.FilterRule("", "")
})
v.Sanitize() // do filtering
v.Sanitize() // repeat call
is.True(v.IsOK())
is.Equal(50, v.Filtered("age"))
is.Equal("INHERE", v.Filtered("name"))
is.Equal("myFilter0", v.Filtered("key0"))
is.Equal("myFilter2:ab", v.Filtered("key1"))
is.Contains(fmt.Sprint(v.FilteredData()), "key0:myFilter0")
v.Trans().AddMessage("new-key", "msg text")
is.True(v.Trans().HasMessage("new-key"))
is.Equal("msg text", v.Trans().Message("new-key", "some"))
is.Equal("SOME field did not pass validation", v.Trans().Message("not-exist", "SOME"))
v.Trans().Reset()
is.False(v.Trans().HasMessage("new-key"))
// filter fail
v = New(SValues{
"name": {"inhere"},
})
v.AddFilter("myFilter3", func(s string) (string, error) {
return s, fmt.Errorf("report a error")
})
v.FilterRules(MS{
"name": "invalid|int",
})
v.Filtering()
is.True(v.IsFail())
is.Contains(v.Errors, "_filter")
v = New(url.Values{
"age": {"invalid"},
})
v.AddFilter("myFilter3", func(s string) (string, error) {
return s, fmt.Errorf("report a error")
})
v.FilterRules(MS{
"age": "myFilter3",
})
v.Filtering()
is.True(v.IsFail())
is.Equal("report a error", v.Errors.FieldOne("_filter"))
}
// check panic caused nil value with custom filter
func TestFilterRuleNilValue(t *testing.T) {
AddFilter("X", func(in interface{}) interface{} {
return in
})
v := Map(map[string]interface{}{
"bad": nil,
})
v.FilterRule("bad", "X")
assert.NotPanics(t, func() {
v.Validate()
})
}