-
Notifications
You must be signed in to change notification settings - Fork 8
/
schema.go
463 lines (395 loc) · 17.9 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
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
package jsonschema
import (
"regexp"
"github.com/goccy/go-json"
)
// Schema represents a JSON Schema as per the 2020-12 draft, containing all
// necessary metadata and validation properties defined by the specification.
type Schema struct {
compiledPatterns map[string]*regexp.Regexp // Cached compiled regular expressions for pattern properties.
compiler *Compiler // Reference to the associated Compiler instance.
parent *Schema // Parent schema for hierarchical resolution.
uri string // Internal schema identifier resolved during compilation.
baseURI string // Base URI for resolving relative references within the schema.
anchors map[string]*Schema // Anchors for quick lookup of internal schema references.
dynamicAnchors map[string]*Schema // Dynamic anchors for more flexible schema references.
schemas map[string]*Schema // Cache of compiled schemas.
ID string `json:"$id,omitempty"` // Public identifier for the schema.
Schema string `json:"$schema,omitempty"` // URI indicating the specification the schema conforms to.
Format *string `json:"format,omitempty"` // Format hint for string data, e.g., "email" or "date-time".
// Schema reference keywords, see https://json-schema.org/draft/2020-12/json-schema-core#ref
Ref string `json:"$ref,omitempty"` // Reference to another schema.
DynamicRef string `json:"$dynamicRef,omitempty"` // Reference to another schema that can be dynamically resolved.
Anchor string `json:"$anchor,omitempty"` // Anchor for resolving relative JSON Pointers.
DynamicAnchor string `json:"$dynamicAnchor,omitempty"` // Anchor for dynamic resolution
Defs map[string]*Schema `json:"$defs,omitempty"` // An object containing schema definitions.
ResolvedRef *Schema `json:"-"` // Resolved schema for $ref
ResolvedDynamicRef *Schema `json:"-"` // Resolved schema for $dynamicRef
// Boolean JSON Schemas, see https://json-schema.org/draft/2020-12/json-schema-core#name-boolean-json-schemas
Boolean *bool `json:"-"` // Boolean schema, used for quick validation.
// Applying subschemas with logical keywords, see https://json-schema.org/draft/2020-12/json-schema-core#name-keywords-for-applying-subsch
AllOf []*Schema `json:"allOf,omitempty"` // Array of schemas for validating the instance against all of them.
AnyOf []*Schema `json:"anyOf,omitempty"` // Array of schemas for validating the instance against any of them.
OneOf []*Schema `json:"oneOf,omitempty"` // Array of schemas for validating the instance against exactly one of them.
Not *Schema `json:"not,omitempty"` // Schema for validating the instance against the negation of it.
// Applying subschemas conditionally, see https://json-schema.org/draft/2020-12/json-schema-core#name-keywords-for-applying-subsche
If *Schema `json:"if,omitempty"` // Schema to be evaluated as a condition
Then *Schema `json:"then,omitempty"` // Schema to be evaluated if 'if' is successful
Else *Schema `json:"else,omitempty"` // Schema to be evaluated if 'if' is not successful
DependentSchemas map[string]*Schema `json:"dependentSchemas,omitempty"` // Dependent schemas based on property presence
// Applying subschemas to array keywords, see https://json-schema.org/draft/2020-12/json-schema-core#name-keywords-for-applying-subschem
PrefixItems []*Schema `json:"prefixItems,omitempty"` // Array of schemas for validating the array items' prefix.
Items *Schema `json:"items,omitempty"` // Schema for items in an array.
Contains *Schema `json:"contains,omitempty"` // Schema for validating items in the array.
// Applying subschemas to objects keywords, see https://json-schema.org/draft/2020-12/json-schema-core#name-keywords-for-applying-subschemas
Properties *SchemaMap `json:"properties,omitempty"` // Definitions of properties for object types.
PatternProperties *SchemaMap `json:"patternProperties,omitempty"` // Definitions of properties for object types matched by specific patterns.
AdditionalProperties *Schema `json:"additionalProperties,omitempty"` // Can be a boolean or a schema, controls additional properties handling.
PropertyNames *Schema `json:"propertyNames,omitempty"` // Can be a boolean or a schema, controls property names validation.
// Any validation keywords, see https://json-schema.org/draft/2020-12/json-schema-validation#section-6.1
Type SchemaType `json:"type,omitempty"` // Can be a single type or an array of types.
Enum []interface{} `json:"enum,omitempty"` // Enumerated values for the property.
Const *ConstValue `json:"const,omitempty"` // Constant value for the property.
// Numeric validation keywords, see https://json-schema.org/draft/2020-12/json-schema-validation#section-6.2
MultipleOf *Rat `json:"multipleOf,omitempty"` // Number must be a multiple of this value, strictly greater than 0.
Maximum *Rat `json:"maximum,omitempty"` // Maximum value of the number.
ExclusiveMaximum *Rat `json:"exclusiveMaximum,omitempty"` // Number must be less than this value.
Minimum *Rat `json:"minimum,omitempty"` // Minimum value of the number.
ExclusiveMinimum *Rat `json:"exclusiveMinimum,omitempty"` // Number must be greater than this value.
// String validation keywords, see https://json-schema.org/draft/2020-12/json-schema-validation#section-6.3
MaxLength *float64 `json:"maxLength,omitempty"` // Maximum length of a string.
MinLength *float64 `json:"minLength,omitempty"` // Minimum length of a string.
Pattern *string `json:"pattern,omitempty"` // Regular expression pattern to match the string against.
// Array validation keywords, see https://json-schema.org/draft/2020-12/json-schema-validation#section-6.4
MaxItems *float64 `json:"maxItems,omitempty"` // Maximum number of items in an array.
MinItems *float64 `json:"minItems,omitempty"` // Minimum number of items in an array.
UniqueItems *bool `json:"uniqueItems,omitempty"` // Whether the items in the array must be unique.
MaxContains *float64 `json:"maxContains,omitempty"` // Maximum number of items in the array that can match the contains schema.
MinContains *float64 `json:"minContains,omitempty"` // Minimum number of items in the array that must match the contains schema.
// https://json-schema.org/draft/2020-12/json-schema-core#name-unevaluateditems
UnevaluatedItems *Schema `json:"unevaluatedItems,omitempty"` // Schema for unevaluated items in an array.
// Object validation keywords, see https://json-schema.org/draft/2020-12/json-schema-validation#section-6.5
MaxProperties *float64 `json:"maxProperties,omitempty"` // Maximum number of properties in an object.
MinProperties *float64 `json:"minProperties,omitempty"` // Minimum number of properties in an object.
Required []string `json:"required,omitempty"` // List of required property names for object types.
DependentRequired map[string][]string `json:"dependentRequired,omitempty"` // Properties required when another property is present.
// https://json-schema.org/draft/2020-12/json-schema-core#name-unevaluatedproperties
UnevaluatedProperties *Schema `json:"unevaluatedProperties,omitempty"` // Schema for unevaluated properties in an object.
// Content validation keywords, see https://json-schema.org/draft/2020-12/json-schema-validation#name-a-vocabulary-for-the-conten
ContentEncoding *string `json:"contentEncoding,omitempty"` // Encoding format of the content.
ContentMediaType *string `json:"contentMediaType,omitempty"` // Media type of the content.
ContentSchema *Schema `json:"contentSchema,omitempty"` // Schema for validating the content.
// Meta-data for schema and instance description, see https://json-schema.org/draft/2020-12/json-schema-validation#name-a-vocabulary-for-basic-meta
Title *string `json:"title,omitempty"` // A short summary of the schema.
Description *string `json:"description,omitempty"` // A detailed description of the purpose of the schema.
Default interface{} `json:"default,omitempty"` // Default value of the instance.
Deprecated *bool `json:"deprecated,omitempty"` // Indicates that the schema is deprecated.
ReadOnly *bool `json:"readOnly,omitempty"` // Indicates that the property is read-only.
WriteOnly *bool `json:"writeOnly,omitempty"` // Indicates that the property is write-only.
Examples []interface{} `json:"examples,omitempty"` // Examples of the instance data that validates against this schema.
}
// newSchema parses JSON schema data and returns a Schema object.
func newSchema(jsonSchema []byte) (*Schema, error) {
var schema Schema
err := json.Unmarshal(jsonSchema, &schema)
if err != nil {
return &schema, err
}
return &schema, nil
}
// initializeSchema sets up the schema structure, resolves URIs, and initializes nested schemas.
// It populates schema properties from the compiler settings and the parent schema context.
func (s *Schema) initializeSchema(compiler *Compiler, parent *Schema) {
s.compiler = compiler
s.parent = parent
parentBaseURI := s.getParentBaseURI()
if parentBaseURI == "" {
parentBaseURI = compiler.DefaultBaseURI
}
if s.ID != "" {
if isValidURI(s.ID) {
s.uri = s.ID
s.baseURI = getBaseURI(s.ID)
} else {
resolvedURL := resolveRelativeURI(parentBaseURI, s.ID)
s.uri = resolvedURL
s.baseURI = getBaseURI(resolvedURL)
}
} else {
s.baseURI = parentBaseURI
}
if s.baseURI == "" {
if s.uri != "" && isValidURI(s.uri) {
s.baseURI = getBaseURI(s.uri)
}
}
if s.Anchor != "" {
s.setAnchor(s.Anchor)
}
if s.DynamicAnchor != "" {
s.setDynamicAnchor(s.DynamicAnchor)
}
if s.uri != "" && isValidURI(s.uri) {
root := s.getRootSchema()
root.setSchema(s.uri, s)
}
initializeNestedSchemas(s, compiler)
s.resolveReferences()
}
// initializeNestedSchemas initializes all nested or related schemas as defined in the structure.
func initializeNestedSchemas(s *Schema, compiler *Compiler) {
if s.Defs != nil {
for _, def := range s.Defs {
def.initializeSchema(compiler, s)
}
}
// Initialize logical schema groupings
initializeSchemas(s.AllOf, compiler, s)
initializeSchemas(s.AnyOf, compiler, s)
initializeSchemas(s.OneOf, compiler, s)
// Initialize conditional schemas
if s.Not != nil {
s.Not.initializeSchema(compiler, s)
}
if s.If != nil {
s.If.initializeSchema(compiler, s)
}
if s.Then != nil {
s.Then.initializeSchema(compiler, s)
}
if s.Else != nil {
s.Else.initializeSchema(compiler, s)
}
if s.DependentSchemas != nil {
for _, depSchema := range s.DependentSchemas {
depSchema.initializeSchema(compiler, s)
}
}
// Initialize array and object schemas
if s.PrefixItems != nil {
for _, item := range s.PrefixItems {
item.initializeSchema(compiler, s)
}
}
if s.Items != nil {
s.Items.initializeSchema(compiler, s)
}
if s.Contains != nil {
s.Contains.initializeSchema(compiler, s)
}
if s.AdditionalProperties != nil {
s.AdditionalProperties.initializeSchema(compiler, s)
}
if s.Properties != nil {
for _, prop := range *s.Properties {
prop.initializeSchema(compiler, s)
}
}
if s.PatternProperties != nil {
for _, prop := range *s.PatternProperties {
prop.initializeSchema(compiler, s)
}
}
if s.UnevaluatedProperties != nil {
s.UnevaluatedProperties.initializeSchema(compiler, s)
}
if s.UnevaluatedItems != nil {
s.UnevaluatedItems.initializeSchema(compiler, s)
}
if s.ContentSchema != nil {
s.ContentSchema.initializeSchema(compiler, s)
}
if s.PropertyNames != nil {
s.PropertyNames.initializeSchema(compiler, s)
}
}
// setAnchor creates or updates the anchor mapping for the current schema and propagates it to parent schemas.
func (s *Schema) setAnchor(anchor string) {
if s.anchors == nil {
s.anchors = make(map[string]*Schema)
}
s.anchors[anchor] = s
root := s.getRootSchema()
if root.anchors == nil {
root.anchors = make(map[string]*Schema)
}
if _, ok := root.anchors[anchor]; !ok {
root.anchors[anchor] = s
}
}
// setDynamicAnchor sets or updates a dynamic anchor for the current schema and propagates it to parents in the same scope.
func (s *Schema) setDynamicAnchor(anchor string) {
if s.dynamicAnchors == nil {
s.dynamicAnchors = make(map[string]*Schema)
}
if _, ok := s.dynamicAnchors[anchor]; !ok {
s.dynamicAnchors[anchor] = s
}
scope := s.getScopeSchema()
if scope.dynamicAnchors == nil {
scope.dynamicAnchors = make(map[string]*Schema)
}
if _, ok := scope.dynamicAnchors[anchor]; !ok {
scope.dynamicAnchors[anchor] = s
}
}
// setSchema adds a schema to the internal schema cache, using the provided URI as the key.
func (s *Schema) setSchema(uri string, schema *Schema) *Schema {
if s.schemas == nil {
s.schemas = make(map[string]*Schema)
}
s.schemas[uri] = schema
return s
}
func (s *Schema) getSchema(ref string) (*Schema, error) {
baseURI, anchor := splitRef(ref)
if schema, exists := s.schemas[baseURI]; exists {
if baseURI == ref {
return schema, nil
}
return schema.resolveAnchor(anchor)
}
return nil, ErrFailedToResolveReference
}
// initializeSchemas iteratively initializes a list of nested schemas.
func initializeSchemas(schemas []*Schema, compiler *Compiler, parent *Schema) {
for _, schema := range schemas {
if schema != nil {
schema.initializeSchema(compiler, parent)
}
}
}
// GetSchemaURI returns the resolved URI for the schema, or an empty string if no URI is defined.
func (s *Schema) GetSchemaURI() string {
if s.uri != "" {
return s.uri
}
root := s.getRootSchema()
if root.uri != "" {
return root.uri
}
return ""
}
func (s *Schema) GetSchemaLocation(anchor string) string {
uri := s.GetSchemaURI()
return uri + "#" + anchor
}
// getRootSchema returns the highest-level parent schema, serving as the root in the schema tree.
func (s *Schema) getRootSchema() *Schema {
if s.parent != nil {
return s.parent.getRootSchema()
}
return s
}
func (s *Schema) getScopeSchema() *Schema {
if s.ID != "" {
return s
} else if s.parent != nil {
return s.parent.getScopeSchema()
}
return s
}
// getParentBaseURI returns the base URI from the nearest parent schema that has one defined,
// or an empty string if none of the parents up to the root define a base URI.
func (s *Schema) getParentBaseURI() string {
for p := s.parent; p != nil; p = p.parent {
if p.baseURI != "" {
return p.baseURI
}
}
return ""
}
// UnmarshalJSON customizes the unmarshaling of a Schema to properly initialize properties,
// particularly handling the 'const' keyword to manage null values and avoid recursion issues.
func (s *Schema) UnmarshalJSON(data []byte) error {
var boolVal bool
if err := json.Unmarshal(data, &boolVal); err == nil {
s.Boolean = &boolVal
return nil
}
type Alias Schema
aux := struct {
*Alias
RawConst json.RawMessage `json:"const,omitempty"` // Manually parse 'const' to handle specific edge cases.
}{
Alias: (*Alias)(s),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
if aux.RawConst != nil {
s.Const = &ConstValue{IsSet: true}
if err := json.Unmarshal(aux.RawConst, &s.Const.Value); err != nil {
return err
}
}
return nil
}
// MarshalJSON ensures that Schema instances serialize correctly, particularly handling boolean schemas directly.
func (s *Schema) MarshalJSON() ([]byte, error) {
if s.Boolean != nil {
return json.Marshal(s.Boolean)
}
type Alias Schema
return json.Marshal(&struct {
*Alias
}{
Alias: (*Alias)(s),
})
}
// SchemaMap represents a map of string keys to *Schema values, used primarily for properties and patternProperties.
type SchemaMap map[string]*Schema
// MarshalJSON ensures that SchemaMap serializes properly as a JSON object.
func (sm SchemaMap) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]*Schema(sm))
}
// UnmarshalJSON ensures that JSON objects are correctly parsed into SchemaMap,
// supporting the detailed structure required for nested schema definitions.
func (sm *SchemaMap) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, (*map[string]*Schema)(sm))
}
// SchemaType holds a set of SchemaType values, accommodating complex schema definitions that permit multiple types.
type SchemaType []string
// MarshalJSON customizes the JSON serialization of SchemaType.
func (r SchemaType) MarshalJSON() ([]byte, error) {
if len(r) == 1 {
// Serialize a single type as a simple JSON string if only one type is present.
return json.Marshal(r[0])
}
// Serialize multiple types as a JSON array of strings.
return json.Marshal([]string(r))
}
// UnmarshalJSON customizes the JSON deserialization into SchemaType.
func (r *SchemaType) UnmarshalJSON(data []byte) error {
var singleType string
// Attempt to unmarshal the data as a single SchemaType.
if err := json.Unmarshal(data, &singleType); err == nil {
*r = SchemaType{singleType}
return nil
}
var multiType []string
// Attempt to unmarshal the data as an array of SchemaType.
if err := json.Unmarshal(data, &multiType); err == nil {
*r = SchemaType(multiType)
return nil
}
// Return a global error if the data does not conform to either a single or multiple SchemaType formats.
return ErrInvalidJSONSchemaType
}
type ConstValue struct {
Value interface{}
IsSet bool
}
// UnmarshalJSON handles unmarshaling a JSON value into the ConstValue type.
func (cv *ConstValue) UnmarshalJSON(data []byte) error {
cv.IsSet = true // If UnmarshalJSON is called, the 'const' was set in JSON.
return json.Unmarshal(data, &cv.Value)
}
// MarshalJSON handles marshaling the ConstValue type back to JSON.
func (cv ConstValue) MarshalJSON() ([]byte, error) {
if !cv.IsSet {
return nil, nil
}
// Otherwise, marshal the value as normal
return json.Marshal(cv.Value)
}