-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_struct_test.go
More file actions
178 lines (173 loc) · 4.39 KB
/
validate_struct_test.go
File metadata and controls
178 lines (173 loc) · 4.39 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
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package validate
import (
"context"
"errors"
"testing"
)
func TestValidator_ValidateStruct(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
rules []Rule
input any
wantErr bool
errString string
}{
{
name: "success simple",
rules: []Rule{
{Name: "nonempty", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
s, ok := value.(string)
if !ok || s == "" {
return errors.New("must be non-empty")
}
return nil
})},
},
input: struct {
Name string `validate:"nonempty"`
}{Name: "test"},
wantErr: false,
},
{
name: "required field missing",
rules: []Rule{
{Name: "nonempty", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
s, ok := value.(string)
if !ok || s == "" {
return errors.New("must be non-empty")
}
return nil
})},
},
input: struct {
Name string `validate:"required;nonempty"`
}{Name: ""},
wantErr: true,
},
{
name: "required field zero but optional ok",
rules: []Rule{
{Name: "nonempty", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
return nil
})},
},
input: struct {
Name string `validate:"required;nonempty"`
}{Name: ""},
wantErr: false,
},
{
name: "rule not found",
rules: []Rule{
{Name: "exists", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error { return nil })},
},
input: struct {
Field string `validate:"unknown"`
}{Field: "test"},
wantErr: true,
errString: "validator `unknown` not found for field `.Field`",
},
{
name: "non-exported field",
rules: []Rule{},
input: struct {
hidden string `validate:"something"`
}{},
wantErr: true,
errString: "`hidden` is not exported",
},
{
name: "multiple rules on one field",
rules: []Rule{
{Name: "minLen", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
s, _ := value.(string)
if len(s) < 2 {
return errors.New("too short")
}
return nil
})},
{Name: "maxLen", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
s, _ := value.(string)
if len(s) > 10 {
return errors.New("too long")
}
return nil
})},
},
input: struct {
Field string `validate:"minLen;maxLen"`
}{Field: "foo"},
wantErr: false,
},
{
name: "first rule fails",
rules: []Rule{
{Name: "minLen", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
return errors.New("minLen failed")
})},
{Name: "maxLen", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error { return nil })},
},
input: struct {
Field string `validate:"minLen;maxLen"`
}{Field: "foo"},
wantErr: true,
},
{
name: "nil pointer to struct",
rules: []Rule{},
input: (*struct{ Name string })(nil),
wantErr: true,
},
{
name: "pointer to struct",
rules: []Rule{
{Name: "nonempty", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
if value.(string) == "" {
return errors.New("empty")
}
return nil
})},
},
input: &struct {
Tag string `validate:"nonempty"`
}{Tag: "ok"},
wantErr: false,
},
{
name: "non-struct input",
rules: []Rule{},
input: 42,
wantErr: true,
},
{
name: "required with zero value but handler returns nil",
rules: []Rule{
{Name: "check", Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error { return nil })},
},
input: struct {
ID int `validate:"required;check"`
}{ID: 0},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := New()
if err := v.Register(tt.rules...); err != nil {
t.Fatalf("Register() error = %v", err)
}
err := v.ValidateStruct(ctx, tt.input)
if (err != nil) != tt.wantErr {
t.Fatalf("ValidateStruct() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr && tt.errString != "" && err.Error() != tt.errString {
t.Errorf("expected error %q, got %q", tt.errString, err.Error())
}
})
}
}