-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstring_custom_test.go
More file actions
252 lines (229 loc) · 5.89 KB
/
Copy pathstring_custom_test.go
File metadata and controls
252 lines (229 loc) · 5.89 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
package zog
import (
"regexp"
"testing"
"github.com/Oudwins/zog/conf"
"github.com/Oudwins/zog/pkgs/internals"
"github.com/Oudwins/zog/pkgs/internals/tutils"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
type Env string
const (
A Env = "A"
B Env = "B"
C Env = "C"
)
func MyStringSchema() *StringSchema[Env] {
s := &StringSchema[Env]{}
WithCoercer(func(x any) (any, error) {
v, e := conf.DefaultCoercers.String(x)
if e != nil {
return nil, e
}
return Env(v.(string)), e
})(s)
return s
}
func TestCustomStringBasics(t *testing.T) {
// Test optional by default
s := MyStringSchema()
var data Env
err := s.Parse("", &data)
assert.Nil(t, err)
assert.Equal(t, Env(""), data)
// Test required
s = MyStringSchema().Required().Min(1)
err = s.Parse("", &data)
assert.NotNil(t, err)
assert.Equal(t, zconst.IssueCodeMin, err[0].Code)
// Test default value
s = MyStringSchema().Default(A)
err = s.Parse(nil, &data)
assert.Nil(t, err)
assert.Equal(t, A, data)
// Test catch
s = MyStringSchema().Required().Min(5).Catch(A)
err = s.Parse("x", &data)
assert.Nil(t, err)
assert.Equal(t, A, data)
}
func TestCustomStringTransforms(t *testing.T) {
// Test pre-transform
var data Env = "test"
// Test post-transform
s := MyStringSchema().Transform(func(v *Env, ctx Ctx) error {
*v = Env(string(*v) + "_post")
return nil
})
data = "test"
err := s.Validate(&data)
assert.Nil(t, err)
assert.Equal(t, Env("test_post"), data)
}
func TestCustomStringValidators(t *testing.T) {
var data Env
// Test length
s := MyStringSchema().Len(3)
err := s.Parse("foo", &data)
assert.Nil(t, err)
assert.Equal(t, Env("foo"), data)
// Test min/max
s = MyStringSchema().Min(2).Max(4)
err = s.Parse("foo", &data)
assert.Nil(t, err)
err = s.Parse("toolong", &data)
assert.NotNil(t, err)
// Test contains
s = MyStringSchema().Contains("test")
err = s.Parse("testing", &data)
assert.Nil(t, err)
err = s.Parse("fail", &data)
assert.NotNil(t, err)
assert.Equal(t, zconst.IssueCodeContains, err[0].Code)
// Test prefix/suffix
s = MyStringSchema().HasPrefix("pre").HasSuffix("fix")
err = s.Parse("prefix", &data)
assert.Nil(t, err)
// Test regex
s = MyStringSchema().Match(regexp.MustCompile("^[0-9]+$"))
err = s.Parse("123", &data)
assert.Nil(t, err)
err = s.Validate(&data)
assert.Nil(t, err)
err = s.Parse("abc", &data)
assert.NotNil(t, err)
// Test OneOf
s = MyStringSchema().OneOf([]Env{A, B, C})
err = s.Parse("A", &data)
assert.Nil(t, err)
assert.Equal(t, A, data)
err = s.Parse("D", &data)
assert.NotNil(t, err)
}
func TestCustomStringSpecialValidators(t *testing.T) {
var data Env
// Test email
s := MyStringSchema().Email()
err := s.Parse("test@example.com", &data)
assert.Nil(t, err)
err = s.Parse("invalid", &data)
assert.NotNil(t, err)
// Test URL
s = MyStringSchema().URL()
err = s.Parse("http://example.com", &data)
assert.Nil(t, err)
err = s.Parse("invalid", &data)
assert.NotNil(t, err)
// Test UUID
s = MyStringSchema().UUID()
err = s.Parse("123e4567-e89b-12d3-a456-426614174000", &data)
assert.Nil(t, err)
err = s.Parse("invalid", &data)
assert.NotNil(t, err)
}
func TestCustomStringNot(t *testing.T) {
tests := map[string]struct {
schema *StringSchema[Env]
strVal string
expectedErrMap internals.ZogIssueList
}{
"not len success": {
schema: MyStringSchema().Not().Len(10).Contains("test"),
strVal: "test",
expectedErrMap: nil,
},
"not len fail": {
schema: MyStringSchema().Not().Len(4).Contains("t"),
strVal: "test",
expectedErrMap: internals.ZogIssueList{
&internals.ZogIssue{
Code: "not_len",
Params: map[string]any{"len": 4},
Dtype: "string",
Value: tutils.PtrOf[Env]("test"),
Message: "string must not be exactly 4 character(s)",
Err: nil,
},
},
},
"not email": {
schema: MyStringSchema().Not().Email(),
strVal: "not-an-email",
expectedErrMap: nil,
},
"not email failure": {
schema: MyStringSchema().Not().Email(),
strVal: "test@test.com",
expectedErrMap: internals.ZogIssueList{
&internals.ZogIssue{
Code: "not_email",
Params: nil,
Dtype: "string",
Value: tutils.PtrOf[Env]("test@test.com"),
Message: "must not be a valid email",
Err: nil,
},
},
},
"not with empty": {
schema: MyStringSchema().Not().Len(1),
strVal: "a",
expectedErrMap: internals.ZogIssueList{
&internals.ZogIssue{
Code: "not_len",
Params: map[string]any{"len": 1},
Dtype: "string",
Value: tutils.PtrOf[Env]("a"),
Message: "string must not be exactly 1 character(s)",
Err: nil,
},
},
},
"not url": {
schema: MyStringSchema().Not().URL(),
strVal: "not a url",
expectedErrMap: nil,
},
"not url failure": {
schema: MyStringSchema().Not().URL(),
strVal: "https://google.com",
expectedErrMap: internals.ZogIssueList{
&internals.ZogIssue{
Code: "not_url",
Dtype: "string",
Value: tutils.PtrOf[Env]("https://google.com"),
Message: "must not be a valid URL",
Err: nil,
},
},
},
"not has prefix": {
schema: MyStringSchema().Not().HasPrefix("test_"),
strVal: "value",
expectedErrMap: nil,
},
"not has prefix failure": {
schema: MyStringSchema().Not().HasPrefix("test_"),
strVal: "test_value",
expectedErrMap: internals.ZogIssueList{
&internals.ZogIssue{
Code: "not_prefix",
Params: map[string]any{"prefix": "test_"},
Dtype: "string",
Value: tutils.PtrOf[Env]("test_value"),
Message: "string must not start with test_",
Err: nil,
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var data Env
err := test.schema.Parse(test.strVal, &data)
assert.Equal(t, test.expectedErrMap, err)
assert.Equal(t, Env(test.strVal), data)
})
}
}