-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
374 lines (325 loc) · 12 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
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
package openapi
import (
"fmt"
"regexp"
"slices"
"strings"
"github.com/MarkRosemaker/errpath"
"github.com/go-json-experiment/json/jsontext"
)
// The Schema Object allows the definition of input and output data types.
// These types can be objects, but also primitives and arrays. This object is a superset of the JSON Schema Specification Draft 2020-12.
//
// For more information about the properties, see JSON Schema Core and JSON Schema Validation.
//
// Unless stated otherwise, the property definitions follow those of JSON Schema and do not add any additional semantics.
// Where JSON Schema indicates that behavior is defined by the application (e.g. for annotations), OAS also defers the definition of semantics to the application consuming the OpenAPI document.
//
// ([Specification])
//
// [Specification]: https://spec.openapis.org/oas/v3.1.0#schema-object
type Schema struct {
// The name of the schema.
Title string `json:"title,omitempty" yaml:"title,omitempty"`
// A short description of the schema.
// CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Specifies the data type of the property.
Type DataType `json:"type,omitempty" yaml:"type,omitempty"`
// Further refines the data type.
Format Format `json:"format,omitempty" yaml:"format,omitempty"`
// AllOf takes an array of object definitions that are validated independently but together compose a single object.
AllOf SchemaRefList `json:"allOf,omitempty" yaml:"allOf,omitempty"`
// Integer / Number
// The minimum value of the number.
Min *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"`
// The maximum value of the number.
Max *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"`
// String
// The pattern is used to validate the string.
// This string SHOULD be a valid regular expression, according to the Ecma-262 Edition 5.1 regular expression dialect.
// NOTE: We simply use text unmarshalling for this field. This guarantees that the regular expression is valid or we can't unmarshal.
Pattern *regexp.Regexp `json:"pattern,omitempty" yaml:"pattern,omitempty"`
// A list of possible values.
Enum []string `json:"enum,omitempty" yaml:"enum,omitempty"`
// Array
// The minimum number of items in the array.
MinItems uint `json:"minItems,omitzero" yaml:"minItems,omitempty"`
// The maximum number of items in the array.
MaxItems *uint `json:"maxItems,omitempty" yaml:"maxItems,omitempty"`
// The items of the array. When the type is array, this property is REQUIRED.
// The empty schema for `items` indicates a media type of `application/octet-stream`.
Items *SchemaRef `json:"items,omitzero" yaml:"items,omitempty"`
// Object
// For object types, defines the properties of the object
Properties SchemaRefs `json:"properties,omitzero" yaml:"properties,omitempty"`
// Which properties are required.
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
AdditionalProperties *SchemaRef `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
// special encoding for binary data
ContentMediaType string `json:"contentMediaType,omitempty" yaml:"contentMediaType,omitempty"`
ContentEncoding string `json:"contentEncoding,omitempty" yaml:"contentEncoding,omitempty"`
// Specifies the default value of the property if no value is provided.
Default any `json:"default,omitempty" yaml:"default,omitempty"`
Example jsontext.Value `json:"example,omitempty" yaml:"example,omitempty"`
// This object MAY be extended with Specification Extensions.
Extensions Extensions `json:",inline" yaml:"-"`
// an index to the original location of this object
idx int
}
func getIndexSchema(s *Schema) int { return s.idx }
func setIndexSchema(s *Schema, idx int) *Schema { s.idx = idx; return s }
func (s *Schema) Validate() error {
s.Description = strings.TrimSpace(s.Description)
if s.Type == "" {
if len(s.AllOf) == 0 {
return &errpath.ErrField{Field: "type", Err: &errpath.ErrRequired{}}
}
} else if err := s.Type.Validate(); err != nil {
return &errpath.ErrField{Field: "type", Err: err}
}
if s.Format != "" {
if err := s.Format.Validate(); err != nil {
return &errpath.ErrField{Field: "format", Err: err}
}
}
// validate if format is valid for type
switch s.Format {
case "": // no format
case FormatInt32, FormatInt64:
if s.Type != TypeInteger {
return &errpath.ErrField{Field: "format", Err: &errpath.ErrInvalid[Format]{
Value: s.Format,
Message: fmt.Sprintf("only valid for integer type, got %s", s.Type),
}}
}
case FormatFloat, FormatDouble:
if s.Type != TypeNumber {
return &errpath.ErrField{Field: "format", Err: &errpath.ErrInvalid[Format]{
Value: s.Format,
Message: fmt.Sprintf("only valid for number type, got %s", s.Type),
}}
}
case FormatDateTime, FormatEmail, FormatPassword,
FormatUUID, FormatURI, FormatURIRef, FormatZipCode,
FormatIPv4, FormatIPv6:
if s.Type != TypeString {
return &errpath.ErrField{Field: "format", Err: &errpath.ErrInvalid[Format]{
Value: s.Format,
Message: fmt.Sprintf("only valid for string type, got %s", s.Type),
}}
}
case FormatDuration:
switch s.Type {
case TypeInteger, TypeString:
default:
return &errpath.ErrField{Field: "format", Err: &errpath.ErrInvalid[Format]{
Value: s.Format,
Message: fmt.Sprintf("only valid for integer or string type, got %s", s.Type),
}}
}
default:
return fmt.Errorf("unimplemented format: %s", s.Format)
}
for i, v := range s.AllOf {
if err := v.Validate(); err != nil {
return &errpath.ErrField{
Field: "allOf",
Err: &errpath.ErrIndex{Index: i, Err: err},
}
}
}
// Integer / Number
// validate min and max
if s.Type == TypeInteger {
if s.Min != nil && *s.Min != float64(int(*s.Min)) {
return &errpath.ErrField{Field: "minimum", Err: &errpath.ErrInvalid[float64]{
Value: *s.Min,
Message: "not an integer",
}}
}
if s.Max != nil && *s.Max != float64(int(*s.Max)) {
return &errpath.ErrField{Field: "maximum", Err: &errpath.ErrInvalid[float64]{
Value: *s.Max,
Message: "not an integer",
}}
}
}
if s.Type == TypeNumber || s.Type == TypeInteger {
if s.Min != nil && s.Max != nil && *s.Min > *s.Max {
return &errpath.ErrField{Field: "minimum", Err: &errpath.ErrInvalid[float64]{
Value: *s.Min,
Message: fmt.Sprintf("minimum is greater than maximum (%v > %v)", *s.Min, *s.Max),
}}
}
} else if s.Min != nil {
return &errpath.ErrField{Field: "minimum", Err: &errpath.ErrInvalid[float64]{
Value: *s.Min,
Message: fmt.Sprintf("only valid for number type, got %s", s.Type),
}}
} else if s.Max != nil {
return &errpath.ErrField{Field: "maximum", Err: &errpath.ErrInvalid[float64]{
Value: *s.Max,
Message: fmt.Sprintf("only valid for number type, got %s", s.Type),
}}
}
// String
if s.Type != TypeString && s.Enum != nil {
return &errpath.ErrField{Field: "enum", Err: &errpath.ErrInvalid[string]{
Message: fmt.Sprintf("only valid for string type, got %s", s.Type),
}}
}
// Array
// validate min and max items
if s.Type == TypeArray {
if s.MaxItems != nil && s.MinItems > *s.MaxItems {
return &errpath.ErrField{Field: "minItems", Err: &errpath.ErrInvalid[uint]{
Value: s.MinItems,
Message: fmt.Sprintf("minItems is greater than maxItems (%d > %d)", s.MinItems, *s.MaxItems),
}}
}
if s.Items == nil {
return &errpath.ErrField{Field: "items", Err: &errpath.ErrRequired{}}
}
// empty schema for items indicates a media type of application/octet-stream.
if !s.Items.Value.isEmpty() {
if err := s.Items.Validate(); err != nil {
return &errpath.ErrField{Field: "items", Err: err}
}
}
} else if s.MinItems != 0 {
return &errpath.ErrField{Field: "minItems", Err: &errpath.ErrInvalid[uint]{
Value: s.MinItems,
Message: fmt.Sprintf("only valid for array type, got %s", s.Type),
}}
} else if s.MaxItems != nil {
return &errpath.ErrField{Field: "maxItems", Err: &errpath.ErrInvalid[uint]{
Value: *s.MaxItems,
Message: fmt.Sprintf("only valid for array type, got %s", s.Type),
}}
} else if s.Items != nil {
return &errpath.ErrField{Field: "items", Err: &errpath.ErrInvalid[string]{
Message: fmt.Sprintf("only valid for array type, got %s", s.Type),
}}
}
// Object
if s.Type == TypeObject {
if err := s.Properties.Validate(); err != nil {
return &errpath.ErrField{Field: "properties", Err: err}
}
for i, r := range s.Required {
if _, ok := s.Properties[r]; ok {
continue
}
return &errpath.ErrField{
Field: "required",
Err: &errpath.ErrIndex{Index: i, Err: &errpath.ErrInvalid[string]{
Value: r,
Message: "property does not exist",
}},
}
}
if s.AdditionalProperties != nil {
if err := s.AdditionalProperties.Validate(); err != nil {
return &errpath.ErrField{Field: "additionalProperties", Err: err}
}
}
} else if s.Properties != nil {
return &errpath.ErrField{Field: "properties", Err: &errpath.ErrInvalid[string]{
Message: fmt.Sprintf("only valid for object type, got %s", s.Type),
}}
} else if s.AdditionalProperties != nil {
return &errpath.ErrField{Field: "additionalProperties", Err: &errpath.ErrInvalid[string]{
Message: fmt.Sprintf("only valid for object type, got %s", s.Type),
}}
}
// validate default
switch dflt := s.Default.(type) {
case nil: // empty
case string:
if s.Type != TypeString {
return &errpath.ErrField{Field: "default", Err: &errpath.ErrInvalid[string]{
Value: dflt,
Message: fmt.Sprintf("does not match schema type, got %s", s.Type),
}}
}
if s.Enum != nil {
if !slices.Contains(s.Enum, dflt) {
return &errpath.ErrField{Field: "default", Err: &errpath.ErrInvalid[string]{
Value: dflt,
Message: fmt.Sprintf("is not one of the enums (%q)", s.Enum),
}}
}
}
case float64:
switch s.Type {
case TypeNumber: // fits
case TypeInteger:
if asInt := int(dflt); dflt != float64(asInt) {
return &errpath.ErrField{Field: "default", Err: &errpath.ErrInvalid[float64]{
Value: dflt,
Message: fmt.Sprintf("does not match schema type, got %s", s.Type),
}}
} else {
s.Default = asInt // set to int version
}
default:
return &errpath.ErrField{Field: "default", Err: &errpath.ErrInvalid[float64]{
Value: dflt,
Message: fmt.Sprintf("does not match schema type, got %s", s.Type),
}}
}
case int:
switch s.Type {
case TypeNumber, TypeInteger: // fits
default:
return &errpath.ErrField{Field: "default", Err: &errpath.ErrInvalid[int]{
Value: dflt,
Message: fmt.Sprintf("does not match schema type, got %s", s.Type),
}}
}
default:
return &errpath.ErrField{Field: "default", Err: &errpath.ErrInvalid[any]{
Value: s.Default,
Message: fmt.Sprintf("unknown type %T", s.Default),
}}
}
return nil
}
func (l *loader) collectSchema(s *Schema, ref ref) {
l.schemas[ref.String()] = s // collect this schema
}
func (l *loader) resolveSchemaRef(s *SchemaRef) error {
return resolveRef(s, l.schemas, l.resolveSchema)
}
func (l *loader) resolveSchema(s *Schema) error {
if err := l.resolveSchemaRefList(s.AllOf); err != nil {
return &errpath.ErrField{Field: "allOf", Err: err}
}
if s.Items != nil {
if err := l.resolveSchemaRef(s.Items); err != nil {
return &errpath.ErrField{Field: "items", Err: err}
}
}
if err := l.resolveSchemaRefs(s.Properties); err != nil {
return &errpath.ErrField{Field: "properties", Err: err}
}
if s.AdditionalProperties != nil {
if err := l.resolveSchemaRef(s.AdditionalProperties); err != nil {
return &errpath.ErrField{Field: "additionalProperties", Err: err}
}
}
return nil
}
func (s *Schema) isEmpty() bool {
return s == nil ||
(s.Type == "" && s.Format == "" &&
len(s.AllOf) == 0 &&
s.Min == nil && s.Max == nil &&
s.Pattern == nil &&
s.MinItems == 0 && s.MaxItems == nil && s.Items == nil &&
s.Properties == nil && s.Required == nil &&
s.AdditionalProperties == nil &&
s.ContentMediaType == "" && s.ContentEncoding == "" &&
s.Example == nil)
}