-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_test.go
More file actions
246 lines (229 loc) · 6.74 KB
/
Copy pathstruct_test.go
File metadata and controls
246 lines (229 loc) · 6.74 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
package validator
import (
"context"
"reflect"
"testing"
)
func TestStructValidate(t *testing.T) {
type User struct {
Email string `validate:"required && email"`
Age int `validate:"required && gte:18"`
Bio string `validate:"-"`
Name string // no tag
}
v := MustNew()
t.Run("valid", func(t *testing.T) {
vd := v.MustStruct(User{Email: "a@b.com", Age: 20})
vd.Validate(context.Background())
if vd.Fails() {
t.Errorf("valid user should pass: %v", vd.Errors().All())
}
})
t.Run("invalid collects field errors", func(t *testing.T) {
vd := v.MustStruct(User{Email: "nope", Age: 10})
vd.Validate(context.Background())
if !vd.Fails() {
t.Fatal("invalid user should fail")
}
if !vd.Errors().Has("Email") || !vd.Errors().Has("Age") {
t.Errorf("expected Email and Age errors, got %v", vd.Errors().All())
}
})
t.Run("pointer to struct", func(t *testing.T) {
vd := v.MustStruct(&User{Email: "a@b.com", Age: 20})
vd.Validate(context.Background())
if vd.Fails() {
t.Errorf("pointer struct should validate: %v", vd.Errors().All())
}
})
}
// TestStructPlanCached verifies the per-type plan is built once and reused.
func TestStructPlanCached(t *testing.T) {
type T struct {
A string `validate:"required"`
}
v := MustNew()
p1 := v.getStructPlan(reflect.TypeFor[T]())
p2 := v.getStructPlan(reflect.TypeFor[T]())
if p1 != p2 {
t.Error("struct plan should be cached and identical across calls")
}
}
// A validate tag on an embedded struct field is a value rule under the type
// name, like a named struct field (a nil embedded pointer must fail required).
func TestEmbeddedFieldTagHonored(t *testing.T) {
type Config struct {
Host string
}
type App struct {
*Config `validate:"required"`
}
v := MustNew()
vd := v.MustStruct(App{})
vd.Validate(context.Background())
if !vd.Errors().Has("Config") {
t.Errorf("required on a nil embedded pointer must fail, got %v", vd.Errors().All())
}
ok := v.MustStruct(App{Config: &Config{Host: "h"}})
ok.Validate(context.Background())
if ok.Fails() {
t.Errorf("present embedded pointer should pass, got %v", ok.Errors().All())
}
}
// Untagged struct-typed fields stay resolvable by name: cross-field rules and
// AddRules see them, matching the Map source on identical logical data.
func TestUntaggedStructFieldResolvable(t *testing.T) {
type Card struct {
Number string
}
type Payment struct {
Method *string `validate:"required_with:Card"`
Card *Card
}
v := MustNew()
vd := v.MustStruct(Payment{Card: &Card{Number: "4111"}})
vd.Validate(context.Background())
if !vd.Errors().Has("Method") {
t.Errorf("required_with must see the untagged struct field Card, got %v", vd.Errors().All())
}
type Holder struct {
Card *Card
}
hv := v.MustStruct(Holder{Card: &Card{Number: "1"}})
if err := hv.AddRules("Card", "required"); err != nil {
t.Fatal(err)
}
hv.Validate(context.Background())
if hv.Fails() {
t.Errorf("AddRules(required) on a present struct field must pass, got %v", hv.Errors().All())
}
}
// Struct types with no reachable subfields (named time types and the like) are
// plan leaves: bindable, resolvable, and rule-addressable.
func TestOpaqueStructFieldIsLeaf(t *testing.T) {
type MyTime struct{ sec int64 }
type Rec struct {
At MyTime
}
v := MustNew()
vd := v.MustStruct(Rec{At: MyTime{sec: 5}})
if err := vd.AddRules("At", "required"); err != nil {
t.Fatal(err)
}
vd.Validate(context.Background())
if vd.Fails() {
t.Errorf("required on a present opaque struct must pass, got %v", vd.Errors().All())
}
src := Rec{At: MyTime{sec: 7}}
var dst Rec
if err := v.MustStruct(src).Bind(&dst); err != nil {
t.Fatal(err)
}
if dst.At != src.At {
t.Errorf("Bind must not drop an opaque struct field: got %+v want %+v", dst.At, src.At)
}
}
// A recursion-cut self-referential field is still a bindable leaf.
func TestRecursiveStructFieldBindable(t *testing.T) {
type Node struct {
Val string
Next *Node
}
v := MustNew()
src := Node{Val: "a", Next: &Node{Val: "b"}}
var dst Node
if err := v.MustStruct(src).Bind(&dst); err != nil {
t.Fatal(err)
}
if dst.Next == nil || dst.Next.Val != "b" {
t.Errorf("Bind must whole-assign a recursion-cut field, got %+v", dst.Next)
}
}
// A shadowed embedded subtree leaves no ghost entries: rules on Go-unreachable
// paths never run and Bind never writes into the shadowed field.
func TestShadowedSubtreeNoGhosts(t *testing.T) {
type hiddenX struct {
OnlyA string `validate:"required"`
}
type base struct {
X hiddenX
}
type Outer struct {
base
X string `validate:"required"`
}
v := MustNew()
vd := v.MustStruct(Outer{X: "visible"})
vd.Validate(context.Background())
if vd.Errors().Has("X.OnlyA") {
t.Errorf("rules on a shadowed subtree path must not run, got %v", vd.Errors().All())
}
if vd.Fails() {
t.Errorf("only the visible X is validated, got %v", vd.Errors().All())
}
}
// prune must not read through its own compaction: a dropped (ambiguous) entry
// ordered before a nested struct's children must not shift what isGhost reads,
// silently dropping legitimate nested fields.
func TestPruneKeepsNestedFieldsAfterDrop(t *testing.T) {
type embA2 struct {
X string `validate:"notblank"`
}
type embB2 struct {
X string `validate:"numeric"`
}
type inner2 struct {
N1 string `validate:"notblank"`
N2 string `validate:"notblank"`
}
type top struct {
embA2
embB2 // X is ambiguous -> dead entry BEFORE the nested children
Outer inner2
}
v := MustNew()
vd := v.MustStruct(top{})
vd.Validate(context.Background())
if !vd.Errors().Has("Outer.N1") || !vd.Errors().Has("Outer.N2") {
t.Errorf("both nested fields must survive prune and validate, got %v", vd.Errors().All())
}
src := top{Outer: inner2{N1: "a", N2: "b"}}
var dst top
if err := v.MustStruct(src).Bind(&dst); err != nil {
t.Fatal(err)
}
if dst.Outer.N1 != "a" || dst.Outer.N2 != "b" {
t.Errorf("Bind must not lose nested fields after a prune drop, got %+v", dst.Outer)
}
}
// Equal-depth same-name promotions are ambiguous, matching Go: neither field's
// rules run under the promoted name.
func TestAmbiguousPromotionDropped(t *testing.T) {
type embA struct {
Code string `validate:"notblank"`
}
type embB struct {
Code string `validate:"numeric"`
}
type Both struct {
embA
embB
}
v := MustNew()
vd := v.MustStruct(Both{})
vd.Validate(context.Background())
if vd.Errors().Has("Code") {
t.Errorf("an ambiguous promoted name must validate under neither rule set, got %v", vd.Errors().All())
}
// a shallower unambiguous field still wins over deeper ambiguity
type Shallow struct {
embA
embB
Code string `validate:"notblank"`
}
sv := v.MustStruct(Shallow{})
sv.Validate(context.Background())
if !sv.Errors().Has("Code") {
t.Errorf("the shallow unambiguous Code must validate, got %v", sv.Errors().All())
}
}