forked from santhosh-tekuri/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcompiler.go
570 lines (516 loc) · 13.7 KB
/
compiler.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
// Copyright 2017 Santhosh Kumar Tekuri. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package jsonschema
import (
"context"
"encoding/json"
"fmt"
"io"
"math/big"
"regexp"
"strings"
)
// A Draft represents json-schema draft
type Draft struct {
meta *Schema
id string // property name used to represent schema id.
version int
data string
url string
}
var latest = Draft7
// A Compiler represents a json-schema compiler.
//
// Currently draft4, draft6 and draft7 are supported
type Compiler struct {
// Draft represents the draft used when '$schema' attribute is missing.
//
// This defaults to latest draft (currently draft7).
Draft *Draft
resources map[string]*resource
// Extensions is used to register extensions.
Extensions map[string]Extension
// ExtractAnnotations tells whether schema annotations has to be extracted
// in compiled Schema or not.
ExtractAnnotations bool
// LoadURL loads the document at given URL.
//
// If nil, package global LoadURL is used.
LoadURL func(ctx context.Context, s string) (io.ReadCloser, error)
}
// NewCompiler returns a json-schema Compiler object.
// if '$schema' attribute is missing, it is treated as draft7. to change this
// behavior change Compiler.Draft value
func NewCompiler() *Compiler {
c := &Compiler{
Draft: latest,
resources: make(map[string]*resource),
Extensions: make(map[string]Extension),
}
drafts := []*Draft{Draft7, Draft6, Draft4}
for _, d := range drafts {
if err := c.AddResource(d.url, strings.NewReader(d.data)); err != nil {
panic(fmt.Sprintf("could not add draft %s: %s", d.url, err.Error()))
}
}
return c
}
// AddResource adds in-memory resource to the compiler.
//
// Note that url must not have fragment
func (c *Compiler) AddResource(url string, r io.Reader) error {
res, err := newResource(url, r)
if err != nil {
return err
}
c.resources[res.url] = res
return nil
}
// MustCompile is like Compile but panics if the url cannot be compiled to *Schema.
// It simplifies safe initialization of global variables holding compiled Schemas.
func (c *Compiler) MustCompile(ctx context.Context, url string) *Schema {
s, err := c.Compile(ctx, url)
if err != nil {
panic(fmt.Sprintf("jsonschema: Compile(%q): %s", url, err))
}
return s
}
// Compile parses json-schema at given url returns, if successful,
// a Schema object that can be used to match against json.
func (c *Compiler) Compile(ctx context.Context, url string) (*Schema, error) {
base, fragment := split(url)
if _, ok := c.resources[base]; !ok {
r, err := c.loadURL(ctx, base)
if err != nil {
return nil, err
}
defer r.Close()
if err := c.AddResource(base, r); err != nil {
return nil, err
}
}
r := c.resources[base]
if r.draft == nil {
if m, ok := r.doc.(map[string]interface{}); ok {
if url, ok := m["$schema"]; ok {
switch url {
case "http://json-schema.org/schema#":
r.draft = latest
case "http://json-schema.org/draft-07/schema#":
r.draft = Draft7
case "http://json-schema.org/draft-06/schema#":
r.draft = Draft6
case "http://json-schema.org/draft-04/schema#":
r.draft = Draft4
default:
return nil, fmt.Errorf("unknown $schema %q", url)
}
}
}
if r.draft == nil {
r.draft = c.Draft
}
}
return c.compileRef(ctx, r, r.url, fragment)
}
func (c Compiler) loadURL(ctx context.Context, s string) (io.ReadCloser, error) {
if c.LoadURL != nil {
return c.LoadURL(ctx, s)
}
return LoadURL(ctx, s)
}
func (c *Compiler) compileRef(ctx context.Context, r *resource, base, ref string) (*Schema, error) {
var err error
if rootFragment(ref) {
if _, ok := r.schemas["#"]; !ok {
if err := c.validateSchema(r, "", r.doc); err != nil {
return nil, err
}
s := &Schema{URL: r.url, Ptr: "#"}
r.schemas["#"] = s
if _, err := c.compile(ctx, r, s, base, r.doc); err != nil {
return nil, err
}
}
return r.schemas["#"], nil
}
if strings.HasPrefix(ref, "#/") {
if _, ok := r.schemas[ref]; !ok {
ptrBase, doc, err := r.resolvePtr(ref)
if err != nil {
return nil, err
}
if err := c.validateSchema(r, strings.TrimPrefix(ref, "#/"), doc); err != nil {
return nil, err
}
r.schemas[ref] = &Schema{URL: base, Ptr: ref}
if _, err := c.compile(ctx, r, r.schemas[ref], ptrBase, doc); err != nil {
return nil, err
}
}
return r.schemas[ref], nil
}
refURL, err := resolveURL(base, ref)
if err != nil {
return nil, err
}
if rs, ok := r.schemas[refURL]; ok {
return rs, nil
}
ids := make(map[string]map[string]interface{})
if err := resolveIDs(r.draft, r.url, r.doc, ids); err != nil {
return nil, err
}
if v, ok := ids[refURL]; ok {
if err := c.validateSchema(r, "", v); err != nil {
return nil, err
}
u, f := split(refURL)
s := &Schema{URL: u, Ptr: f}
r.schemas[refURL] = s
if err := c.compileMap(ctx, r, s, refURL, v); err != nil {
return nil, err
}
return s, nil
}
base, _ = split(refURL)
if base == r.url {
return nil, fmt.Errorf("invalid ref: %q", refURL)
}
return c.Compile(ctx, refURL)
}
func (c *Compiler) compile(ctx context.Context, r *resource, s *Schema, base string, m interface{}) (*Schema, error) {
if s == nil {
s = new(Schema)
s.URL, _ = split(base)
}
switch m := m.(type) {
case bool:
s.Always = &m
return s, nil
default:
return s, c.compileMap(ctx, r, s, base, m.(map[string]interface{}))
}
}
func (c *Compiler) compileMap(ctx context.Context, r *resource, s *Schema, base string, m map[string]interface{}) error {
var err error
if id, ok := m[r.draft.id]; ok {
if base, err = resolveURL(base, id.(string)); err != nil {
return err
}
}
if ref, ok := m["$ref"]; ok {
b, _ := split(base)
s.Ref, err = c.compileRef(ctx, r, b, ref.(string))
if err != nil {
return err
}
// All other properties in a "$ref" object MUST be ignored
return nil
}
if t, ok := m["type"]; ok {
switch t := t.(type) {
case string:
s.Types = []string{t}
case []interface{}:
s.Types = toStrings(t)
}
}
if e, ok := m["enum"]; ok {
s.Enum = e.([]interface{})
allPrimitives := true
for _, item := range s.Enum {
switch jsonType(item) {
case "object", "array":
allPrimitives = false
}
}
s.enumError = "enum failed"
if allPrimitives {
if len(s.Enum) == 1 {
s.enumError = fmt.Sprintf("value must be %#v", s.Enum[0])
} else {
strEnum := make([]string, len(s.Enum))
for i, item := range s.Enum {
strEnum[i] = fmt.Sprintf("%#v", item)
}
s.enumError = fmt.Sprintf("value must be one of %s", strings.Join(strEnum, ", "))
}
}
}
loadSchema := func(pname string) (*Schema, error) {
if pvalue, ok := m[pname]; ok {
return c.compile(ctx, r, nil, base, pvalue)
}
return nil, nil
}
if s.Not, err = loadSchema("not"); err != nil {
return err
}
loadSchemas := func(pname string) ([]*Schema, error) {
if pvalue, ok := m[pname]; ok {
pvalue := pvalue.([]interface{})
schemas := make([]*Schema, len(pvalue))
for i, v := range pvalue {
sch, err := c.compile(ctx, r, nil, base, v)
if err != nil {
return nil, err
}
schemas[i] = sch
}
return schemas, nil
}
return nil, nil
}
if s.AllOf, err = loadSchemas("allOf"); err != nil {
return err
}
if s.AnyOf, err = loadSchemas("anyOf"); err != nil {
return err
}
if s.OneOf, err = loadSchemas("oneOf"); err != nil {
return err
}
loadInt := func(pname string) int {
if num, ok := m[pname]; ok {
i, _ := num.(json.Number).Int64()
return int(i)
}
return -1
}
s.MinProperties, s.MaxProperties = loadInt("minProperties"), loadInt("maxProperties")
if req, ok := m["required"]; ok {
s.Required = toStrings(req.([]interface{}))
}
if props, ok := m["properties"]; ok {
props := props.(map[string]interface{})
s.Properties = make(map[string]*Schema, len(props))
for pname, pmap := range props {
s.Properties[pname], err = c.compile(ctx, r, nil, base, pmap)
if err != nil {
return err
}
}
}
if regexProps, ok := m["regexProperties"]; ok {
s.RegexProperties = regexProps.(bool)
}
if patternProps, ok := m["patternProperties"]; ok {
patternProps := patternProps.(map[string]interface{})
s.PatternProperties = make(map[*regexp.Regexp]*Schema, len(patternProps))
for pattern, pmap := range patternProps {
s.PatternProperties[regexp.MustCompile(pattern)], err = c.compile(ctx, r, nil, base, pmap)
if err != nil {
return err
}
}
}
if additionalProps, ok := m["additionalProperties"]; ok {
switch additionalProps := additionalProps.(type) {
case bool:
if !additionalProps {
s.AdditionalProperties = false
}
case map[string]interface{}:
s.AdditionalProperties, err = c.compile(ctx, r, nil, base, additionalProps)
if err != nil {
return err
}
}
}
if deps, ok := m["dependencies"]; ok {
deps := deps.(map[string]interface{})
s.Dependencies = make(map[string]interface{}, len(deps))
for pname, pvalue := range deps {
switch pvalue := pvalue.(type) {
case []interface{}:
s.Dependencies[pname] = toStrings(pvalue)
default:
s.Dependencies[pname], err = c.compile(ctx, r, nil, base, pvalue)
if err != nil {
return err
}
}
}
}
s.MinItems, s.MaxItems = loadInt("minItems"), loadInt("maxItems")
if unique, ok := m["uniqueItems"]; ok {
s.UniqueItems = unique.(bool)
}
if items, ok := m["items"]; ok {
switch items := items.(type) {
case []interface{}:
s.Items, err = loadSchemas("items")
if err != nil {
return err
}
if additionalItems, ok := m["additionalItems"]; ok {
switch additionalItems := additionalItems.(type) {
case bool:
s.AdditionalItems = additionalItems
case map[string]interface{}:
s.AdditionalItems, err = c.compile(ctx, r, nil, base, additionalItems)
if err != nil {
return err
}
}
} else {
s.AdditionalItems = true
}
default:
s.Items, err = c.compile(ctx, r, nil, base, items)
if err != nil {
return err
}
}
}
s.MinLength, s.MaxLength = loadInt("minLength"), loadInt("maxLength")
if pattern, ok := m["pattern"]; ok {
s.Pattern = regexp.MustCompile(pattern.(string))
}
if format, ok := m["format"]; ok {
s.Format = format.(string)
s.format = Formats[s.Format]
}
loadFloat := func(pname string) *big.Float {
if num, ok := m[pname]; ok {
r, _ := new(big.Float).SetString(string(num.(json.Number)))
return r
}
return nil
}
s.Minimum = loadFloat("minimum")
if exclusive, ok := m["exclusiveMinimum"]; ok {
if exclusive, ok := exclusive.(bool); ok {
if exclusive {
s.Minimum, s.ExclusiveMinimum = nil, s.Minimum
}
} else {
s.ExclusiveMinimum = loadFloat("exclusiveMinimum")
}
}
s.Maximum = loadFloat("maximum")
if exclusive, ok := m["exclusiveMaximum"]; ok {
if exclusive, ok := exclusive.(bool); ok {
if exclusive {
s.Maximum, s.ExclusiveMaximum = nil, s.Maximum
}
} else {
s.ExclusiveMaximum = loadFloat("exclusiveMaximum")
}
}
s.MultipleOf = loadFloat("multipleOf")
if c.ExtractAnnotations {
if title, ok := m["title"]; ok {
s.Title = title.(string)
}
if description, ok := m["description"]; ok {
s.Description = description.(string)
}
s.Default = m["default"]
}
if r.draft.version >= 6 {
if c, ok := m["const"]; ok {
s.Constant = []interface{}{c}
}
if s.PropertyNames, err = loadSchema("propertyNames"); err != nil {
return err
}
if s.Contains, err = loadSchema("contains"); err != nil {
return err
}
}
if r.draft.version >= 7 {
if m["if"] != nil && (m["then"] != nil || m["else"] != nil) {
if s.If, err = loadSchema("if"); err != nil {
return err
}
if s.Then, err = loadSchema("then"); err != nil {
return err
}
if s.Else, err = loadSchema("else"); err != nil {
return err
}
}
if encoding, ok := m["contentEncoding"]; ok {
s.ContentEncoding = encoding.(string)
s.decoder = Decoders[s.ContentEncoding]
}
if mediaType, ok := m["contentMediaType"]; ok {
s.ContentMediaType = mediaType.(string)
s.mediaType = MediaTypes[s.ContentMediaType]
}
if c.ExtractAnnotations {
if readOnly, ok := m["readOnly"]; ok {
s.ReadOnly = readOnly.(bool)
}
if writeOnly, ok := m["writeOnly"]; ok {
s.WriteOnly = writeOnly.(bool)
}
if examples, ok := m["examples"]; ok {
s.Examples = examples.([]interface{})
}
}
}
for name, ext := range c.Extensions {
cs, err := ext.Compile(CompilerContext{c, r, base}, m)
if err != nil {
return err
}
if cs != nil {
if s.Extensions == nil {
s.Extensions = make(map[string]interface{})
s.extensions = make(map[string]func(ctx ValidationContext, s interface{}, v interface{}) error)
}
s.Extensions[name] = cs
s.extensions[name] = ext.Validate
}
}
return nil
}
func (c *Compiler) validateSchema(r *resource, ptr string, v interface{}) error {
validate := func(meta *Schema) error {
if meta == nil {
return nil
}
if err := meta.validate(v); err != nil {
_ = addContext(ptr, "", err)
finishSchemaContext(err, meta)
finishInstanceContext(err)
var instancePtr string
if ptr == "" {
instancePtr = "#"
} else {
instancePtr = "#/" + ptr
}
return &SchemaError{
r.url,
&ValidationError{
Message: fmt.Sprintf("doesn't validate with %q", meta.URL+meta.Ptr),
InstancePtr: instancePtr,
SchemaURL: meta.URL,
SchemaPtr: "#",
Causes: []*ValidationError{err.(*ValidationError)},
},
}
}
return nil
}
if err := validate(r.draft.meta); err != nil {
return err
}
for _, ext := range c.Extensions {
if err := validate(ext.Meta); err != nil {
return err
}
}
return nil
}
func toStrings(arr []interface{}) []string {
s := make([]string, len(arr))
for i, v := range arr {
s[i] = v.(string)
}
return s
}