-
Notifications
You must be signed in to change notification settings - Fork 7
/
string.go
402 lines (363 loc) · 9.24 KB
/
string.go
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package zog
import (
"net/url"
"regexp"
"strings"
"github.com/Oudwins/zog/conf"
p "github.com/Oudwins/zog/internals"
"github.com/Oudwins/zog/zconst"
)
var (
emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
uuidRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$`)
)
type stringProcessor struct {
preTransforms []p.PreTransform
tests []p.Test
postTransforms []p.PostTransform
defaultVal *string
required *p.Test
catch *string
coercer conf.CoercerFunc
}
// ! INTERNALS
// Internal function to process the data
func (v *stringProcessor) process(val any, dest any, path p.PathBuilder, ctx ParseCtx) {
primitiveProcessor(val, dest, path, ctx, v.preTransforms, v.tests, v.postTransforms, v.defaultVal, v.required, v.catch, v.coercer, p.IsParseZeroValue)
}
// Returns the type of the schema
func (v *stringProcessor) getType() zconst.ZogType {
return zconst.TypeString
}
// Sets the coercer for the schema
func (v *stringProcessor) setCoercer(c conf.CoercerFunc) {
v.coercer = c
}
// ! USER FACING FUNCTIONS
// Returns a new String Schema
func String(opts ...SchemaOption) *stringProcessor {
s := &stringProcessor{
coercer: conf.Coercers.String, // default coercer
}
for _, opt := range opts {
opt(s)
}
return s
}
// Parses the data into the destination string. Returns a list of errors
func (v *stringProcessor) Parse(data any, dest *string, options ...ParsingOption) p.ZogErrList {
errs := p.NewErrsList()
ctx := p.NewParseCtx(errs, conf.ErrorFormatter)
for _, opt := range options {
opt(ctx)
}
path := p.PathBuilder("")
v.process(data, dest, path, ctx)
return errs.List
}
// Adds pretransform function to schema
func (v *stringProcessor) PreTransform(transform p.PreTransform) *stringProcessor {
if v.preTransforms == nil {
v.preTransforms = []p.PreTransform{}
}
v.preTransforms = append(v.preTransforms, transform)
return v
}
// PreTransform: trims the input data of whitespace if it is a string
func (v *stringProcessor) Trim() *stringProcessor {
v.preTransforms = append(v.preTransforms, func(val any, ctx ParseCtx) (any, error) {
s, ok := val.(string)
if !ok {
return val, nil
}
return strings.TrimSpace(s), nil
})
return v
}
// Adds posttransform function to schema
func (v *stringProcessor) PostTransform(transform p.PostTransform) *stringProcessor {
if v.postTransforms == nil {
v.postTransforms = []p.PostTransform{}
}
v.postTransforms = append(v.postTransforms, transform)
return v
}
// ! MODIFIERS
// marks field as required
func (v *stringProcessor) Required(options ...TestOption) *stringProcessor {
r := p.Required()
for _, opt := range options {
opt(&r)
}
v.required = &r
return v
}
// marks field as optional
func (v *stringProcessor) Optional() *stringProcessor {
v.required = nil
return v
}
// sets the default value
func (v *stringProcessor) Default(val string) *stringProcessor {
v.defaultVal = &val
return v
}
// sets the catch value (i.e the value to use if the validation fails)
func (v *stringProcessor) Catch(val string) *stringProcessor {
v.catch = &val
return v
}
// ! PRETRANSFORMS
// ! Tests
// custom test function call it -> schema.Test(t z.Test, opts ...TestOption)
func (v *stringProcessor) Test(t p.Test, opts ...TestOption) *stringProcessor {
for _, opt := range opts {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value is one of the enum values
func (v *stringProcessor) OneOf(enum []string, options ...TestOption) *stringProcessor {
t := p.In(enum)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value is at least n characters long
func (v *stringProcessor) Min(n int, options ...TestOption) *stringProcessor {
t := p.LenMin[string](n)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value is at most n characters long
func (v *stringProcessor) Max(n int, options ...TestOption) *stringProcessor {
t := p.LenMax[string](n)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value is exactly n characters long
func (v *stringProcessor) Len(n int, options ...TestOption) *stringProcessor {
t := p.Len[string](n)
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value is a valid email address
func (v *stringProcessor) Email(options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeEmail,
ValidateFunc: func(v any, ctx ParseCtx) bool {
email, ok := v.(string)
if !ok {
return false
}
return emailRegex.MatchString(email)
},
}
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value is a valid URL
func (v *stringProcessor) URL(options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeURL,
ValidateFunc: func(v any, ctx ParseCtx) bool {
s, ok := v.(string)
if !ok {
return false
}
u, err := url.Parse(s)
return err == nil && u.Scheme != "" && u.Host != ""
},
}
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value has the prefix
func (v *stringProcessor) HasPrefix(s string, options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeHasPrefix,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
if !ok {
return false
}
return strings.HasPrefix(val, s)
},
}
t.Params[zconst.ErrCodeHasPrefix] = s
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value has the suffix
func (v *stringProcessor) HasSuffix(s string, options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeHasSuffix,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
if !ok {
return false
}
return strings.HasSuffix(val, s)
},
}
t.Params[zconst.ErrCodeHasSuffix] = s
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value contains the substring
func (v *stringProcessor) Contains(sub string, options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeContains,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
if !ok {
return false
}
return strings.Contains(val, sub)
},
}
t.Params[zconst.ErrCodeContains] = sub
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value contains an uppercase letter
func (v *stringProcessor) ContainsUpper(options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeContainsUpper,
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
if !ok {
return false
}
for _, r := range val {
if r >= 'A' && r <= 'Z' {
return true
}
}
return false
},
}
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value contains a digit
func (v *stringProcessor) ContainsDigit(options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeContainsDigit,
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
if !ok {
return false
}
for _, r := range val {
if r >= '0' && r <= '9' {
return true
}
}
return false
},
}
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value contains a special character
func (v *stringProcessor) ContainsSpecial(options ...TestOption) *stringProcessor {
t :=
p.Test{
ErrCode: zconst.ErrCodeContainsSpecial,
ValidateFunc: func(v any, ctx ParseCtx) bool {
val, ok := v.(string)
if !ok {
return false
}
for _, r := range val {
if (r >= '!' && r <= '/') ||
(r >= ':' && r <= '@') ||
(r >= '[' && r <= '`') ||
(r >= '{' && r <= '~') {
return true
}
}
return false
},
}
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that the value is a valid uuid
func (v *stringProcessor) UUID(options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeUUID,
ValidateFunc: func(v any, ctx ParseCtx) bool {
uuid, ok := v.(string)
if !ok {
return false
}
return uuidRegex.MatchString(uuid)
},
}
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}
// Test: checks that value matches to regex
func (v *stringProcessor) Match(regex *regexp.Regexp, options ...TestOption) *stringProcessor {
t := p.Test{
ErrCode: zconst.ErrCodeMatch,
Params: make(map[string]any, 1),
ValidateFunc: func(v any, ctx ParseCtx) bool {
s, ok := v.(string)
if !ok {
return false
}
return regex.MatchString(s)
},
}
t.Params[zconst.ErrCodeMatch] = regex.String()
for _, opt := range options {
opt(&t)
}
v.tests = append(v.tests, t)
return v
}