-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_test.go
174 lines (139 loc) · 3.55 KB
/
std_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package validator
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStdOptional_String(t *testing.T) {
Register("post_optional_str", func(v string, cmp string) (bool, string) {
return v == cmp, "should equal to " + cmp
})
type TestStruct struct {
Attr string `validate:"optional,post_optional_str(foobar)"`
}
var err error
var ts TestStruct
err = Validate(ts)
assert.NoError(t, err)
ts.Attr = "foobar"
err = Validate(ts)
assert.NoError(t, err)
ts.Attr = "barbaz"
err = Validate(ts)
assert.Error(t, err)
assert.Equal(t, "Validation failed for field \"Attr\": should equal to foobar", err.Error())
}
func TestStdOptional_Uint32(t *testing.T) {
Register("post_optional_uint32", func(v uint32, cmp uint32) (bool, string) {
return v == cmp, fmt.Sprintf("should equal to %d", cmp)
})
type TestStruct struct {
Attr uint32 `validate:"optional,post_optional_uint32(42)"`
}
var err error
var ts TestStruct
err = Validate(ts)
assert.NoError(t, err)
ts.Attr = 42
err = Validate(ts)
assert.NoError(t, err)
ts.Attr = 123
err = Validate(ts)
assert.Error(t, err)
assert.Equal(t, "Validation failed for field \"Attr\": should equal to 42", err.Error())
}
func TestStdOptional_Pointer(t *testing.T) {
type TestSubStruct struct {
Attr string `validate:"nonempty"`
}
type TestStruct struct {
SubStruct *TestSubStruct `validate:"optional"`
}
var err error
var ts TestStruct
err = Validate(ts)
assert.NoError(t, err)
ts.SubStruct = &TestSubStruct{}
err = Validate(ts)
assert.Error(t, err)
assert.Equal(t, "Validation failed for field \"Attr\": should not be empty", err.Error())
ts.SubStruct.Attr = "foobar"
err = Validate(ts)
assert.NoError(t, err)
}
func TestStdEmpty(t *testing.T) {
type TestStruct struct {
Attr int `validate:"empty"`
}
var err error
var ts TestStruct
err = Validate(ts)
assert.NoError(t, err)
ts.Attr = 42
err = Validate(ts)
assert.Error(t, err)
assert.Equal(t, "Validation failed for field \"Attr\": should be empty", err.Error())
}
func TestStdEnum(t *testing.T) {
type TestStruct struct {
Attr string `validate:"enum(foo, bar, baz, boo)"`
}
var err error
legal := []string{"foo", "bar", "baz", "boo"}
illegal := []string{"bak", "bazz", "123"}
for _, v := range legal {
var ts TestStruct
ts.Attr = v
err = Validate(ts)
assert.NoError(t, err)
}
for _, v := range illegal {
var ts TestStruct
ts.Attr = v
err = Validate(ts)
assert.Error(t, err)
assert.Equal(t, "Validation failed for field \"Attr\": should be in range [foo bar baz boo]", err.Error())
}
}
func TestStdLen(t *testing.T) {
type TestStruct struct {
Str5 string `validate:"len(5)"`
}
var err error
valid := []string{"hello", "world"}
invalid := []string{"", "hello!", "worl"}
for _, v := range valid {
var ts TestStruct
ts.Str5 = v
err = Validate(ts)
assert.NoError(t, err)
}
for _, v := range invalid {
var ts TestStruct
ts.Str5 = v
err = Validate(ts)
assert.Error(t, err)
assert.Equal(t, "Validation failed for field \"Str5\": length must be exactly 5", err.Error())
}
}
func TestStdMaxLen(t *testing.T) {
type TestStruct struct {
Str5 string `validate:"maxlen(5)"`
}
var err error
valid := []string{"hello", "", "hey"}
invalid := []string{"hello!", "world123"}
for _, v := range valid {
var ts TestStruct
ts.Str5 = v
err = Validate(ts)
assert.NoError(t, err)
}
for _, v := range invalid {
var ts TestStruct
ts.Str5 = v
err = Validate(ts)
assert.Error(t, err)
assert.Equal(t, "Validation failed for field \"Str5\": length must be up to 5", err.Error())
}
}