-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathissues_x2_test.go
More file actions
107 lines (90 loc) · 2.63 KB
/
issues_x2_test.go
File metadata and controls
107 lines (90 loc) · 2.63 KB
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
package validate_test
import (
"mime/multipart"
"net/url"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/validate"
)
// https://github.com/gookit/validate/issues/227
// 一个 key 包含多个上传文件时,除了第一个文件,其他文件被丢弃,导致 BindSafeData 行为非预期
func Test_Issue227(t *testing.T) {
type UserForm struct {
Name string
File []*multipart.FileHeader
}
d := validate.FromURLValues(url.Values{
"name": {"inhere"},
"age": {"30"},
})
// add files
d.AddFile("File", &multipart.FileHeader{Filename: "test1.txt"}, &multipart.FileHeader{Filename: "test2.txt"})
v := d.Create()
v.AddRule("File", "min_len", 1)
assert.True(t, v.Validate())
dump.P(v.Errors)
assert.Nil(t, v.Errors.ErrOrNil())
u := &UserForm{}
err := v.BindStruct(u)
assert.NoError(t, err)
dump.P(u)
}
// https://github.com/gookit/validate/issues/259 Embedded structs are not validated properly #259
// https://github.com/gookit/validate/issues/272 eqField对于指针类型数据无法正确校验
func Test_Issue272(t *testing.T) {
type T272 struct {
FieldA *string `validate:"required"`
FieldB *string `validate:"required|eqField:FieldA"`
}
// test eqField
var str = "abc"
var str1 = "bcd"
v := validate.Struct(&T272{
FieldA: &str,
FieldB: &str1,
})
assert.False(t, v.Validate())
assert.Len(t, v.Errors, 1)
assert.ErrSubMsg(t, v.Errors, "FieldB value must be equal the field FieldA")
var str2 = "abc"
v = validate.Struct(&T272{
FieldA: &str,
FieldB: &str2,
})
assert.True(t, v.Validate())
assert.Nil(t, v.Errors.ErrOrNil())
// nil value
v = validate.Struct(&T272{
FieldA: nil,
FieldB: nil,
})
assert.False(t, v.Validate())
assert.Len(t, v.Errors, 1)
assert.ErrSubMsg(t, v.Errors, "FieldA is required")
}
// https://github.com/gookit/validate/issues/316
// The int validator failed to validate a number exceeds the range of int64
func Test_Issue316(t *testing.T) {
data := []byte(`{"value": 9223372036854775807}`)
t.Run("not use filter", func(t *testing.T) {
dataFace, err := validate.FromJSONBytes(data)
assert.NoErr(t, err)
v := dataFace.Create()
v.StringRule("value", "int")
assert.False(t, v.Validate())
dump.P(v.Errors)
assert.Err(t, v.Errors.ErrOrNil())
assert.Equal(t, "value value must be an integer", v.Errors.One())
})
t.Run("use filter", func(t *testing.T) {
dataFace, err := validate.FromJSONBytes(data)
assert.NoErr(t, err)
v := dataFace.Create()
v.FilterRule("value", "int64")
v.StringRule("value", "int")
assert.True(t, v.Validate())
assert.Nil(t, v.Errors.ErrOrNil())
dump.P(v.SafeData())
})
}