-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_test.go
181 lines (156 loc) · 4.82 KB
/
rules_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
package laravalidate
import (
"context"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func validatorCtx(value *reflect.Value, valueType reflect.Type, args []string) *ValidatorCtx {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
return &ValidatorCtx{
ctx: ctx,
Needle: Needle{
Value: value,
Type: valueType,
},
Args: args,
state: &ValidatorCtxState{
bail: false,
state: map[string]any{},
stack: Stack{},
validator: newValidator(ctx, nil, reflect.ValueOf(struct{}{}), GoMode),
},
}
}
// TestRulesFuzz tests if no pannics occur when running all rules
func TestRulesFuzz(t *testing.T) {
// var empty any
args := [][]string{
{},
{"foo"},
{"1"},
{"2", "4"},
{"2", "foo"},
}
testValue := "foo"
testValuePtr := &testValue
testValuePtrPtr := &testValuePtr
testValuePtrPtrPtr := &testValuePtrPtr
values := []any{
testValue,
testValuePtr,
testValuePtrPtr,
testValuePtrPtrPtr,
3,
uint8(3),
1,
uint8(1),
int16(1),
-1,
0,
3.5,
true,
false,
"",
"yes",
"no",
[]string{},
[]string{"foo"},
[]string{"foo", "bar"},
`{"hello": "world"}`,
[]byte("hello world"),
[]rune("hello world"),
'a',
byte('a'),
func() {},
map[string]string{},
map[string]string{
"foo": "bar",
},
}
for name, validator := range validators {
t.Run(name, func(t *testing.T) {
for _, arg := range args {
for _, value := range values {
valueReflection := reflect.ValueOf(value)
valueType := valueReflection.Type()
assert.NotPanicsf(t, func() {
validator.Fn(validatorCtx(nil, valueType, arg))
}, "value=%+v, args=%+v onlyType=true", value, arg)
assert.NotPanicsf(t, func() {
validator.Fn(validatorCtx(&valueReflection, valueType, arg))
}, "value=%+v, args=%+v onlyType=false", value, arg)
}
}
})
}
}
func validationRulePasses(t *testing.T, validator ValidatorFn, value any, args []string, baseStruct ...any) {
valueReflection := reflect.ValueOf(value)
valueType := valueReflection.Type()
failReason, passes := validator(validatorCtx(&valueReflection, valueType, args))
assert.Truef(t, passes, "failHint=%s, value=%+v, args=%+v", failReason, value, args)
}
func validationRuleInvalid(t *testing.T, validator ValidatorFn, value any, args []string) {
valueReflection := reflect.ValueOf(value)
valueType := valueReflection.Type()
_, passes := validator(validatorCtx(&valueReflection, valueType, args))
assert.Falsef(t, passes, "value=%+v, args=%+v", value, args)
}
func TestURL(t *testing.T) {
validationRulePasses(t, URL, "http://example.com", []string{"http", "https"})
validationRulePasses(t, URL, "http://example.com", nil)
validationRulePasses(t, URL, "steam://some_game", nil)
validationRuleInvalid(t, URL, "this is not a url", nil)
validationRuleInvalid(t, URL, "steam://some_game", []string{"http", "https"})
}
func TestUuid(t *testing.T) {
validationRulePasses(t, Uuid, "550e8400-e29b-41d4-a716-446655440000", nil)
validationRulePasses(t, Uuid, "550e8400-e29b-41d4-a716-446655440000", []string{"3", "4", "5"})
validationRuleInvalid(t, Uuid, "this is not a uuid", nil)
validationRuleInvalid(t, Uuid, "550e8400-e29b-41d4-a716-446655440000", []string{"1", "2", "3"})
}
func TestRegex(t *testing.T) {
validationRulePasses(t, Regex, "foo", []string{"/^foo$/"})
validationRuleInvalid(t, NotRegex, "foo", []string{"/^foo$/"})
validationRuleInvalid(t, Regex, "foo", []string{"/^bar$/"})
validationRulePasses(t, NotRegex, "foo", []string{"/^bar$/"})
}
func TestIp(t *testing.T) {
validationRulePasses(t, IP, "1.1.1.1", nil)
validationRulePasses(t, IPV4, "1.1.1.1", nil)
validationRuleInvalid(t, IPV6, "1.1.1.1", nil)
validationRulePasses(t, IP, "2606:4700:4700::1111", nil)
validationRuleInvalid(t, IPV4, "2606:4700:4700::1111", nil)
validationRulePasses(t, IPV6, "2606:4700:4700::1111", nil)
validationRuleInvalid(t, IP, "This is not an ip", nil)
}
func TestMime(t *testing.T) {
validationRulePasses(t, Mimetypes, "image/png", []string{"image/png"})
validationRuleInvalid(t, Mimetypes, "image/jpg", []string{"image/png"})
validationRulePasses(t, Mimetypes, "image/png", []string{"image/*"})
validationRuleInvalid(t, Mimetypes, "application/html", []string{"image/*"})
validationRulePasses(t, Mimes, "image/png", []string{"png"})
validationRulePasses(t, Mimes, "image/jpeg", []string{"jpeg"})
}
func TestMac(t *testing.T) {
validationRulePasses(t, MacAddress, "00:00:5e:00:53:01", nil)
validationRuleInvalid(t, MacAddress, "00:00:5e", nil)
}
func TestConfirmed(t *testing.T) {
v := &testValidator{t}
type Test struct {
Field string `validate:"required|confirmed"`
FieldConfirmation string
}
v.AssertInvalid(Test{
Field: "Foo",
FieldConfirmation: "Bar",
})
v.AssertValid(Test{
Field: "Foo",
FieldConfirmation: "Foo",
})
}