|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const URI = require('uri-js') |
| 4 | +const cloner = require('rfdc')({ proto: true, circles: false }) |
| 5 | +const { EventEmitter } = require('events') |
| 6 | +const debug = require('debug')('json-schema-resolver') |
| 7 | + |
| 8 | +const kIgnore = Symbol('json-schema-resolver.ignore') // untrack a schema (usually the root one) |
| 9 | +const kRefToDef = Symbol('json-schema-resolver.refToDef') // assign to an external json a new reference |
| 10 | +const kConsumed = Symbol('json-schema-resolver.consumed') // when an external json has been referenced |
| 11 | + |
| 12 | +// ! Target: DRAFT-07 |
| 13 | +// https://tools.ietf.org/html/draft-handrews-json-schema-01 |
| 14 | + |
| 15 | +// ? Open to DRAFT 08 |
| 16 | +// https://json-schema.org/draft/2019-09/json-schema-core.html |
| 17 | + |
| 18 | +const defaultOpts = { |
| 19 | + target: 'draft-07', |
| 20 | + clone: false |
| 21 | +} |
| 22 | + |
| 23 | +const targetSupported = ['draft-07'] // TODO , 'draft-08' |
| 24 | +const targetCfg = { |
| 25 | + 'draft-07': { |
| 26 | + def: 'definitions' |
| 27 | + }, |
| 28 | + 'draft-08': { |
| 29 | + def: '$defs' |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// logic: https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.appendix.B.1 |
| 34 | +function jsonSchemaResolver (options) { |
| 35 | + const ee = new EventEmitter() |
| 36 | + const { clone, target } = Object.assign({}, defaultOpts, options) |
| 37 | + |
| 38 | + if (!targetSupported.includes(target)) { |
| 39 | + throw new Error(`Unsupported JSON schema version ${target}`) |
| 40 | + } |
| 41 | + |
| 42 | + const allIds = new Map() |
| 43 | + let rolling = 0 |
| 44 | + ee.on('$id', collectIds) |
| 45 | + |
| 46 | + const allRefs = [] |
| 47 | + ee.on('$ref', collectRefs) |
| 48 | + |
| 49 | + return { |
| 50 | + resolve |
| 51 | + } |
| 52 | + |
| 53 | + function resolve (rootSchema, opts) { |
| 54 | + const { externalSchemas } = opts || {} |
| 55 | + |
| 56 | + allIds.clear() |
| 57 | + allRefs.length = 0 |
| 58 | + |
| 59 | + if (clone) { |
| 60 | + rootSchema = cloner(rootSchema) |
| 61 | + } |
| 62 | + |
| 63 | + // If present, the value for this keyword MUST be a string, and MUST |
| 64 | + // represent a valid URI-reference [RFC3986]. This value SHOULD be |
| 65 | + // normalized, and SHOULD NOT be an empty fragment <#> or an empty |
| 66 | + // string <>. |
| 67 | + const appUri = URI.parse(rootSchema.$id) |
| 68 | + appUri.fragment = undefined // remove fragment |
| 69 | + debug('Found app URI %o', appUri) |
| 70 | + |
| 71 | + if (externalSchemas) { |
| 72 | + for (const es of externalSchemas) { mapIds(ee, appUri, es) } |
| 73 | + debug('Processed external schemas') |
| 74 | + } |
| 75 | + |
| 76 | + const baseUri = URI.serialize(appUri) // canonical absolute-URI |
| 77 | + rootSchema.$id = baseUri // fix the schema $id value |
| 78 | + rootSchema[kIgnore] = true |
| 79 | + |
| 80 | + mapIds(ee, appUri, rootSchema) |
| 81 | + debug('Processed root schema') |
| 82 | + |
| 83 | + debug('Generating %d refs', allRefs.length) |
| 84 | + allRefs.forEach(({ baseUri, ref, json }) => { |
| 85 | + debug('Evaluating $ref %s', ref) |
| 86 | + if (ref[0] === '#') { return } |
| 87 | + |
| 88 | + const evaluatedJson = allIds.get(baseUri) |
| 89 | + if (!evaluatedJson) { |
| 90 | + debug('External $ref %s not provided', ref) |
| 91 | + return |
| 92 | + } |
| 93 | + evaluatedJson[kConsumed] = true |
| 94 | + json.$ref = `#/definitions/${evaluatedJson[kRefToDef]}` |
| 95 | + }) |
| 96 | + |
| 97 | + const defKey = targetCfg[target].def |
| 98 | + allIds.forEach((json, baseUri) => { |
| 99 | + if (json[kConsumed] === true) { |
| 100 | + if (!rootSchema[defKey]) { |
| 101 | + rootSchema[defKey] = {} |
| 102 | + } |
| 103 | + |
| 104 | + rootSchema[defKey][json[kRefToDef]] = json |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + return rootSchema |
| 109 | + } |
| 110 | + |
| 111 | + function collectIds (json, baseUri, relative) { |
| 112 | + if (json[kIgnore]) { return } |
| 113 | + |
| 114 | + const rel = (relative && URI.serialize(relative)) || '' |
| 115 | + const id = URI.serialize(baseUri) + rel |
| 116 | + if (!allIds.has(id)) { |
| 117 | + debug('Collected $id %s', id) |
| 118 | + json[kRefToDef] = `def-${rolling++}` |
| 119 | + allIds.set(id, json) |
| 120 | + } else { |
| 121 | + debug('WARN duplicated id %s .. IGNORED - ', id) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + function collectRefs (json, baseUri, refVal) { |
| 126 | + const refUri = URI.parse(refVal) |
| 127 | + debug('Pre enqueue $ref %o', refUri) |
| 128 | + |
| 129 | + // "same-document"; |
| 130 | + // "relative"; |
| 131 | + // "absolute"; |
| 132 | + // "uri"; |
| 133 | + if (refUri.reference === 'relative') { |
| 134 | + refUri.scheme = baseUri.scheme |
| 135 | + refUri.userinfo = baseUri.userinfo |
| 136 | + refUri.host = baseUri.host |
| 137 | + refUri.port = baseUri.port |
| 138 | + |
| 139 | + const newBaseUri = Object.assign({}, baseUri) |
| 140 | + newBaseUri.path = refUri.path |
| 141 | + baseUri = newBaseUri |
| 142 | + } |
| 143 | + |
| 144 | + const ref = URI.serialize(refUri) |
| 145 | + allRefs.push({ |
| 146 | + baseUri: URI.serialize(baseUri), |
| 147 | + ref, |
| 148 | + json |
| 149 | + }) |
| 150 | + debug('Enqueue $ref %s', ref) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * |
| 156 | + * @param {URI} baseUri |
| 157 | + * @param {*} json |
| 158 | + */ |
| 159 | +function mapIds (ee, baseUri, json) { |
| 160 | + if (!(json instanceof Object)) return |
| 161 | + |
| 162 | + if (json.$id) { |
| 163 | + const $idUri = URI.parse(json.$id) |
| 164 | + let fragment = null |
| 165 | + |
| 166 | + if ($idUri.reference === 'absolute') { |
| 167 | + // "$id": "http://example.com/root.json" |
| 168 | + baseUri = $idUri // a new baseURI for children |
| 169 | + } else if ($idUri.reference === 'relative') { |
| 170 | + // "$id": "other.json", |
| 171 | + const newBaseUri = Object.assign({}, baseUri) |
| 172 | + newBaseUri.path = $idUri.path |
| 173 | + newBaseUri.fragment = $idUri.fragment |
| 174 | + baseUri = newBaseUri |
| 175 | + } else { |
| 176 | + // { "$id": "#bar" } |
| 177 | + fragment = $idUri |
| 178 | + } |
| 179 | + ee.emit('$id', json, baseUri, fragment) |
| 180 | + } |
| 181 | + // else if (json.$anchor) { |
| 182 | + // TODO the $id should manage $anchor to support draft 08 |
| 183 | + // } |
| 184 | + |
| 185 | + const fields = Object.keys(json) |
| 186 | + for (const prop of fields) { |
| 187 | + if (prop === '$ref') { |
| 188 | + ee.emit('$ref', json, baseUri, json[prop]) |
| 189 | + } |
| 190 | + mapIds(ee, baseUri, json[prop]) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +module.exports = jsonSchemaResolver |
0 commit comments