-
Notifications
You must be signed in to change notification settings - Fork 8
/
field.go
577 lines (528 loc) · 13.9 KB
/
field.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package crud
import (
"fmt"
"reflect"
"regexp"
"slices"
"strings"
"time"
)
// Field allows specification of swagger or json schema types using the builder pattern.
type Field struct {
kind string
format string
obj map[string]Field
max *float64
min *float64
pattern *regexp.Regexp
required *bool
example interface{}
description string
enum enum
_default interface{}
arr *Field
allow enum
strip *bool
unknown *bool
}
func (f Field) String() string {
var str strings.Builder
str.WriteString(fmt.Sprintf("{Field: '%v'", f.kind))
if f.required != nil {
str.WriteString(" required")
}
str.WriteString("}")
return str.String()
}
// Initialized returns true if the field has been initialized with Number, String, etc.
// When the Swagger is being built, often an uninitialized field will be ignored.
func (f Field) Initialized() bool {
return f.kind != ""
}
// Kind returns the kind of the field.
func (f Field) Kind() string {
return f.kind
}
type enum []interface{}
func (e enum) has(needle interface{}) bool {
for _, value := range e {
if value == needle {
return true
}
}
return false
}
var (
errRequired = fmt.Errorf("value is required")
errWrongType = fmt.Errorf("wrong type passed")
errMaximum = fmt.Errorf("maximum exceeded")
errMinimum = fmt.Errorf("minimum exceeded")
errEnumNotFound = fmt.Errorf("value not in enum")
errUnknown = fmt.Errorf("unknown value")
errPattern = fmt.Errorf("value does not match pattern")
)
// Validate is used in the validation middleware to tell if the value passed
// into the controller meets the restrictions set on the field.
func (f *Field) Validate(value interface{}) error {
if value == nil && f.required != nil && *f.required {
return errRequired
}
if value == nil {
return nil
}
switch v := value.(type) {
case int:
if f.kind != KindInteger {
return errWrongType
}
if f.max != nil && float64(v) > *f.max {
return errMaximum
}
if f.min != nil && float64(v) < *f.min {
return errMinimum
}
case float64:
if f.kind == KindInteger {
// since JSON is unmarshalled as float64 always
if float64(int(v)) != v {
return errWrongType
}
} else if f.kind != KindNumber {
return errWrongType
}
if f.max != nil && v > *f.max {
return errMaximum
}
if f.min != nil && v < *f.min {
return errMinimum
}
case string:
if f.kind != KindString {
return errWrongType
}
if f.required != nil && *f.required && v == "" && !f.allow.has("") {
return errRequired
}
if f.max != nil && len(v) > int(*f.max) {
return errMaximum
}
if f.min != nil && len(v) < int(*f.min) {
return errMinimum
}
if f.pattern != nil && !f.pattern.MatchString(v) {
return errPattern
}
switch f.format {
case FormatDateTime:
_, err := time.Parse(time.RFC3339, v)
if err != nil {
return err
}
case FormatDate:
_, err := time.Parse(fullDate, v)
if err != nil {
return err
}
}
case bool:
if f.kind != KindBoolean {
return errWrongType
}
case []interface{}:
if f.kind != KindArray {
return errWrongType
}
if f.min != nil && float64(len(v)) < *f.min {
return errMinimum
}
if f.max != nil && float64(len(v)) > *f.max {
return errMaximum
}
if f.arr != nil {
// child fields inherit parent's settings, unless specified on child
if f.arr.strip == nil {
f.arr.strip = f.strip
}
if f.arr.unknown == nil {
f.arr.unknown = f.unknown
}
for _, item := range v {
if err := f.arr.Validate(item); err != nil {
return err
}
}
}
case map[string]interface{}:
if f.kind != KindObject {
return errWrongType
}
return validateObject("", f, v)
default:
return fmt.Errorf("unhandled type %v", v)
}
if f.enum != nil && !f.enum.has(value) {
return errEnumNotFound
}
return nil
}
// validateObject is a recursive function that validates the field values in the object. It also
// performs stripping of values, or erroring when unexpected fields are present, depending on the
// options on the fields.
func validateObject(name string, field *Field, input interface{}) error {
switch v := input.(type) {
case nil:
if field.required != nil && *field.required {
return fmt.Errorf("object validation failed for field %v: %w", name, errRequired)
}
case string, bool:
if err := field.Validate(v); err != nil {
return fmt.Errorf("object validation failed for field %v: %w", name, err)
}
case float64:
if field.kind == KindInteger {
// JSON doesn't have integers, so Go treats these fields as float64.
// Need to convert to integer before validating it.
if v != float64(int64(v)) {
return fmt.Errorf("object validation failed for field %v: %w", name, errWrongType)
}
if err := field.Validate(int(v)); err != nil {
return fmt.Errorf("object validation failed for field %v: %w", name, err)
}
} else {
if err := field.Validate(v); err != nil {
return fmt.Errorf("object validation failed for field %v: %w", name, err)
}
}
case []interface{}:
if err := field.Validate(v); err != nil {
return fmt.Errorf("object validation failed for field %v: %w", name, err)
}
if field.arr != nil {
for i, item := range v {
if err := validateObject(fmt.Sprintf("%v[%v]", name, i), field.arr, item); err != nil {
return err
}
}
}
case map[string]interface{}:
if !field.isAllowUnknown() {
for key := range v {
if _, ok := field.obj[key]; !ok {
return fmt.Errorf("unknown field in object: %v %w", key, errUnknown)
}
}
}
if field.isStripUnknown() {
for key := range v {
if _, ok := field.obj[key]; !ok {
delete(v, key)
}
}
}
for childName, childField := range field.obj {
// child fields inherit parent's settings, unless specified on child
if childField.strip == nil {
childField.strip = field.strip
}
if childField.unknown == nil {
childField.unknown = field.unknown
}
newV := v[childName]
if newV == nil && childField.required != nil && *childField.required {
return fmt.Errorf("object validation failed for field %v.%v: %w", name, childName, errRequired)
} else if newV == nil && childField._default != nil {
v[childName] = childField._default
} else if err := validateObject(name+"."+childName, &childField, v[childName]); err != nil {
return err
}
}
default:
return fmt.Errorf("object validation failed for type %v: %w", reflect.TypeOf(v), errWrongType)
}
return nil
}
// These kinds correlate to swagger and json types.
const (
KindNumber = "number"
KindString = "string"
KindBoolean = "boolean"
KindObject = "object"
KindArray = "array"
KindFile = "file"
KindInteger = "integer"
)
// Number creates a field with floating point type
func Number() Field {
return Field{kind: KindNumber}
}
// String creates a field with string type
func String() Field {
return Field{kind: KindString}
}
// DateTime creates a field with dateTime type
func DateTime() Field {
return Field{kind: KindString, format: FormatDateTime}
}
// https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14
const fullDate = "2006-01-02"
// Date creates a field with date type
func Date() Field {
return Field{kind: KindString, format: FormatDate}
}
// Boolean creates a field with boolean type
func Boolean() Field {
return Field{kind: KindBoolean}
}
// Object creates a field with object type
func Object(obj map[string]Field) Field {
return Field{kind: KindObject, obj: obj}
}
// Array creates a field with array type
func Array() Field {
return Field{kind: KindArray}
}
// File creates a field with file type
func File() Field {
return Field{kind: KindFile}
}
// Integer creates a field with integer type
func Integer() Field {
return Field{kind: KindInteger}
}
// Min specifies a minimum value for this field
func (f Field) Min(min float64) Field {
f.min = &min
if f.max != nil && *f.max < min {
panic("min cannot be larger than max")
}
return f
}
// Max specifies a maximum value for this field
func (f Field) Max(max float64) Field {
f.max = &max
if f.min != nil && *f.min > max {
panic("min cannot be larger than max")
}
return f
}
// Pattern specifies a regex pattern value for this field
func (f Field) Pattern(pattern string) Field {
f.pattern = regexp.MustCompile(pattern)
return f
}
// Required specifies the field must be provided. Can't be used with Default.
func (f Field) Required() Field {
if f._default != nil {
panic("required and default cannot be used together")
}
required := true
f.required = &required
return f
}
// Example specifies an example value for the swagger to display
func (f Field) Example(ex interface{}) Field {
f.example = ex
return f
}
// Description specifies a human-readable explanation of the field
func (f Field) Description(description string) Field {
f.description = description
return f
}
// Default specifies a default value to use if the field is nil. Can't be used with Required.
func (f Field) Default(value interface{}) Field {
if f.required != nil && *f.required {
panic("default and required cannot be used together")
}
switch value.(type) {
case int:
if f.kind != KindInteger {
panic("wrong type passed default")
}
case float64:
if f.kind != KindNumber {
panic("wrong type passed default")
}
case string:
if f.kind != KindString {
panic("wrong type passed default")
}
case bool:
if f.kind != KindBoolean {
panic("wrong type passed default")
}
default:
panic("default must be an int, float64, bool or string")
}
f._default = value
return f
}
// Enum restricts the field's values to the set of values specified
func (f Field) Enum(values ...interface{}) Field {
if f.kind == KindArray || f.kind == KindObject || f.kind == KindFile {
panic("Enum cannot be used on arrays, objects, files")
}
f.enum = values
return f
}
// Items specifies the type of elements in an array
func (f Field) Items(item Field) Field {
if f.kind != KindArray {
panic("Items can only be used with array types")
}
f.arr = &item
return f
}
const (
FormatDate = "date"
FormatDateTime = "dateTime"
)
// Format is used to set custom format types. Note that formats with special
// validation in this library also have their own constructor. See DateTime for example.
func (f Field) Format(format string) Field {
f.format = format
return f
}
// Allow lets you break rules
// For example, String().Required() excludes "", unless you Allow("")
func (f Field) Allow(values ...interface{}) Field {
f.allow = append(f.allow, values...)
return f
}
// Strip overrides the global "strip unknown" setting just for this field, and all children of this field
func (f Field) Strip(strip bool) Field {
f.strip = &strip
return f
}
// Unknown overrides the global "allow unknown" setting just for this field, and all children of this field
func (f Field) Unknown(allow bool) Field {
f.unknown = &allow
return f
}
// ToSwaggerParameters transforms a field into a slice of Parameter.
func (f *Field) ToSwaggerParameters(in string) (parameters []Parameter) {
switch f.kind {
case KindArray:
p := Parameter{
In: in,
Type: f.kind,
CollectionFormat: "multi",
Required: f.required,
Description: f.description,
Default: f._default,
}
if f.arr != nil {
items := f.arr.ToJsonSchema()
p.Items = &items
}
parameters = append(parameters, p)
case KindObject:
for name, field := range f.obj {
param := Parameter{
In: in,
Name: name,
Type: field.kind,
Required: field.required,
Description: field.description,
Default: field._default,
Enum: field.enum,
Minimum: field.min,
Maximum: field.max,
}
if field.pattern != nil {
param.Pattern = field.pattern.String()
}
if field.kind == KindArray {
if field.arr != nil {
temp := field.arr.ToJsonSchema()
param.Items = &temp
}
param.CollectionFormat = "multi"
}
parameters = append(parameters, param)
}
slices.SortFunc(parameters, func(a, b Parameter) int {
if a.Name < b.Name {
return -1
}
if a.Name > b.Name {
return 1
}
return 0
})
}
return
}
// ToJsonSchema transforms a field into a Swagger Schema.
// TODO this is an extension of JsonSchema, rename in v2 ToSchema() Schema
func (f *Field) ToJsonSchema() JsonSchema {
schema := JsonSchema{
Type: f.kind,
}
switch f.kind {
case KindArray:
if f.arr != nil {
items := f.arr.ToJsonSchema()
schema.Items = &items
}
case KindObject:
populateProperties(f.obj, &schema)
}
return schema
}
// recursively fill in the schema
func populateProperties(obj map[string]Field, schema *JsonSchema) {
schema.Properties = map[string]JsonSchema{}
for name, field := range obj {
prop := JsonSchema{
Type: field.kind,
Format: field.format,
Example: field.example,
Description: field.description,
Default: field._default,
}
if field.example == nil {
if field.kind == KindString {
switch field.format {
case FormatDateTime:
prop.Example = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local).Format(time.RFC3339)
case FormatDate:
prop.Example = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local).Format(fullDate)
}
}
}
if field.required != nil && *field.required {
schema.Required = append(schema.Required, name)
}
if field.min != nil {
prop.Minimum = *field.min
}
if field.max != nil {
prop.Maximum = *field.max
}
if field.pattern != nil {
prop.Pattern = field.pattern.String()
}
if prop.Type == KindArray {
if field.arr != nil {
items := field.arr.ToJsonSchema()
prop.Items = &items
}
} else if prop.Type == KindObject {
populateProperties(field.obj, &prop)
}
schema.Properties[name] = prop
}
}
func (f Field) isAllowUnknown() bool {
if f.unknown == nil {
return true // by default allow unknown
}
return *f.unknown
}
func (f Field) isStripUnknown() bool {
if f.strip == nil {
return true // by default strip
}
return *f.strip
}