-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_test.go
190 lines (186 loc) · 3.43 KB
/
validation_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
package validation
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestRequired(t *testing.T) {
type T struct {
Data string `json:"data" validate:"required"`
}
models := []struct {
name string
data T
expected []*ValidationErrorMessage
}{
{
name: "required ok",
data: T{
Data: "foo",
},
},
{
name: "required bad",
data: T{
Data: "",
},
expected: []*ValidationErrorMessage{
{
Field: "data",
Message: []interface{}{"This field is required"},
},
},
},
}
v := New()
for _, m := range models {
t.Run(m.name, func(t *testing.T) {
r, err := v.Validate(m.data)
if err != nil {
require.Equal(t, m.expected, r)
} else {
require.Equal(t, nil, err)
}
})
}
}
func TestSubRequired(t *testing.T) {
type Children struct {
Name string `json:"name" validate:"required"`
Active bool `json:"active" validate:"required"`
Level string `json:"level" enum:"beginner,intermediate,advanced"`
}
type Parent struct {
UserId int64 `json:"user_id" validate:"required"`
Children []Children `json:"skill" validate:"required"`
}
models := []struct {
name string
data Parent
expected []*ValidationErrorMessage
}{
{
name: "required ok",
data: Parent{
UserId: 1,
Children: []Children{
{
Name: "foo",
Active: true,
Level: "beginner",
},
},
},
},
}
v := New()
for _, m := range models {
t.Run(m.name, func(t *testing.T) {
r, err := v.Validate(m.data)
if err != nil {
require.Equal(t, m.expected, r)
} else {
require.Equal(t, nil, err)
}
})
}
}
func TestEmail(t *testing.T) {
type T struct {
Data string `json:"email" validate:"email"`
}
models := []struct {
name string
data T
expected []*ValidationErrorMessage
}{
{
name: "email ok",
data: T{
Data: "foo@email.com",
},
},
{
name: "empty email",
data: T{
Data: "",
},
expected: []*ValidationErrorMessage{
{
Field: "email",
Message: []interface{}{"This field must be a valid email address"},
},
},
},
{
name: "email bad 1",
data: T{
Data: "email",
},
expected: []*ValidationErrorMessage{
{
Field: "email",
Message: []interface{}{"This field must be a valid email address"},
},
},
},
{
name: "email bad 2",
data: T{
Data: "email@",
},
expected: []*ValidationErrorMessage{
{
Field: "email",
Message: []interface{}{"This field must be a valid email address"},
},
},
},
{
name: "email bad 3",
data: T{
Data: "@email",
},
expected: []*ValidationErrorMessage{
{
Field: "email",
Message: []interface{}{"This field must be a valid email address"},
},
},
},
{
name: "email bad 4",
data: T{
Data: "@email.com",
},
expected: []*ValidationErrorMessage{
{
Field: "email",
Message: []interface{}{"This field must be a valid email address"},
},
},
},
{
name: "email bad 5",
data: T{
Data: "email.com",
},
expected: []*ValidationErrorMessage{
{
Field: "email",
Message: []interface{}{"This field must be a valid email address"},
},
},
},
}
v := New()
for _, m := range models {
t.Run(m.name, func(t *testing.T) {
r, err := v.Validate(m.data)
if err != nil {
require.Equal(t, m.expected, r)
} else {
require.Equal(t, nil, err)
}
})
}
}