-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrequired_variants_test.go
More file actions
194 lines (175 loc) · 6.32 KB
/
Copy pathrequired_variants_test.go
File metadata and controls
194 lines (175 loc) · 6.32 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
package validator
import (
"testing"
)
// Test struct for RequiredWith validation
type RequiredWithTest struct {
Field1 string `valid:"requiredWith=Field2"`
Field2 string
}
// Test RequiredWith validation
func TestRequiredWithValidation(t *testing.T) {
tests := []struct {
name string
data RequiredWithTest
expected bool
}{
{"Both fields present - valid", RequiredWithTest{Field1: "value1", Field2: "value2"}, true},
{"Field2 present, Field1 missing - invalid", RequiredWithTest{Field2: "value2"}, false},
{"Field2 absent, Field1 absent - valid", RequiredWithTest{}, true},
{"Field2 absent, Field1 present - valid", RequiredWithTest{Field1: "value1"}, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ValidateStruct(test.data)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected %t for %s, got %t. Error: %v", test.expected, test.name, actual, err)
}
})
}
}
// Test struct for RequiredWithAll validation
type RequiredWithAllTest struct {
Field1 string `valid:"requiredWithAll=Field2|Field3"`
Field2 string
Field3 string
}
// Test RequiredWithAll validation
func TestRequiredWithAllValidation(t *testing.T) {
tests := []struct {
name string
data RequiredWithAllTest
expected bool
}{
{"All fields present - valid", RequiredWithAllTest{Field1: "v1", Field2: "v2", Field3: "v3"}, true},
{"Field2 and Field3 present, Field1 missing - invalid", RequiredWithAllTest{Field2: "v2", Field3: "v3"}, false},
{"Only Field2 present - valid", RequiredWithAllTest{Field2: "v2"}, true},
{"Only Field3 present - valid", RequiredWithAllTest{Field3: "v3"}, true},
{"No fields present - valid", RequiredWithAllTest{}, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ValidateStruct(test.data)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected %t for %s, got %t. Error: %v", test.expected, test.name, actual, err)
}
})
}
}
// Test struct for RequiredWithout validation
type RequiredWithoutTest struct {
Field1 string `valid:"requiredWithout=Field2"`
Field2 string
}
// Test RequiredWithout validation
func TestRequiredWithoutValidation(t *testing.T) {
tests := []struct {
name string
data RequiredWithoutTest
expected bool
}{
{"Field2 absent, Field1 present - valid", RequiredWithoutTest{Field1: "value1"}, true},
{"Field2 absent, Field1 absent - invalid", RequiredWithoutTest{}, false},
{"Field2 present, Field1 present - valid", RequiredWithoutTest{Field1: "v1", Field2: "v2"}, true},
{"Field2 present, Field1 absent - valid", RequiredWithoutTest{Field2: "v2"}, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ValidateStruct(test.data)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected %t for %s, got %t. Error: %v", test.expected, test.name, actual, err)
}
})
}
}
// Test struct for RequiredWithoutAll validation
type RequiredWithoutAllTest struct {
Field1 string `valid:"requiredWithoutAll=Field2|Field3"`
Field2 string
Field3 string
}
// Test RequiredWithoutAll validation
func TestRequiredWithoutAllValidation(t *testing.T) {
tests := []struct {
name string
data RequiredWithoutAllTest
expected bool
}{
{"All fields absent, Field1 present - valid", RequiredWithoutAllTest{Field1: "v1"}, true},
{"All fields absent, Field1 absent - invalid", RequiredWithoutAllTest{}, false},
{"Field2 present - valid", RequiredWithoutAllTest{Field2: "v2"}, true},
{"Field3 present - valid", RequiredWithoutAllTest{Field3: "v3"}, true},
{"Both Field2 and Field3 present - valid", RequiredWithoutAllTest{Field2: "v2", Field3: "v3"}, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ValidateStruct(test.data)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected %t for %s, got %t. Error: %v", test.expected, test.name, actual, err)
}
})
}
}
// Test struct for RequiredUnless validation
type RequiredUnlessTest struct {
Field1 string `valid:"requiredUnless=Field2|exempt"`
Field2 string
}
// Test RequiredUnless validation
func TestRequiredUnlessValidation(t *testing.T) {
tests := []struct {
name string
data RequiredUnlessTest
expected bool
}{
{"Field2 is exempt, Field1 absent - valid", RequiredUnlessTest{Field2: "exempt"}, true},
{"Field2 is exempt, Field1 present - valid", RequiredUnlessTest{Field1: "v1", Field2: "exempt"}, true},
{"Field2 not exempt, Field1 present - valid", RequiredUnlessTest{Field1: "v1", Field2: "other"}, true},
{"Field2 not exempt, Field1 absent - invalid", RequiredUnlessTest{Field2: "other"}, false},
{"Field2 absent, Field1 present - valid", RequiredUnlessTest{Field1: "v1"}, true},
{"Field2 absent, Field1 absent - invalid", RequiredUnlessTest{}, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ValidateStruct(test.data)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected %t for %s, got %t. Error: %v", test.expected, test.name, actual, err)
}
})
}
}
// Test struct for RequiredIf with multiple conditions
type RequiredIfAdvancedTest struct {
Field1 string `valid:"requiredIf=Field2|active"`
Field2 string
Field3 string
}
// Test RequiredIf with multiple conditions
func TestRequiredIfAdvancedValidation(t *testing.T) {
tests := []struct {
name string
data RequiredIfAdvancedTest
expected bool
}{
{"Condition met, field present - valid", RequiredIfAdvancedTest{Field1: "v1", Field2: "active"}, true},
{"Condition met, field absent - invalid", RequiredIfAdvancedTest{Field2: "active"}, false},
{"Condition not met, field absent - valid", RequiredIfAdvancedTest{Field2: "inactive"}, true},
{"Condition not met, field present - valid", RequiredIfAdvancedTest{Field1: "v1", Field2: "inactive"}, true},
{"Multiple conditions - one met", RequiredIfAdvancedTest{Field1: "v1", Field2: "active", Field3: "disabled"}, true},
{"Multiple conditions - both met", RequiredIfAdvancedTest{Field1: "v1", Field2: "active", Field3: "enabled"}, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := ValidateStruct(test.data)
actual := err == nil
if actual != test.expected {
t.Errorf("Expected %t for %s, got %t. Error: %v", test.expected, test.name, actual, err)
}
})
}
}