forked from santhosh-tekuri/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft.go
331 lines (303 loc) · 8.31 KB
/
draft.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
package jsonschema
import (
"embed"
"fmt"
"strconv"
"strings"
)
// A Draft represents json-schema draft
type Draft struct {
version int
meta *Schema
id string // property name used to represent schema id.
boolSchema bool // is boolean valid schema
vocab []string // built-in vocab
defaultVocab []string // vocabs when $vocabulary is not used
subschemas map[string]position
}
func (d *Draft) URL() string {
switch d.version {
case 2020:
return "https://json-schema.org/draft/2020-12/schema"
case 2019:
return "https://json-schema.org/draft/2019-09/schema"
case 7:
return "https://json-schema.org/draft-07/schema"
case 6:
return "https://json-schema.org/draft-06/schema"
case 4:
return "https://json-schema.org/draft-04/schema"
}
return ""
}
func (d *Draft) String() string {
return fmt.Sprintf("Draft%d", d.version)
}
func (d *Draft) loadMeta(url string) {
c := NewCompiler()
c.AssertFormat = true
d.meta = c.MustCompile(url)
d.meta.meta = d.meta
}
func (d *Draft) getID(sch interface{}) string {
m, ok := sch.(map[string]interface{})
if !ok {
return ""
}
if _, ok := m["$ref"]; ok && d.version <= 7 {
// $ref prevents a sibling id from changing the base uri
return ""
}
v, ok := m[d.id]
if !ok {
return ""
}
id, ok := v.(string)
if !ok {
return ""
}
return id
}
func (d *Draft) resolveID(base string, sch interface{}) (string, error) {
id, _ := split(d.getID(sch)) // strip fragment
if id == "" {
return "", nil
}
url, err := resolveURL(base, id)
url, _ = split(url) // strip fragment
return url, err
}
func (d *Draft) anchors(sch interface{}) []string {
m, ok := sch.(map[string]interface{})
if !ok {
return nil
}
var anchors []string
// before draft2019, anchor is specified in id
_, f := split(d.getID(m))
if f != "#" {
anchors = append(anchors, f[1:])
}
if v, ok := m["$anchor"]; ok && d.version >= 2019 {
anchors = append(anchors, v.(string))
}
if v, ok := m["$dynamicAnchor"]; ok && d.version >= 2020 {
anchors = append(anchors, v.(string))
}
return anchors
}
// listSubschemas collects subschemas in r into rr.
func (d *Draft) listSubschemas(r *resource, base string, rr map[string]*resource) error {
add := func(loc string, sch interface{}) error {
url, err := d.resolveID(base, sch)
if err != nil {
return err
}
floc := r.floc + "/" + loc
sr := &resource{url: url, floc: floc, doc: sch}
rr[floc] = sr
base := base
if url != "" {
base = url
}
return d.listSubschemas(sr, base, rr)
}
sch, ok := r.doc.(map[string]interface{})
if !ok {
return nil
}
for kw, pos := range d.subschemas {
v, ok := sch[kw]
if !ok {
continue
}
if pos&self != 0 {
switch v := v.(type) {
case map[string]interface{}:
if err := add(kw, v); err != nil {
return err
}
case bool:
if d.boolSchema {
if err := add(kw, v); err != nil {
return err
}
}
}
}
if pos&item != 0 {
if v, ok := v.([]interface{}); ok {
for i, item := range v {
if err := add(kw+"/"+strconv.Itoa(i), item); err != nil {
return err
}
}
}
}
if pos&prop != 0 {
if v, ok := v.(map[string]interface{}); ok {
for pname, pval := range v {
if err := add(kw+"/"+escape(pname), pval); err != nil {
return err
}
}
}
}
}
return nil
}
// isVocab tells whether url is built-in vocab.
func (d *Draft) isVocab(url string) bool {
for _, v := range d.vocab {
if url == v {
return true
}
}
return false
}
type position uint
const (
self position = 1 << iota
prop
item
)
// supported drafts
var (
Draft4 = &Draft{version: 4, id: "id", boolSchema: false}
Draft6 = &Draft{version: 6, id: "$id", boolSchema: true}
Draft7 = &Draft{version: 7, id: "$id", boolSchema: true}
Draft2019 = &Draft{
version: 2019,
id: "$id",
boolSchema: true,
vocab: []string{
"https://json-schema.org/draft/2019-09/vocab/core",
"https://json-schema.org/draft/2019-09/vocab/applicator",
"https://json-schema.org/draft/2019-09/vocab/validation",
"https://json-schema.org/draft/2019-09/vocab/meta-data",
"https://json-schema.org/draft/2019-09/vocab/format",
"https://json-schema.org/draft/2019-09/vocab/content",
},
defaultVocab: []string{
"https://json-schema.org/draft/2019-09/vocab/core",
"https://json-schema.org/draft/2019-09/vocab/applicator",
"https://json-schema.org/draft/2019-09/vocab/validation",
},
}
Draft2020 = &Draft{
version: 2020,
id: "$id",
boolSchema: true,
vocab: []string{
"https://json-schema.org/draft/2020-12/vocab/core",
"https://json-schema.org/draft/2020-12/vocab/applicator",
"https://json-schema.org/draft/2020-12/vocab/unevaluated",
"https://json-schema.org/draft/2020-12/vocab/validation",
"https://json-schema.org/draft/2020-12/vocab/meta-data",
"https://json-schema.org/draft/2020-12/vocab/format-annotation",
"https://json-schema.org/draft/2020-12/vocab/format-assertion",
"https://json-schema.org/draft/2020-12/vocab/content",
},
defaultVocab: []string{
"https://json-schema.org/draft/2020-12/vocab/core",
"https://json-schema.org/draft/2020-12/vocab/applicator",
"https://json-schema.org/draft/2020-12/vocab/unevaluated",
"https://json-schema.org/draft/2020-12/vocab/validation",
},
}
Draft2020HyperSchema = &Draft{
version: 2020,
id: "$id",
boolSchema: true,
vocab: []string{
"https://json-schema.org/draft/2020-12/vocab/core",
"https://json-schema.org/draft/2020-12/vocab/applicator",
"https://json-schema.org/draft/2020-12/vocab/unevaluated",
"https://json-schema.org/draft/2020-12/vocab/validation",
"https://json-schema.org/draft/2020-12/vocab/meta-data",
"https://json-schema.org/draft/2020-12/vocab/format-annotation",
"https://json-schema.org/draft/2020-12/vocab/content",
"https://json-schema.org/draft/2019-09/vocab/hyper-schema",
},
defaultVocab: []string{
"https://json-schema.org/draft/2020-12/vocab/core",
"https://json-schema.org/draft/2020-12/vocab/applicator",
"https://json-schema.org/draft/2020-12/vocab/unevaluated",
"https://json-schema.org/draft/2020-12/vocab/validation",
},
}
latest = Draft2020
)
func findDraft(url string) *Draft {
if strings.HasPrefix(url, "http://") {
url = "https://" + strings.TrimPrefix(url, "http://")
}
if strings.HasSuffix(url, "#") || strings.HasSuffix(url, "#/") {
url = url[:strings.IndexByte(url, '#')]
}
switch url {
case "https://json-schema.org/schema":
return latest
case "https://json-schema.org/draft/2020-12/schema":
return Draft2020
case "https://json-schema.org/draft/2020-12/hyper-schema":
return Draft2020HyperSchema
case "https://json-schema.org/draft/2019-09/schema":
return Draft2019
case "https://json-schema.org/draft-07/schema":
return Draft7
case "https://json-schema.org/draft-06/schema":
return Draft6
case "https://json-schema.org/draft-04/schema":
return Draft4
}
return nil
}
//go:embed metaschemas
var metaFiles embed.FS
func init() {
subschemas := map[string]position{
// type agnostic
"definitions": prop,
"not": self,
"allOf": item,
"anyOf": item,
"oneOf": item,
// object
"properties": prop,
"additionalProperties": self,
"patternProperties": prop,
// array
"items": self | item,
"additionalItems": self,
"dependencies": prop,
}
Draft4.subschemas = clone(subschemas)
subschemas["propertyNames"] = self
subschemas["contains"] = self
Draft6.subschemas = clone(subschemas)
subschemas["if"] = self
subschemas["then"] = self
subschemas["else"] = self
Draft7.subschemas = clone(subschemas)
subschemas["$defs"] = prop
subschemas["dependentSchemas"] = prop
subschemas["unevaluatedProperties"] = self
subschemas["unevaluatedItems"] = self
subschemas["contentSchema"] = self
Draft2019.subschemas = clone(subschemas)
subschemas["prefixItems"] = item
Draft2020.subschemas = clone(subschemas)
Draft4.loadMeta("http://json-schema.org/draft-04/schema")
Draft6.loadMeta("http://json-schema.org/draft-06/schema")
Draft7.loadMeta("http://json-schema.org/draft-07/schema")
Draft2019.loadMeta("https://json-schema.org/draft/2019-09/schema")
Draft2020.loadMeta("https://json-schema.org/draft/2020-12/schema")
}
func clone(m map[string]position) map[string]position {
mm := make(map[string]position)
for k, v := range m {
mm[k] = v
}
return mm
}