forked from gookit/validate
/
issues_test.go
318 lines (265 loc) · 7.21 KB
/
issues_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package validate
import (
"fmt"
"reflect"
"testing"
"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
)
// https://github.com/gookit/validate/issues/19
func TestIssues19(t *testing.T) {
is := assert.New(t)
// use tag name: country_code
type smsReq struct {
CountryCode string `json:"country_code" validate:"required" filter:"trim|lower"`
Phone string `json:"phone" validate:"required" filter:"trim"`
Type string `json:"type" validate:"required|in:register,forget_password,set_pay_password,reset_pay_password,reset_password" filter:"trim"`
}
req := &smsReq{
" ABcd ", "13677778888 ", "register",
}
v := New(req)
is.True(v.Validate())
sd := v.SafeData()
is.Equal("abcd", sd["CountryCode"])
is.Equal("13677778888", sd["Phone"])
// Notice: since 1.2, filtered value will update to struct
// err := v.BindSafeData(req)
// is.NoError(err)
is.Equal("abcd", req.CountryCode)
is.Equal("13677778888", req.Phone)
// use tag name: countrycode
type smsReq1 struct {
// CountryCode string `json:"countryCode" validate:"required" filter:"trim|lower"`
CountryCode string `json:"countrycode" validate:"required" filter:"trim|lower"`
Phone string `json:"phone" validate:"required" filter:"trim"`
Type string `json:"type" validate:"required|in:register,forget_password,set_pay_password,reset_pay_password,reset_password" filter:"trim"`
}
req1 := &smsReq1{
" ABcd ", "13677778888 ", "register",
}
v = New(req1)
is.True(v.Validate())
sd = v.SafeData()
is.Equal("abcd", sd["CountryCode"])
is.Equal("abcd", req1.CountryCode)
is.Equal("13677778888", req1.Phone)
}
// https://github.com/gookit/validate/issues/20
func TestIssues20(t *testing.T) {
is := assert.New(t)
type setProfileReq struct {
Nickname string `json:"nickname" validate:"string" filter:"trim"`
Avatar string `json:"avatar" validate:"required|url" filter:"trim"`
}
req := &setProfileReq{"123nickname111", "123"}
v := New(req)
is.True(v.Validate())
type setProfileReq1 struct {
Nickname string `json:"nickname" validate:"string" filter:"trim"`
Avatar string `json:"avatar" validate:"required|fullUrl" filter:"trim"`
}
req1 := &setProfileReq1{"123nickname111", "123"}
Config(func(opt *GlobalOption) {
opt.FieldTag = ""
})
v = New(req1)
is.False(v.Validate())
is.Len(v.Errors, 1)
is.Equal("Avatar must be an valid full URL address", v.Errors.One())
ResetOption()
v = New(req1)
is.False(v.Validate())
is.Len(v.Errors, 1)
is.Equal("avatar must be an valid full URL address", v.Errors.One())
}
// https://github.com/gookit/validate/issues/22
func TestIssues22(t *testing.T) {
type userInfo0 struct {
Nickname string `validate:"minLen:6" message:"OO! nickname min len is 6"`
Avatar string `validate:"maxLen:6" message:"OO! avatar max len is %d"`
}
is := assert.New(t)
u0 := &userInfo0{
Nickname: "tom",
Avatar: "https://github.com/gookit/validate/issues/22",
}
v := Struct(u0)
is.False(v.Validate())
is.Equal("OO! nickname min len is 6", v.Errors.FieldOne("Nickname"))
u0 = &userInfo0{
Nickname: "inhere",
Avatar: "some url",
}
v = Struct(u0)
is.False(v.Validate())
is.Equal("OO! avatar max len is 6", v.Errors.FieldOne("Avatar"))
// multi messages
type userInfo1 struct {
Nickname string `validate:"required|minLen:6" message:"required:OO! nickname cannot be empty!|minLen:OO! nickname min len is %d"`
}
u1 := &userInfo1{Nickname: ""}
v = Struct(u1)
is.False(v.Validate())
is.Equal("OO! nickname cannot be empty!", v.Errors.FieldOne("Nickname"))
u1 = &userInfo1{Nickname: "tom"}
v = Struct(u1)
is.False(v.Validate())
is.Equal("OO! nickname min len is 6", v.Errors.FieldOne("Nickname"))
}
// https://github.com/gookit/validate/issues/30
func TestIssues30(t *testing.T) {
v := JSON(`{
"cost_type": 10
}`)
v.StringRule("cost_type", "str_num")
assert.True(t, v.Validate())
assert.Len(t, v.Errors, 0)
}
// https://github.com/gookit/validate/issues/34
func TestIssues34(t *testing.T) {
type STATUS int32
var s1 STATUS = 1
// v.RegisterType(func() {})
// use custom validator
v := New(M{
"age": s1,
})
v.AddValidator("checkAge", func(val interface{}, ints ...int) bool {
return Enum(int32(val.(STATUS)), ints)
})
v.StringRule("age", "required|checkAge:1,2,3,4")
assert.True(t, v.Validate())
// TODO refer https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29
v = New(M{
"age": s1,
})
v.StringRules(MS{
"age": "required|in:1,2,3,4",
})
assert.NotContains(t, []int{1, 2, 3, 4}, s1)
rv := reflect.ValueOf(s1)
// iv := reflect.New()
// sc := rv.Interface()
// fmt.Println(rv.Type().Kind(), sc.(int32))
fmt.Println(rv.Type().Kind())
dump.Println(Enum(s1, []int{1, 2, 3, 4}), Enum(int32(s1), []int{1, 2, 3, 4}))
assert.True(t, v.Validate())
dump.Println(v.Errors)
type someMode string
var m1 someMode = "abc"
v = New(M{
"mode": m1,
})
v.StringRules(MS{
"mode": "required|in:abc,def",
})
assert.True(t, v.Validate())
dump.Println(v.Errors)
}
// https://github.com/gookit/validate/issues/60
func TestIssues60(t *testing.T) {
is := assert.New(t)
m := map[string]interface{}{
"title": "1",
}
v := Map(m)
v.StringRule("title", "in:2,3")
v.AddMessages(map[string]string{
"in": "自定义错误",
})
is.False(v.Validate())
is.Equal("自定义错误", v.Errors.One())
}
// https://github.com/gookit/validate/issues/64
func TestPtrFieldValidation(t *testing.T) {
type Foo struct {
Name *string `validate:"in:henry,jim"`
}
name := "henry"
v := New(&Foo{Name: &name})
assert.True(t, v.Validate())
name = "fish"
valid := New(&Foo{Name: &name})
assert.False(t, valid.Validate())
}
// https://github.com/gookit/validate/issues/58
func TestStructNested(t *testing.T) {
type Org struct {
Company string `validate:"in:A,B,C,D"`
}
type Info struct {
Email string `validate:"email" filter:"trim|lower"`
Age *int `validate:"in:1,2,3,4"`
}
// anonymous struct nested
type User struct {
Name string `validate:"required|string" filter:"trim|lower"`
*Info
Org
Sex string `validate:"string"`
}
// non-anonymous struct nested
type User2 struct {
Name string `validate:"required|string" filter:"trim|lower"`
In Info
Sex string `validate:"string"`
}
// anonymous field test
age := 3
u := &User{
Name: "fish",
Info: &Info{
Email: "fish_yww@163.com",
Age: &age,
},
Org: Org{Company: "E"},
Sex: "male",
}
// anonymous field test
v := Struct(u)
if v.Validate() {
assert.True(t, v.Validate())
} else {
// Print error msg,verify valid
fmt.Println(v.Errors)
assert.False(t, v.Validate())
}
// non-anonymous field test
age = 3
user2 := &User2{
Name: "fish",
In: Info{
Email: "fish_yww@163.com",
Age: &age,
},
Sex: "male",
}
v2 := Struct(user2)
if v2.Validate() {
assert.True(t, v2.Validate())
} else {
// Print error msg,verify valid
fmt.Printf("%v\n", v2.Errors)
assert.False(t, v2.Validate())
}
}
// https://github.com/gookit/validate/issues/78
func TestIssue78(t *testing.T) {
type UserDto struct {
Name string `validate:"required"`
Sex *bool `validate:"required"`
}
//sex := true
u := UserDto{
Name: "abc",
Sex: nil,
}
// 创建 Validation 实例
v := Struct(&u)
if !v.Validate() {
fmt.Println(v.Errors)
} else {
fmt.Println("Success...")
}
}