-
Notifications
You must be signed in to change notification settings - Fork 4
/
schema.go
93 lines (85 loc) · 2.44 KB
/
schema.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
package openapi
import (
"fmt"
"strings"
)
// codebeat:disable[TOO_MANY_IVARS]
// Schema Object
type Schema struct {
Title string
MultipleOf int `yaml:"multipleOf"`
Maximum int
ExclusiveMaximum bool `yaml:"exclusiveMaximum"`
Minimum int
ExclusiveMinimum bool `yaml:"exclusiveMinimum"`
MaxLength int `yaml:"maxLength"`
MinLength int `yaml:"minLength"`
Pattern string
MaxItems int `yaml:"maxItems"`
MinItems int `yaml:"minItems"`
MaxProperties int `yaml:"maxProperties"`
MinProperties int `yaml:"minProperties"`
Required []string
Enum []string
Type string
AllOf []*Schema `yaml:"allOf"`
OneOf []*Schema `yaml:"oneOf"`
AnyOf []*Schema `yaml:"anyOf"`
Not *Schema
Items *Schema
Properties map[string]*Schema
AdditionalProperties *Schema `yaml:"additionalProperties"`
Description string
Format string
Default interface{}
Nullable bool
Discriminator *Discriminator
ReadOnly bool `yaml:"readOnly"`
WriteOnly bool `yaml:"writeOnly"`
XML *XML
ExternalDocs *ExternalDocumentation `yaml:"externalDocs"`
Example interface{}
Deprecated bool
Ref string `yaml:"$ref"`
Extension map[string]interface{} `yaml:",inline"`
}
// Validate the values of Schema object.
func (schema Schema) Validate() error {
validaters := []validater{}
for _, s := range schema.AllOf {
validaters = append(validaters, s)
}
for _, s := range schema.OneOf {
validaters = append(validaters, s)
}
for _, s := range schema.AnyOf {
validaters = append(validaters, s)
}
if schema.Not != nil {
validaters = append(validaters, schema.Not)
}
if schema.Items != nil {
validaters = append(validaters, schema.Items)
}
if schema.Discriminator != nil {
validaters = append(validaters, schema.Discriminator)
}
if schema.XML != nil {
validaters = append(validaters, schema.XML)
}
if schema.ExternalDocs != nil {
validaters = append(validaters, schema.ExternalDocs)
}
for _, property := range schema.Properties {
validaters = append(validaters, property)
}
if e, ok := schema.Example.(validater); ok {
validaters = append(validaters, e)
}
for k := range schema.Extension {
if !strings.HasPrefix(k, "x-") {
return fmt.Errorf("unknown field: %s", k)
}
}
return validateAll(validaters)
}