-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
schema_type.go
280 lines (245 loc) · 9.73 KB
/
schema_type.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
// Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley // https://pb33f.io
package openapi
import (
"fmt"
"github.com/daveshanley/vacuum/model"
vacuumUtils "github.com/daveshanley/vacuum/utils"
"github.com/dop251/goja"
"github.com/pb33f/doctor/model/high/base"
"gopkg.in/yaml.v3"
"strings"
)
// SchemaTypeCheck will determine if document schemas contain the correct type
type SchemaTypeCheck struct {
}
// GetSchema returns a model.RuleFunctionSchema defining the schema of the DuplicatedEnum rule.
func (st SchemaTypeCheck) GetSchema() model.RuleFunctionSchema {
return model.RuleFunctionSchema{
Name: "schemaTypeCheck",
}
}
// GetCategory returns the category of the DuplicatedEnum rule.
func (st SchemaTypeCheck) GetCategory() string {
return model.FunctionCategoryOpenAPI
}
// RunRule will execute the DuplicatedEnum rule, based on supplied context and a supplied []*yaml.Node slice.
func (st SchemaTypeCheck) RunRule(_ []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult {
if context.DrDocument == nil {
return nil
}
var results []model.RuleFunctionResult
schemas := context.DrDocument.Schemas
for _, schema := range schemas {
schemaType := schema.Value.Type
for _, t := range schemaType {
switch t {
case "string":
errs := st.validateString(schema, &context)
results = append(results, errs...)
case "integer", "number":
errs := st.validateNumber(schema, &context)
results = append(results, errs...)
case "boolean":
break
case "array":
errs := st.validateArray(schema, &context)
results = append(results, errs...)
case "object":
errs := st.validateObject(schema, &context)
results = append(results, errs...)
case "null":
default:
result := model.RuleFunctionResult{
Message: fmt.Sprintf("unknown schema type: `%s`", t),
StartNode: schema.Value.GoLow().Type.KeyNode,
EndNode: vacuumUtils.BuildEndNode(schema.Value.GoLow().Type.KeyNode),
Path: fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "type"),
Rule: context.Rule,
}
schema.AddRuleFunctionResult(base.ConvertRuleResult(&result))
results = append(results, result)
}
}
}
return results
}
func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.RuleFunctionContext) []model.RuleFunctionResult {
var results []model.RuleFunctionResult
if schema.Value.MultipleOf != nil {
if *schema.Value.MultipleOf <= 0 {
result := st.buildResult("`multipleOf` should be a number greater than `0`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "multipleOf"),
schema, schema.Value.GoLow().MultipleOf.KeyNode, context)
results = append(results, result)
}
}
if schema.Value.Maximum != nil {
if schema.Value.Minimum != nil {
if *schema.Value.Maximum < *schema.Value.Minimum {
result := st.buildResult("`maximum` should be a number greater than or equal to `minimum`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maximum"),
schema, schema.Value.GoLow().Maximum.KeyNode, context)
results = append(results, result)
}
}
}
if schema.Value.ExclusiveMaximum != nil {
if schema.Value.ExclusiveMinimum != nil {
if schema.Value.ExclusiveMinimum.IsB() && schema.Value.ExclusiveMinimum.B > schema.Value.ExclusiveMaximum.B {
result := st.buildResult("`exclusiveMaximum` should be greater than or equal to `exclusiveMinimum`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "exclusiveMaximum"),
schema, schema.Value.GoLow().ExclusiveMaximum.KeyNode, context)
results = append(results, result)
}
}
}
return results
}
func (st SchemaTypeCheck) validateString(schema *base.Schema, context *model.RuleFunctionContext) []model.RuleFunctionResult {
var results []model.RuleFunctionResult
if schema.Value.MinLength != nil {
if *schema.Value.MinLength < 0 {
result := st.buildResult("`minLength` should be a non-negative number",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "minLength"),
schema, schema.Value.GoLow().MinLength.KeyNode, context)
results = append(results, result)
}
}
if schema.Value.MaxLength != nil {
if *schema.Value.MaxLength < 0 {
result := st.buildResult("`maxLength` should be a non-negative number",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maxLength"),
schema, schema.Value.GoLow().MaxLength.KeyNode, context)
results = append(results, result)
}
if schema.Value.MinLength != nil {
if *schema.Value.MinLength > *schema.Value.MaxLength {
result := st.buildResult("`maxLength` should be greater than or equal to `minLength`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maxLength"),
schema, schema.Value.GoLow().MaxLength.KeyNode, context)
results = append(results, result)
}
}
}
if schema.Value.Pattern != "" {
vm := goja.New()
script := strings.Replace("const regex = new RegExp('{pattern}');", "{pattern}", schema.Value.Pattern, 1)
_, err := vm.RunString(script)
if err != nil {
result := st.buildResult("schema `pattern` should be a ECMA-262 regular expression dialect",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "pattern"),
schema, schema.Value.GoLow().Pattern.KeyNode, context)
results = append(results, result)
}
}
return results
}
func (st SchemaTypeCheck) validateArray(schema *base.Schema, context *model.RuleFunctionContext) []model.RuleFunctionResult {
var results []model.RuleFunctionResult
if schema.Value.MinItems != nil {
if *schema.Value.MinItems < 0 {
result := st.buildResult("`minItems` should be a non-negative number",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "minItems"),
schema, schema.Value.GoLow().MinItems.KeyNode, context)
results = append(results, result)
}
}
if schema.Value.MaxItems != nil {
if *schema.Value.MaxItems < 0 {
result := st.buildResult("`maxItems` should be a non-negative number",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maxItems"),
schema, schema.Value.GoLow().MaxItems.KeyNode, context)
results = append(results, result)
}
if schema.Value.MinItems != nil {
if *schema.Value.MinItems > *schema.Value.MaxItems {
result := st.buildResult("`maxItems` should be greater than or equal to `minItems`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maxItems"),
schema, schema.Value.GoLow().MaxItems.KeyNode, context)
results = append(results, result)
}
}
}
if schema.Value.MinContains != nil {
if *schema.Value.MinContains < 0 {
result := st.buildResult("`minContains` should be a non-negative number",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "minContains"),
schema, schema.Value.GoLow().MinContains.KeyNode, context)
results = append(results, result)
}
}
if schema.Value.MaxContains != nil {
if *schema.Value.MaxContains < 0 {
result := st.buildResult("`maxContains` should be a non-negative number",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maxContains"),
schema, schema.Value.GoLow().MaxContains.KeyNode, context)
results = append(results, result)
}
if schema.Value.MinContains != nil {
if *schema.Value.MinContains > *schema.Value.MaxContains {
result := st.buildResult("`maxContains` should be greater than or equal to `minContains`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maxContains"),
schema, schema.Value.GoLow().MaxContains.KeyNode, context)
results = append(results, result)
}
}
}
return results
}
func (st SchemaTypeCheck) buildResult(message, path string, schema *base.Schema, node *yaml.Node, context *model.RuleFunctionContext) model.RuleFunctionResult {
result := model.RuleFunctionResult{
Message: message,
StartNode: node,
EndNode: vacuumUtils.BuildEndNode(node),
Path: path,
Rule: context.Rule,
}
schema.AddRuleFunctionResult(base.ConvertRuleResult(&result))
return result
}
func (st SchemaTypeCheck) validateObject(schema *base.Schema, context *model.RuleFunctionContext) []model.RuleFunctionResult {
var results []model.RuleFunctionResult
if schema.Value.MinProperties != nil {
if *schema.Value.MinProperties < 0 {
result := st.buildResult("`minProperties` should be a non-negative number",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "minProperties"),
schema, schema.Value.GoLow().MinProperties.KeyNode, context)
results = append(results, result)
}
}
if schema.Value.MaxProperties != nil {
if *schema.Value.MaxProperties < 0 {
result := st.buildResult("`maxProperties` should be a non-negative number",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maxProperties"),
schema, schema.Value.GoLow().MaxProperties.KeyNode, context)
results = append(results, result)
}
if schema.Value.MinProperties != nil {
if *schema.Value.MinProperties > *schema.Value.MaxProperties {
result := st.buildResult("`maxProperties` should be greater than or equal to `minProperties`",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maxProperties"),
schema, schema.Value.GoLow().MaxProperties.KeyNode, context)
results = append(results, result)
}
}
}
if len(schema.Value.Required) > 0 {
for i, required := range schema.Value.Required {
if schema.Value.Properties == nil {
result := st.buildResult("object contains `required` fields but no `properties`",
fmt.Sprintf("%s.%s[%d]", schema.GenerateJSONPath(), "required", i),
schema, schema.Value.GoLow().Required.KeyNode, context)
results = append(results, result)
break
}
if schema.Value.Properties.GetOrZero(required) == nil {
result := st.buildResult(fmt.Sprintf("`required` field `%s` is not defined in `properties`", required),
fmt.Sprintf("%s.%s[%d]", schema.GenerateJSONPath(), "required", i),
schema, schema.Value.GoLow().Required.KeyNode, context)
results = append(results, result)
}
}
}
// TODO: DependentRequired
return results
}