-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
dereference.ts
190 lines (175 loc) Β· 5.12 KB
/
dereference.ts
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
import { encodePointer } from "./pointer.js";
import { Schema } from "./types.js";
export const schemaKeyword: Record<string, boolean> = {
additionalItems: true,
unevaluatedItems: true,
items: true,
contains: true,
additionalProperties: true,
unevaluatedProperties: true,
propertyNames: true,
not: true,
if: true,
then: true,
else: true,
};
export const schemaArrayKeyword: Record<string, boolean> = {
prefixItems: true,
items: true,
allOf: true,
anyOf: true,
oneOf: true,
};
export const schemaMapKeyword: Record<string, boolean> = {
$defs: true,
definitions: true,
properties: true,
patternProperties: true,
dependentSchemas: true,
};
export const ignoredKeyword: Record<string, boolean> = {
id: true,
$id: true,
$ref: true,
$schema: true,
$anchor: true,
$vocabulary: true,
$comment: true,
default: true,
enum: true,
const: true,
required: true,
type: true,
maximum: true,
minimum: true,
exclusiveMaximum: true,
exclusiveMinimum: true,
multipleOf: true,
maxLength: true,
minLength: true,
pattern: true,
format: true,
maxItems: true,
minItems: true,
uniqueItems: true,
maxProperties: true,
minProperties: true,
};
/**
* Default base URI for schemas without an $id.
* https://json-schema.org/draft/2019-09/json-schema-core.html#initial-base
* https://tools.ietf.org/html/rfc3986#section-5.1
*/
export let initialBaseURI =
// @ts-ignore
typeof self !== "undefined" &&
self.location &&
self.location.origin !== "null"
? //@ts-ignore
/* #__PURE__ */ new URL(
self.location.origin + self.location.pathname + location.search
)
: /* #__PURE__ */ new URL("https://github.com/cfworker");
export function dereference(
schema: Schema | boolean,
lookup: Record<string, Schema | boolean> = Object.create(null),
baseURI = initialBaseURI,
basePointer = ""
) {
if (schema && typeof schema === "object" && !Array.isArray(schema)) {
const id: string = schema.$id || schema.id;
if (id) {
const url = new URL(id, baseURI.href);
if (url.hash.length > 1) {
lookup[url.href] = schema;
} else {
url.hash = ""; // normalize hash https://url.spec.whatwg.org/#dom-url-hash
if (basePointer === "") {
baseURI = url;
} else {
dereference(schema, lookup, baseURI);
}
}
}
} else if (schema !== true && schema !== false) {
return lookup;
}
// compute the schema's URI and add it to the mapping.
const schemaURI = baseURI.href + (basePointer ? "#" + basePointer : "");
if (lookup[schemaURI] !== undefined) {
throw new Error(`Duplicate schema URI "${schemaURI}".`);
}
lookup[schemaURI] = schema;
// exit early if this is a boolean schema.
if (schema === true || schema === false) {
return lookup;
}
// set the schema's absolute URI.
if (schema.__absolute_uri__ === undefined) {
Object.defineProperty(schema, "__absolute_uri__", {
enumerable: false,
value: schemaURI,
});
}
// if a $ref is found, resolve it's absolute URI.
if (schema.$ref && schema.__absolute_ref__ === undefined) {
const url = new URL(schema.$ref, baseURI.href);
url.hash = url.hash; // normalize hash https://url.spec.whatwg.org/#dom-url-hash
Object.defineProperty(schema, "__absolute_ref__", {
enumerable: false,
value: url.href,
});
}
// if a $recursiveRef is found, resolve it's absolute URI.
if (schema.$recursiveRef && schema.__absolute_recursive_ref__ === undefined) {
const url = new URL(schema.$recursiveRef, baseURI.href);
url.hash = url.hash; // normalize hash https://url.spec.whatwg.org/#dom-url-hash
Object.defineProperty(schema, "__absolute_recursive_ref__", {
enumerable: false,
value: url.href,
});
}
// if an $anchor is found, compute it's URI and add it to the mapping.
if (schema.$anchor) {
const url = new URL("#" + schema.$anchor, baseURI.href);
lookup[url.href] = schema;
}
// process subschemas.
for (let key in schema) {
if (ignoredKeyword[key]) {
continue;
}
const keyBase = `${basePointer}/${encodePointer(key)}`;
const subSchema = schema[key];
if (Array.isArray(subSchema)) {
if (schemaArrayKeyword[key]) {
const length = subSchema.length;
for (let i = 0; i < length; i++) {
dereference(subSchema[i], lookup, baseURI, `${keyBase}/${i}`);
}
}
} else if (schemaMapKeyword[key]) {
for (let subKey in subSchema) {
dereference(
subSchema[subKey],
lookup,
baseURI,
`${keyBase}/${encodePointer(subKey)}`
);
}
} else {
dereference(subSchema, lookup, baseURI, keyBase);
}
}
return lookup;
}
// schema identification examples
// https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.appendix.A
// $ref delegation
// https://github.com/json-schema-org/json-schema-spec/issues/514
// output format
// https://json-schema.org/draft/2019-09/json-schema-core.html#output
// JSON pointer
// https://tools.ietf.org/html/rfc6901
// JSON relative pointer
// https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01