forked from cohesivestack/valgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_string_p.go
More file actions
519 lines (459 loc) · 17.9 KB
/
Copy pathvalidator_string_p.go
File metadata and controls
519 lines (459 loc) · 17.9 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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package valgo
import (
"regexp"
)
// The String pointer validator type that keeps its validator context.
type ValidatorStringP[T ~string] struct {
context *ValidatorContext
}
// Receives a string pointer to validate.
//
// The value also can be a custom boolean type such as `type Status *string;`
//
// Optionally, the function can receive a name and title, in that order,
// to be used in the error messages. A `value_%N`` pattern is used as a name in
// error messages if a name and title are not supplied; for example: value_0.
// When the name is provided but not the title, then the name is humanized to be
// used as the title as well; for example the name `phone_number` will be
// humanized as `Phone Number`
func StringP[T ~string](value *T, nameAndTitle ...string) *ValidatorStringP[T] {
return &ValidatorStringP[T]{context: NewContext(value, nameAndTitle...)}
}
// Return the context of the validator. The context is useful to create a custom
// validator by extending this validator.
func (validator *ValidatorStringP[T]) Context() *ValidatorContext {
return validator.context
}
// Invert the logical value associated to the next validator function.
// For example:
//
// // It will return false because Not() inverts the boolean value associated with the Blank() function
// status := ""
// Is(v.StringP(&status).Not().Blank()).Valid()
func (validator *ValidatorStringP[T]) Not() *ValidatorStringP[T] {
validator.context.Not()
return validator
}
// Or introduces a logical OR boundary in the current validator chain.
//
// Or groups adjacent validation fragments into a single OR-group that is
// evaluated left-to-right until one fragment succeeds. The OR-group succeeds
// if any fragment succeeds; it fails only if all fragments fail.
//
// Precedence: the OR-group is evaluated as a unit before the implicit AND
// that continues the chain. For example:
//
// A.Or().B.C == (A OR B) AND C
//
// Error reporting: if the OR-group fails, the error message for that group is
// a single message composed by joining the failing fragments' messages using
// the localized OR list format.
//
// Example:
//
// // Passes because input equals "test" (MinLength(5) OR EqualTo("test")).
// input := "test"
// isValid := v.Is(v.StringP(&input).MinLength(5).Or().EqualTo("test")).Valid()
func (validator *ValidatorStringP[T]) Or() *ValidatorStringP[T] {
validator.context.Or()
return validator
}
// OrElse introduces a logical OR boundary with a cut (short-circuit) in the
// validator chain.
//
// OrElse behaves like Or for building an OR-group, but with an additional rule:
// if the left side (a single fragment, or the entire OR-group accumulated to
// the left of OrElse) succeeds, validation stops and no fragments to the right
// of OrElse are evaluated.
//
// This is primarily used to express "accept X, otherwise validate the rest"
// without repeating X across multiple OR fragments.
//
// Precedence: OrElse still participates in OR-grouping precedence. For example:
//
// A.OrElse().B.C == A OR (B AND C) (with a cut if A succeeds)
//
// Error reporting: if the OR-group fails, its message is composed the same way
// as Or (localized OR list join).
//
// Example:
//
// // If input is empty, the chain succeeds and MinLength/EqualTo are not evaluated.
// // Otherwise, input must have MinLength(5) AND EqualTo("test").
// input := ""
// isValid := v.Is(v.StringP(&input).Empty().OrElse().MinLength(5).EqualTo("test")).Valid()
func (validator *ValidatorStringP[T]) OrElse() *ValidatorStringP[T] {
validator.context.OrElse()
return validator
}
// Validate if the value of a string pointer is equal to a another value.
// For example:
//
// status := "running"
// Is(v.StringP(&status).Equal("running"))
func (validator *ValidatorStringP[T]) EqualTo(value T, template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) != nil && isStringEqualTo(*(validator.context.Value().(*T)), value)
},
ErrorKeyEqualTo, value, template...)
return validator
}
// Validate if a string value is greater than another. This function internally
// uses the golang `>` operator.
// For example:
//
// section := "bb"
// Is(v.StringP(§ion).GreaterThan("ba"))
func (validator *ValidatorStringP[T]) GreaterThan(value T, template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) != nil && isStringGreaterThan(*(validator.context.Value().(*T)), value)
},
ErrorKeyGreaterThan, value, template...)
return validator
}
// Validate if a string value is greater than or equal to another. This function
// internally uses the golang `>=` operator.
// For example:
//
// section := "bc"
// Is(v.StringP(§ion).GreaterOrEqualTo("bc"))
func (validator *ValidatorStringP[T]) GreaterOrEqualTo(value T, template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) != nil && isStringGreaterOrEqualTo(*(validator.context.Value().(*T)), value)
},
ErrorKeyGreaterOrEqualTo, value, template...)
return validator
}
// Validate if a string value is less than another. This function internally
// uses the golang `<` operator.
// For example:
//
// section := "bb"
// Is(v.StringP(§ion).LessThan("bc"))
func (validator *ValidatorStringP[T]) LessThan(value T, template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) != nil && isStringLessThan(*(validator.context.Value().(*T)), value)
},
ErrorKeyLessThan, value, template...)
return validator
}
// Validate if a string value is less or equal to another. This function
// internally uses the golang `<=` operator to compare two strings.
// For example:
//
// section := "bc"
// Is(v.StringP(§ion).LessOrEqualTo("bc"))
func (validator *ValidatorStringP[T]) LessOrEqualTo(value T, template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) != nil && isStringLessOrEqualTo(*(validator.context.Value().(*T)), value)
},
ErrorKeyLessOrEqualTo, value, template...)
return validator
}
// Validate if a string value is empty. Empty will be false if the length
// of the string is greater than zero, even if the string has only spaces.
// For checking if the string has only spaces, uses the function `Blank()`
// instead.
// For example:
//
// status := ""
// Is(v.StringP(&status).Empty()) // Will be true
// status = " "
// Is(v.StringP(&status).Empty()) // Will be false
func (validator *ValidatorStringP[T]) Empty(template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) != nil && isStringEmpty(*(validator.context.Value().(*T)))
},
ErrorKeyEmpty, validator.context.Value(), template...)
return validator
}
// Validate if a string value is empty or nil. Empty will be false if the length
// of the string is greater than zero, even if the string has only spaces.
// For checking if the string has only spaces, uses the function `BlankOrNil()`
// instead.
// For example:
//
// status := ""
// Is(v.StringP(&status).EmptyOrNil()) // Will be true
// status = " "
// Is(v.StringP(&status).EmptyOrNil()) // Will be false
// var _status *string
// Is(v.StringP(_status).EmptyOrNil()) // Will be true
func (validator *ValidatorStringP[T]) EmptyOrNil(template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) == nil || isStringEmpty(*(validator.context.Value().(*T)))
},
ErrorKeyEmpty, validator.context.Value(), template...)
return validator
}
// Validate if a string value is blank. Blank will be true if the length
// of the string is zero or if the string only has spaces.
// For example:
//
// status := ""
// Is(v.StringP(&status).Blank()) // Will be true
// status = " "
// Is(v.StringP(&status).Blank()) // Will be true
func (validator *ValidatorStringP[T]) Blank(template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) != nil && isStringBlank(*(validator.context.Value().(*T)))
},
ErrorKeyBlank, validator.context.Value(), template...)
return validator
}
// Validate if a string value is blank or nil. Blank will be true if the length
// of the string is zero or if the string only has spaces.
// For example:
//
// status := ""
// Is(v.StringP(&status).BlankOrNil()) // Will be true
// status = " "
// Is(v.StringP(&status).BlankOrNil()) // Will be true
// var _status *string
// Is(v.StringP(_status).BlankOrNil()) // Will be true
func (validator *ValidatorStringP[T]) BlankOrNil(template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) == nil || isStringBlank(*(validator.context.Value().(*T)))
},
ErrorKeyBlank, validator.context.Value(), template...)
return validator
}
// Validate if a string pointer pass a custom function.
// For example:
//
// status := ""
// Is(v.StringP(&status).Passing((v string) bool {
// return v == getNewStatus()
// })
func (validator *ValidatorStringP[T]) Passing(function func(v0 *T) bool, template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return function(validator.context.Value().(*T))
},
ErrorKeyPassing, validator.context.Value(), template...)
return validator
}
// Validate if the value of a string pointer is present in a string slice.
// For example:
//
// status := "idle"
// validStatus := []string{"idle", "paused", "stopped"}
// Is(v.StringP(&status).InSlice(validStatus))
func (validator *ValidatorStringP[T]) InSlice(slice []T, template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) != nil && isStringInSlice(*(validator.context.Value().(*T)), slice)
},
ErrorKeyInSlice, validator.context.Value(), template...)
return validator
}
// Validate if the value of a string pointer match a regular expression.
// For example:
//
// status := "pre-approved"
// regex, _ := regexp.Compile("pre-.+")
// Is(v.StringP(&status).MatchingTo(regex))
func (validator *ValidatorStringP[T]) MatchingTo(regex *regexp.Regexp, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringMatchingTo(*(validator.context.Value().(*T)), regex)
},
ErrorKeyMatchingTo,
map[string]any{"title": validator.context.title, "regexp": regex, "value": validator.context.Value()},
template...)
return validator
}
// Validate the maximum length (in bytes) of a string pointer's value.
// For example:
//
// slug := "myname"
// Is(v.StringP(&slug).MaxBytes(6))
//
// For character count, use `MaxLength` instead.
func (validator *ValidatorStringP[T]) MaxBytes(length int, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringByteMaxLength(*(validator.context.Value().(*T)), length)
},
ErrorKeyMaxLength,
map[string]any{"title": validator.context.title, "length": length, "value": validator.context.Value()},
template...)
return validator
}
// Validate the minimum length (in bytes) of a string pointer's value.
// For example:
//
// slug := "myname"
// Is(v.StringP(&slug).MinBytes(6))
//
// For character count, use `MinLength` instead.
func (validator *ValidatorStringP[T]) MinBytes(length int, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringByteMinLength(*(validator.context.Value().(*T)), length)
},
ErrorKeyMinLength,
map[string]any{"title": validator.context.title, "length": length, "value": validator.context.Value()},
template...)
return validator
}
// Validate the length (in bytes) of a string pointer's value.
// For example:
//
// slug := "myname"
// Is(v.StringP(&slug).ByteLength(6))
//
// For character count, use `Length` instead.
func (validator *ValidatorStringP[T]) ByteLength(length int, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringByteLength(*(validator.context.Value().(*T)), length)
},
ErrorKeyLength,
map[string]any{"title": validator.context.title, "length": length, "value": validator.context.Value()},
template...)
return validator
}
// OfByteLength validates the length (in bytes) of a string pointer's value.
//
// Deprecated: use ByteLength instead. OfByteLength will be removed in v1.0.
func (validator *ValidatorStringP[T]) OfByteLength(length int, template ...string) *ValidatorStringP[T] {
return validator.ByteLength(length, template...)
}
// Validate if the length (in bytes) of a string pointer's value is in a range (inclusive).
// For example:
//
// slug := "myname"
// Is(v.StringP(&slug).ByteLengthBetween(2,6))
//
// For character count, use `LengthBetween` instead.
func (validator *ValidatorStringP[T]) ByteLengthBetween(min int, max int, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringByteLengthBetween(*(validator.context.Value().(*T)), min, max)
},
ErrorKeyLengthBetween,
map[string]any{"title": validator.context.title, "min": min, "max": max, "value": validator.context.Value()},
template...)
return validator
}
// OfByteLengthBetween validates if the length (in bytes) of a string pointer's
// value is in a range (inclusive).
//
// Deprecated: use ByteLengthBetween instead. OfByteLengthBetween will be removed in v1.0.
func (validator *ValidatorStringP[T]) OfByteLengthBetween(min int, max int, template ...string) *ValidatorStringP[T] {
return validator.ByteLengthBetween(min, max, template...)
}
// Validate the maximum length (in runes/characters) of a string pointer's value.
// For example:
//
// slug := "虎視眈々" // 4 runes, len(slug) = 12 bytes
// Is(v.StringP(&slug).MaxLength(4))
func (validator *ValidatorStringP[T]) MaxLength(length int, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringRuneMaxLength(*(validator.context.Value().(*T)), length)
},
ErrorKeyMaxLength,
map[string]any{"title": validator.context.title, "length": length, "value": validator.context.Value()},
template...)
return validator
}
// Validate the minimum length (in runes/characters) of a string pointer's value.
// For example:
//
// slug := "虎視眈々" // 4 runes, len(slug) = 12 bytes
// Is(v.StringP(&slug).MinLength(4))
func (validator *ValidatorStringP[T]) MinLength(length int, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringRuneMinLength(*(validator.context.Value().(*T)), length)
},
ErrorKeyMinLength,
map[string]any{"title": validator.context.title, "length": length, "value": validator.context.Value()},
template...)
return validator
}
// Validate the length (in runes/characters) of a string pointer's value.
// For example:
//
// slug := "虎視眈々" // 4 runes, len(slug) = 12 bytes
// Is(v.StringP(&slug).Length(4))
func (validator *ValidatorStringP[T]) Length(length int, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringRuneLength(*(validator.context.Value().(*T)), length)
},
ErrorKeyLength,
map[string]any{"title": validator.context.title, "length": length, "value": validator.context.Value()},
template...)
return validator
}
// OfLength validates the length (in runes/characters) of a string pointer's value.
//
// Deprecated: use Length instead. OfLength will be removed in v1.0.
func (validator *ValidatorStringP[T]) OfLength(length int, template ...string) *ValidatorStringP[T] {
return validator.Length(length, template...)
}
// Validate if the length (in runes/characters) of a string pointer's value is in a range (inclusive).
// For example:
//
// slug := "虎視眈々" // 4 runes, len(slug) = 12 bytes
// Is(v.StringP(&slug).LengthBetween(2,4))
func (validator *ValidatorStringP[T]) LengthBetween(min int, max int, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
if validator.context.Value().(*T) == nil {
return false
}
return isStringRuneLengthBetween(*(validator.context.Value().(*T)), min, max)
},
ErrorKeyLengthBetween,
map[string]any{"title": validator.context.title, "min": min, "max": max, "value": validator.context.Value()},
template...)
return validator
}
// OfLengthBetween validates if the length (in runes/characters) of a string
// pointer's value is in a range (inclusive).
//
// Deprecated: use LengthBetween instead. OfLengthBetween will be removed in v1.0.
func (validator *ValidatorStringP[T]) OfLengthBetween(min int, max int, template ...string) *ValidatorStringP[T] {
return validator.LengthBetween(min, max, template...)
}
// Validate if the value of a string pointer's value is in a range (inclusive).
// For example:
//
// slug := "ab"
// Is(v.StringP(&slug).Between("ab","ac"))
func (validator *ValidatorStringP[T]) Between(min T, max T, template ...string) *ValidatorStringP[T] {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*T) != nil && isStringBetween(*(validator.context.Value().(*T)), min, max)
},
ErrorKeyBetween,
map[string]any{"title": validator.context.title, "min": min, "max": max, "value": validator.context.Value()},
template...)
return validator
}
// Validate if a string pointer is nil.
// For example:
//
// var status *string
// Is(v.StringP(status).Nil())
func (validator *ValidatorStringP[T]) Nil(template ...string) *ValidatorStringP[T] {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*T) == nil
},
ErrorKeyNil, validator.context.Value(), template...)
return validator
}