-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_test.go
More file actions
302 lines (266 loc) · 7.35 KB
/
Copy pathvalidator_test.go
File metadata and controls
302 lines (266 loc) · 7.35 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
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
package gencfg
import (
"errors"
"testing"
validator "github.com/go-playground/validator/v10"
)
func TestValidateValidStruct(t *testing.T) {
type config struct {
Host string `validate:"required"`
Port int `validate:"required,min=1,max=65535"`
}
cfg := &config{Host: "localhost", Port: 8080}
if err := Validate(cfg); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
func TestValidateRequiredFieldMissing(t *testing.T) {
type config struct {
Host string `validate:"required"`
Port int `validate:"required"`
}
cfg := &config{Host: "", Port: 0}
err := Validate(cfg)
if err == nil {
t.Fatal("expected validation error, got nil")
}
var validationErrors validator.ValidationErrors
if !errors.As(err, &validationErrors) {
t.Fatalf("expected validator.ValidationErrors, got %T: %v", err, err)
}
// Both fields should fail
fields := make(map[string]bool)
for _, fe := range validationErrors {
fields[fe.Field()] = true
}
if !fields["Host"] {
t.Error("expected Host field to fail validation")
}
if !fields["Port"] {
t.Error("expected Port field to fail validation")
}
}
func TestValidateMinMax(t *testing.T) {
type config struct {
Port int `validate:"min=1,max=65535"`
}
tests := []struct {
name string
port int
wantErr bool
}{
{"valid_low", 1, false},
{"valid_mid", 8080, false},
{"valid_high", 65535, false},
{"too_low", 0, true},
{"too_high", 65536, true},
{"negative", -1, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &config{Port: tt.port}
err := Validate(cfg)
if tt.wantErr && err == nil {
t.Errorf("expected error for port=%d, got nil", tt.port)
}
if !tt.wantErr && err != nil {
t.Errorf("expected no error for port=%d, got: %v", tt.port, err)
}
})
}
}
func TestValidateStringConstraints(t *testing.T) {
type config struct {
Name string `validate:"required,min=3,max=20"`
Email string `validate:"required,email"`
}
tests := []struct {
name string
cfg config
wantErr bool
}{
{"valid", config{Name: "myapp", Email: "test@example.com"}, false},
{"name_too_short", config{Name: "ab", Email: "test@example.com"}, true},
{"invalid_email", config{Name: "myapp", Email: "not-an-email"}, true},
{"empty_name", config{Name: "", Email: "test@example.com"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := tt.cfg
err := Validate(&cfg)
if tt.wantErr && err == nil {
t.Error("expected error, got nil")
}
if !tt.wantErr && err != nil {
t.Errorf("expected no error, got: %v", err)
}
})
}
}
func TestValidateNestedStruct(t *testing.T) {
type database struct {
Host string `validate:"required"`
Port int `validate:"required,min=1"`
}
type config struct {
DB database `validate:"required"`
}
// Valid nested struct
cfg := &config{DB: database{Host: "db.local", Port: 5432}}
if err := Validate(cfg); err != nil {
t.Fatalf("expected no error for valid nested struct, got: %v", err)
}
// Invalid nested struct (missing required field)
cfg = &config{DB: database{Host: "", Port: 5432}}
err := Validate(cfg)
if err == nil {
t.Fatal("expected error for invalid nested struct, got nil")
}
var validationErrors validator.ValidationErrors
if !errors.As(err, &validationErrors) {
t.Fatalf("expected validator.ValidationErrors, got %T", err)
}
// The failing field should be the nested Host
found := false
for _, fe := range validationErrors {
if fe.StructNamespace() == "config.DB.Host" {
found = true
break
}
}
if !found {
t.Error("expected DB.Host to fail validation")
}
}
func TestValidateWithAdditionalChecks(t *testing.T) {
type config struct {
MinPort int `validate:"required,min=1"`
MaxPort int `validate:"required,min=1"`
}
// Custom check: MaxPort must be greater than MinPort
customCheck := func(sl validator.StructLevel) {
cfg := sl.Current().Interface().(config)
if cfg.MaxPort <= cfg.MinPort {
sl.ReportError(cfg.MaxPort, "MaxPort", "MaxPort", "gtfield", "MinPort")
}
}
// Valid: MaxPort > MinPort
cfg := &config{MinPort: 1000, MaxPort: 2000}
if err := Validate(cfg, WithAdditionalChecks(customCheck)); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
// Invalid: MaxPort <= MinPort
cfg = &config{MinPort: 2000, MaxPort: 1000}
err := Validate(cfg, WithAdditionalChecks(customCheck))
if err == nil {
t.Fatal("expected error for MaxPort <= MinPort, got nil")
}
var validationErrors validator.ValidationErrors
if !errors.As(err, &validationErrors) {
t.Fatalf("expected validator.ValidationErrors, got %T", err)
}
found := false
for _, fe := range validationErrors {
if fe.Field() == "MaxPort" {
found = true
break
}
}
if !found {
t.Error("expected MaxPort to fail custom validation")
}
// Equal ports should also fail
cfg = &config{MinPort: 1000, MaxPort: 1000}
if err := Validate(cfg, WithAdditionalChecks(customCheck)); err == nil {
t.Error("expected error for equal ports, got nil")
}
}
func TestValidateWithoutAdditionalChecks(t *testing.T) {
// Ensure Validate works correctly without any options
type config struct {
Name string `validate:"required"`
}
cfg := &config{Name: "test"}
if err := Validate(cfg); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
func TestValidateNoTags(t *testing.T) {
// Struct with no validate tags should always pass
type config struct {
Name string
Port int
}
cfg := &config{}
if err := Validate(cfg); err != nil {
t.Fatalf("expected no error for struct with no validate tags, got: %v", err)
}
}
func TestValidateMultipleTagFailures(t *testing.T) {
type config struct {
Host string `validate:"required"`
Port int `validate:"required,min=1"`
Name string `validate:"required,min=3"`
}
// All fields invalid
cfg := &config{}
err := Validate(cfg)
if err == nil {
t.Fatal("expected error, got nil")
}
var validationErrors validator.ValidationErrors
if !errors.As(err, &validationErrors) {
t.Fatalf("expected validator.ValidationErrors, got %T", err)
}
if len(validationErrors) < 3 {
t.Errorf("expected at least 3 validation errors, got %d", len(validationErrors))
}
}
func TestValidateOneofTag(t *testing.T) {
type config struct {
Env string `validate:"required,oneof=dev staging production"`
}
tests := []struct {
name string
env string
wantErr bool
}{
{"valid_dev", "dev", false},
{"valid_staging", "staging", false},
{"valid_production", "production", false},
{"invalid_value", "testing", true},
{"empty", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &config{Env: tt.env}
err := Validate(cfg)
if tt.wantErr && err == nil {
t.Errorf("expected error for env=%q, got nil", tt.env)
}
if !tt.wantErr && err != nil {
t.Errorf("expected no error for env=%q, got: %v", tt.env, err)
}
})
}
}
func TestValidateSliceField(t *testing.T) {
type config struct {
Tags []string `validate:"required,min=1"`
}
// Valid: non-empty slice
cfg := &config{Tags: []string{"web"}}
if err := Validate(cfg); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
// Invalid: empty slice
cfg = &config{Tags: []string{}}
if err := Validate(cfg); err == nil {
t.Error("expected error for empty slice, got nil")
}
// Invalid: nil slice
cfg = &config{Tags: nil}
if err := Validate(cfg); err == nil {
t.Error("expected error for nil slice, got nil")
}
}