-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ref-resolver.js
229 lines (192 loc) · 6.16 KB
/
ref-resolver.js
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
'use strict'
const URI = require('uri-js')
const cloner = require('rfdc')({ proto: true, circles: false })
const { EventEmitter } = require('events')
const debug = require('debug')('json-schema-resolver')
const kIgnore = Symbol('json-schema-resolver.ignore') // untrack a schema (usually the root one)
const kRefToDef = Symbol('json-schema-resolver.refToDef') // assign to an external json a new reference
const kConsumed = Symbol('json-schema-resolver.consumed') // when an external json has been referenced
// ! Target: DRAFT-07
// https://tools.ietf.org/html/draft-handrews-json-schema-01
// ? Open to DRAFT 08
// https://json-schema.org/draft/2019-09/json-schema-core.html
const defaultOpts = {
target: 'draft-07',
clone: false
}
const targetSupported = ['draft-07'] // TODO , 'draft-08'
const targetCfg = {
'draft-07': {
def: 'definitions'
},
'draft-08': {
def: '$defs'
}
}
// logic: https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.appendix.B.1
function jsonSchemaResolver (options) {
const ee = new EventEmitter()
const { clone, target, applicationUri, externalSchemas: rootExternalSchemas } = Object.assign({}, defaultOpts, options)
const allIds = new Map()
let rolling = 0
ee.on('$id', collectIds)
const allRefs = []
ee.on('$ref', collectRefs)
if (!targetSupported.includes(target)) {
throw new Error(`Unsupported JSON schema version ${target}`)
}
let defaultUri
if (applicationUri) {
defaultUri = getRootUri(applicationUri)
if (rootExternalSchemas) {
for (const es of rootExternalSchemas) { mapIds(ee, defaultUri, es) }
debug('Processed root external schemas')
}
} else if (rootExternalSchemas) {
throw new Error('If you set root externalSchema, the applicationUri option is needed')
}
return {
resolve,
definitions () {
const defKey = targetCfg[target].def
const x = { [defKey]: {} }
allIds.forEach((json, baseUri) => {
x[defKey][json[kRefToDef]] = json
})
return x
}
}
function resolve (rootSchema, opts) {
const { externalSchemas } = opts || {}
if (!rootExternalSchemas) {
allIds.clear()
}
allRefs.length = 0
if (clone) {
rootSchema = cloner(rootSchema)
}
const appUri = defaultUri || getRootUri(rootSchema.$id)
debug('Found app URI %o', appUri)
if (externalSchemas) {
for (const es of externalSchemas) { mapIds(ee, appUri, es) }
debug('Processed external schemas')
}
const baseUri = URI.serialize(appUri) // canonical absolute-URI
if (rootSchema.$id) {
rootSchema.$id = baseUri // fix the schema $id value
}
rootSchema[kIgnore] = true
mapIds(ee, appUri, rootSchema)
debug('Processed root schema')
debug('Generating %d refs', allRefs.length)
allRefs.forEach(({ baseUri, ref, refUri, json }) => {
debug('Evaluating $ref %s', ref)
if (ref[0] === '#') { return }
const evaluatedJson = allIds.get(baseUri)
if (!evaluatedJson) {
debug('External $ref %s not provided with baseUri %s', ref, baseUri)
return
}
evaluatedJson[kConsumed] = true
json.$ref = `#/definitions/${evaluatedJson[kRefToDef]}${refUri.fragment || ''}`
})
if (externalSchemas) {
// only if user sets external schema add it to the definitions
const defKey = targetCfg[target].def
allIds.forEach((json, baseUri) => {
if (json[kConsumed] === true) {
if (!rootSchema[defKey]) {
rootSchema[defKey] = {}
}
rootSchema[defKey][json[kRefToDef]] = json
}
})
}
return rootSchema
}
function collectIds (json, baseUri, relative) {
if (json[kIgnore]) { return }
const rel = (relative && URI.serialize(relative)) || ''
const id = URI.serialize(baseUri) + rel
if (!allIds.has(id)) {
debug('Collected $id %s', id)
json[kRefToDef] = `def-${rolling++}`
allIds.set(id, json)
} else {
debug('WARN duplicated id %s .. IGNORED - ', id)
}
}
function collectRefs (json, baseUri, refVal) {
const refUri = URI.parse(refVal)
debug('Pre enqueue $ref %o', refUri)
// "same-document";
// "relative";
// "absolute";
// "uri";
if (refUri.reference === 'relative') {
refUri.scheme = baseUri.scheme
refUri.userinfo = baseUri.userinfo
refUri.host = baseUri.host
refUri.port = baseUri.port
const newBaseUri = Object.assign({}, baseUri)
newBaseUri.path = refUri.path
baseUri = newBaseUri
} else if (refUri.reference === 'uri' || refUri.reference === 'absolute') {
baseUri = { ...refUri, fragment: undefined }
}
const ref = URI.serialize(refUri)
allRefs.push({
baseUri: URI.serialize(baseUri),
refUri,
ref,
json
})
debug('Enqueue $ref %s', ref)
}
}
/**
*
* @param {URI} baseUri
* @param {*} json
*/
function mapIds (ee, baseUri, json) {
if (!(json instanceof Object)) return
if (json.$id) {
const $idUri = URI.parse(json.$id)
let fragment = null
if ($idUri.reference === 'absolute') {
// "$id": "http://example.com/root.json"
baseUri = $idUri // a new baseURI for children
} else if ($idUri.reference === 'relative') {
// "$id": "other.json",
const newBaseUri = Object.assign({}, baseUri)
newBaseUri.path = $idUri.path
newBaseUri.fragment = $idUri.fragment
baseUri = newBaseUri
} else {
// { "$id": "#bar" }
fragment = $idUri
}
ee.emit('$id', json, baseUri, fragment)
}
// else if (json.$anchor) {
// TODO the $id should manage $anchor to support draft 08
// }
const fields = Object.keys(json)
for (const prop of fields) {
if (prop === '$ref') {
ee.emit('$ref', json, baseUri, json[prop])
}
mapIds(ee, baseUri, json[prop])
}
}
function getRootUri (strUri = 'application.uri') {
// If present, the value for this keyword MUST be a string, and MUST
// represent a valid URI-reference [RFC3986]. This value SHOULD be
// normalized, and SHOULD NOT be an empty fragment <#> or an empty
// string <>.
const uri = URI.parse(strUri)
uri.fragment = undefined
return uri
}
module.exports = jsonSchemaResolver