forked from santhosh-tekuri/jsonschema
/
compiler.go
492 lines (447 loc) · 11.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
// 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 (
"encoding/json"
"fmt"
"io"
"math/big"
"regexp"
"strings"
"github.com/santhosh-tekuri/jsonschema/formats"
"github.com/santhosh-tekuri/jsonschema/loader"
)
// A Draft represents json-schema draft
type Draft struct {
meta *Schema
id string
}
var latest = Draft6
func (draft *Draft) validateSchema(url, ptr string, v interface{}) error {
if meta := draft.meta; meta != 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{
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
}
// A Compiler represents a json-schema compiler.
//
// Currently draft4 and draft6 are supported
type Compiler struct {
// Draft represents the draft used when '$schema' attribute is missing.
//
// This defaults to latest draft (currently draft6).
Draft *Draft
resources map[string]*resource
}
// NewCompiler returns a draft4 json-schema Compiler object.
func NewCompiler() *Compiler {
return &Compiler{resources: make(map[string]*resource)}
}
// 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
}
func (c *Compiler) draft(v interface{}) (*Draft, error) {
if m, ok := v.(map[string]interface{}); ok {
if url, ok := m["$schema"]; ok {
switch url {
case "http://json-schema.org/schema#":
return latest, nil
case "http://json-schema.org/draft-06/schema#":
return Draft6, nil
case "http://json-schema.org/draft-04/schema#":
return Draft4, nil
default:
return nil, fmt.Errorf("unknown $schema %q", url)
}
}
}
if c.Draft == nil {
return latest, nil
}
return c.Draft, 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(url string) *Schema {
s, err := c.Compile(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(url string) (*Schema, error) {
base, fragment := split(url)
if _, ok := c.resources[base]; !ok {
r, err := loader.Load(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]
return c.compileRef(nil, r, nil, r.url, fragment)
}
func (c Compiler) compileRef(draft *Draft, r *resource, root map[string]interface{}, base, ref string) (*Schema, error) {
var err error
if rootFragment(ref) {
if _, ok := r.schemas["#"]; !ok {
if draft == nil {
draft, err = c.draft(r.doc)
if err != nil {
return nil, err
}
}
if err := draft.validateSchema(r.url, "", r.doc); err != nil {
return nil, err
}
s := &Schema{URL: r.url, Ptr: "#"}
r.schemas["#"] = s
if m, ok := r.doc.(map[string]interface{}); ok {
if _, err := c.compile(draft, r, s, base, m, m); err != nil {
return nil, err
}
} else {
if _, err := c.compile(draft, r, s, base, nil, r.doc); err != nil {
return nil, err
}
}
}
return r.schemas["#"], nil
}
if strings.HasPrefix(ref, "#/") {
if _, ok := r.schemas[ref]; !ok {
docDraft := draft
if docDraft == nil {
docDraft = c.Draft
}
if docDraft == nil {
docDraft = latest
}
ptrBase, doc, err := r.resolvePtr(docDraft, ref)
if err != nil {
return nil, err
}
if draft == nil {
draft, err = c.draft(doc)
if err != nil {
return nil, err
}
}
if err := draft.validateSchema(r.url, strings.TrimPrefix(ref, "#/"), doc); err != nil {
return nil, err
}
r.schemas[ref] = &Schema{URL: base, Ptr: ref}
if _, err := c.compile(draft, r, r.schemas[ref], ptrBase, root, 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(draft, r.url, root, ids); err != nil {
return nil, err
}
if v, ok := ids[refURL]; ok {
if err := draft.validateSchema(r.url, "", v); err != nil {
return nil, err
}
u, f := split(refURL)
s := &Schema{URL: u, Ptr: f}
r.schemas[refURL] = s
if err := c.compileMap(draft, r, s, refURL, root, 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(refURL)
}
func (c Compiler) compile(draft *Draft, r *resource, s *Schema, base string, root map[string]interface{}, 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(draft, r, s, base, root, m.(map[string]interface{}))
}
}
func (c Compiler) compileMap(draft *Draft, r *resource, s *Schema, base string, root, m map[string]interface{}) error {
var err error
if id, ok := m[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(draft, r, root, 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
break
}
}
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, ", "))
}
}
}
if not, ok := m["not"]; ok {
s.Not, err = c.compile(draft, r, nil, base, root, not)
if 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(draft, r, nil, base, root, 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(draft, r, nil, base, root, 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(draft, r, nil, base, root, 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(draft, r, nil, base, root, 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(draft, r, nil, base, root, 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(draft, r, nil, base, root, additionalItems)
if err != nil {
return err
}
}
} else {
s.AdditionalItems = true
}
default:
s.Items, err = c.compile(draft, r, nil, base, root, 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.FormatName = format.(string)
s.Format, _ = formats.Get(s.FormatName)
}
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 draft == Draft6 {
if c, ok := m["const"]; ok {
s.Constant = []interface{}{c}
}
if propertyNames, ok := m["propertyNames"]; ok {
s.PropertyNames, err = c.compile(draft, r, nil, base, root, propertyNames)
if err != nil {
return err
}
}
if contains, ok := m["contains"]; ok {
s.Contains, err = c.compile(draft, r, nil, base, root, contains)
if 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
}