-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
416 lines (355 loc) · 10.3 KB
/
functions.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package validator
import (
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"golang.org/x/exp/slices"
)
var validatorFunctions = map[string]ValidationFunction{
"required": IsRequired,
"alphanum": IsAlphaNumeric,
"uuid1": IsUuid1,
"uuid2": IsUuid2,
"uuid3": IsUuid3,
"uuid4": IsUuid4,
"min": IsMin,
"max": IsMax,
"enum": IsEnum,
"email": IsEmail,
"at_least_today": IsOrBeforeToday,
"at_most_today": IsOrAfterToday,
"today": IsToday,
"before_today": IsBeforeToday,
"after_today": IsAfterToday,
}
var emailHostNameMatcher *regexp.Regexp
func init() {
var err error
emailHostNameMatcher, err = regexp.Compile("^[a-zA-Z0-9][a-zA-Z0-9]+$")
if err != nil {
panic(errors.Join(errors.New("package init: regex error"), err))
}
}
func timeValidator(ctx *ValidationContext, comparator Comparator) bool {
var err error
var then time.Time
today := time.Now()
layout := "2006-01-02"
if ctx.IsPointer && ctx.IsNull {
return true
}
if ctx.ArgCount() == 1 {
layout = ctx.Args[0]
}
if ctx.IsValueOfKind(reflect.String) {
then, err = time.Parse(layout, ctx.GetValue().String())
if err != nil {
ctx.AdditionalError = err
ctx.ErrorMessage = "invalid date format. expected format is " + layout
return false
}
} else if ctx.IsValueOfType(&then) {
then = ctx.GetValue().Interface().(time.Time)
} else {
panic(newValidationError("only time.Time and string and their pointer types are supported"))
}
match := false
switch comparator {
case GREATER_THAN:
match = then.After(today)
case GREATER_THAN_OR_EQUAL:
match = then.After(today) || then.Equal(today)
case LESS_THAN:
match = then.Before(today)
case LESS_THAN_OR_EQUAL:
match = then.Before(today) || then.Equal(today)
case NOT_EQUAL:
match = !then.Equal(today)
}
if !match {
ctx.ErrorMessage = fmt.Sprintf(
"%s must be %s %s",
then.Format(layout),
comparator.TemporalDescription(),
today.Format(layout),
)
}
return match
}
// IsBeforeToday tests whether the given date is today or before today.
//
// If the time layout is not specified, '2006-01-02' will be used
func IsOrBeforeToday(ctx *ValidationContext) bool {
return timeValidator(ctx, LESS_THAN_OR_EQUAL)
}
// IsOrAfterToday tests whether the given date is today or after today.
//
// If the time layout is not specified, '2006-01-02' will be used
func IsOrAfterToday(ctx *ValidationContext) bool {
return timeValidator(ctx, GREATER_THAN_OR_EQUAL)
}
// IsBeforeToday tests whether the given date is before today.
//
// If the time layout is not specified, '2006-01-02' will be used
func IsBeforeToday(ctx *ValidationContext) bool {
return timeValidator(ctx, LESS_THAN)
}
// IsAfterToday tests whether the given date is after today.
//
// If the time layout is not specified, '2006-01-02' will be used
func IsAfterToday(ctx *ValidationContext) bool {
return timeValidator(ctx, GREATER_THAN)
}
// IsToday tests whether the given date is today.
//
// If the time layout is not specified, '2006-01-02' will be used
func IsToday(ctx *ValidationContext) bool {
return timeValidator(ctx, EQUALS)
}
// IsNotToday tests whether the given date is not today.
//
// If the time layout is not specified, '2006-01-02' will be used
func IsNotToday(ctx *ValidationContext) bool {
return timeValidator(ctx, NOT_EQUAL)
}
// IsEmail tests if the input value matches an email format.
//
// The validation rules used here do not conform to RFC and only allow only a few latin character set values.
// Therefore this function could be considered as very strict.
func IsEmail(ctx *ValidationContext) bool {
ctx.ValueMustBeOfKind(reflect.String)
if ctx.IsNull {
return true
}
email := ctx.GetValue().String()
parts := strings.Split(email, "@")
if len(parts) != 2 {
return false
}
user := parts[0]
host := parts[1]
// last.first@sub.main.tld
usernamePattern := "^[a-zA-Z0-9][a-zA-Z0-9_.]+$"
m, err := regexp.MatchString(usernamePattern, user)
if err != nil {
panic(newValidationError("email: user part regex error", err))
}
if !m {
return false
}
parts = strings.Split(host, ".")
for _, domain := range parts {
m := emailHostNameMatcher.MatchString(domain)
if err != nil {
panic(newValidationError("email: host part regex error", err))
}
if !m {
return false
}
}
return true
}
// IsEnum tests if the input value matches any of the values passed in the arguments
func IsEnum(ctx *ValidationContext) bool {
if ctx.IsNull {
return true
}
match := false
if ctx.ArgCount() == 0 {
panic(newValidationError("enum: At least one enum value must be specified"))
}
if ctx.IsValueOfKind(reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64) {
value := strconv.FormatInt(ctx.GetValue().Int(), 10)
match = slices.Contains(ctx.Args, value)
} else if ctx.IsValueOfKind(reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64) {
value := strconv.FormatUint(ctx.GetValue().Uint(), 10)
match = slices.Contains(ctx.Args, value)
} else if ctx.IsValueOfKind(reflect.String) {
match = slices.Contains(ctx.Args, ctx.GetValue().String())
} else {
panic(newValidationError("enum: unsupported type " + ctx.valueKind.String()))
}
if !match {
ctx.ErrorMessage = "invalid value specified"
if ctx.Options.ExposeEnumValues {
ctx.ErrorMessage += ". expected any of " + strings.Join(ctx.Args, ",")
}
}
return match
}
// IsMin tests if the given input (string, integer, list) contains at least the given number of elements
func IsMin(ctx *ValidationContext) bool {
ctx.ValueMustBeOfKind(
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.String,
)
if ctx.ArgCount() == 0 {
panic(newValidationError("min: expected length or size parameter"))
}
if ctx.IsNull {
return true
}
match := false
propertyName := "value"
var expected int64 = ctx.MustGetIntArg(0)
if ctx.IsValueOfKind(reflect.String) {
actual := len(ctx.GetValue().String())
match = int64(actual) >= expected
propertyName = "length"
} else if ctx.IsValueOfKind(reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64) {
actual := ctx.GetValue().Int()
match = actual >= expected
} else if ctx.IsValueOfKind(reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64) {
expected := ctx.MustGetUintArg(0)
actual := ctx.GetValue().Uint()
match = actual >= expected
}
if !match {
ctx.ErrorMessage = fmt.Sprintf("%s (%v) must be at least %v", propertyName, ctx.GetValue(), ctx.Args[0])
}
return match
}
// IsMax tests if the given input (string, integer, list) contains at least the given number of elements
func IsMax(ctx *ValidationContext) bool {
ctx.ValueMustBeOfKind(
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.String,
)
if ctx.ArgCount() == 0 {
panic(newValidationError("max: expected length or size parameter"))
}
if ctx.IsNull {
return true
}
match := false
propertyName := "value"
var expected int64 = ctx.MustGetIntArg(0)
if ctx.IsValueOfKind(reflect.String) {
actual := len(ctx.GetValue().String())
match = int64(actual) <= expected
propertyName = "length"
} else if ctx.IsValueOfKind(reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64) {
actual := ctx.GetValue().Int()
match = actual <= expected
} else if ctx.IsValueOfKind(reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64) {
expected := ctx.MustGetUintArg(0)
actual := ctx.GetValue().Uint()
match = actual <= expected
}
if !match {
ctx.ErrorMessage = fmt.Sprintf("%s (%v) must not exceed %v", propertyName, ctx.GetValue(), ctx.Args[0])
}
return match
}
// IsAlphaNumeric verifies that the given string is alphanumeric
func IsAlphaNumeric(ctx *ValidationContext) bool {
ctx.ValueMustBeOfKind(reflect.String)
if ctx.IsNull {
return true
}
alphaNumPattern := "^[a-z0-9]+$"
m, err := regexp.MatchString(alphaNumPattern, ctx.GetValue().String())
if err != nil {
panic(newValidationError("regex error when validating input", err))
}
if !m {
ctx.ErrorMessage = "must be alphanumeric"
}
return m
}
// IsRequired check if the required field has values.
//
// For literal values, the function always returns true because the values are present and can subsequnetly
// be validated appropriately.
//
// For pointer types, the function will return false if the pointer is null or true if the pointer is not null
func IsRequired(ctx *ValidationContext) bool {
if ctx.IsNull {
ctx.ErrorMessage = "this field is requiredd"
return false
}
return true
}
func uuidFn(ctx *ValidationContext, version int) bool {
ctx.ValueMustBeOfKind(reflect.String)
if ctx.IsNull {
return false
}
id, err := uuid.Parse(ctx.GetValue().String())
if err != nil {
ctx.ErrorMessage = "invalid uuid format"
return false
}
match := id.Version() == uuid.Version(version)
if !match {
ctx.ErrorMessage = fmt.Sprintf("expectedd UUIDv%d but found UUIDv%d", version, int(id.Version()))
}
return match
}
func IsUuid1(ctx *ValidationContext) bool {
return uuidFn(ctx, 1)
}
func IsUuid2(ctx *ValidationContext) bool {
return uuidFn(ctx, 2)
}
func IsUuid3(ctx *ValidationContext) bool {
return uuidFn(ctx, 3)
}
func IsUuid4(ctx *ValidationContext) bool {
return uuidFn(ctx, 4)
}
var filterFunctions = map[string]FilterFunction{
"trim": Trim,
"null_if_empty": NullIfEmpty,
}
func Trim(ctx *ValidationContext) reflect.Value {
ctx.ValueMustBeOfKind(reflect.String)
if ctx.IsPointer && !ctx.IsNull {
value := ctx.GetValue().String()
trimmed := strings.TrimSpace(value)
return reflect.ValueOf(&trimmed)
} else {
if ctx.IsNull {
return ctx.value
}
return reflect.ValueOf(strings.TrimSpace(ctx.GetValue().String()))
}
}
// NullIfEmpty Sets the given string pointer's value to null if the string is empty
func NullIfEmpty(ctx *ValidationContext) reflect.Value {
ctx.ValueMustBeOfKind(reflect.String)
if !ctx.IsPointer {
panic(newValidationError("null_if_empty: filter only works with string pointers"))
}
if !ctx.IsNull {
value := ctx.GetValue().String()
if len(value) == 0 {
var nullString *string = nil
return reflect.ValueOf(nullString)
}
}
return ctx.GetValue()
}