-
Notifications
You must be signed in to change notification settings - Fork 8
/
resolve.js
164 lines (142 loc) · 3.39 KB
/
resolve.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
function escapeJsonPointer(str) {
return str.replace(/~/g, "~0").replace(/\//g, "~1");
}
function unescapeJsonPointer(str) {
return str.replace(/~1/g, "/").replace(/~0/g, "~");
}
const isObject = (obj) => typeof obj === "object" && obj !== null;
const pointerWords = new Set([
"$ref",
"$id",
"$anchor",
"$dynamicRef",
"$dynamicAnchor",
"$schema",
]);
function resolveUri(uri, anchors) {
const [prefix, path] = uri.split("#", 2);
const hashPresent = !!path;
const err = new Error(
`Can't resolve ${uri}${
prefix ? ", only internal refs are supported." : ""
}`,
);
if (hashPresent && path[0] !== "/") {
if (anchors[uri]) {
return anchors[uri];
}
throw err;
}
if (!anchors[prefix]) {
throw err;
}
if (!hashPresent) {
return anchors[prefix];
}
const paths = path.split("/").slice(1);
try {
const result = paths.reduce(
(o, n) => o[unescapeJsonPointer(n)],
anchors[prefix],
);
if (result === undefined) {
throw "";
}
return result;
} catch (_) {
throw err;
}
}
export function replaceRefs(tree) {
return resolve(tree, true);
}
export function checkRefs(tree) {
try {
resolve(tree, false);
return { valid: true };
} catch (err) {
return { valid: false, errors: err.message };
}
}
function resolve(tree, replace) {
let treeObj = tree;
if (!isObject(treeObj)) {
return undefined;
}
if (replace === false) {
treeObj = structuredClone(tree);
}
const pointers = {};
for (const word of pointerWords) {
pointers[word] = [];
}
function applyRef(path, target) {
let root = treeObj;
const paths = path.split("/").slice(1);
const prop = paths.pop();
for (const p of paths) {
root = root[unescapeJsonPointer(p)];
}
if (typeof prop !== "undefined") {
root[unescapeJsonPointer(prop)] = target;
} else {
treeObj = target;
}
}
function parse(obj, path, id) {
if (!isObject(obj)) {
return;
}
const objId = obj.$id || id;
for (const prop in obj) {
if (pointerWords.has(prop)) {
pointers[prop].push({ ref: obj[prop], obj, prop, path, id: objId });
delete obj[prop];
}
parse(obj[prop], `${path}/${escapeJsonPointer(prop)}`, objId);
}
}
// find all refs
parse(treeObj, "#", "");
// resolve them
const anchors = { "": treeObj };
const dynamicAnchors = {};
for (const item of pointers.$id) {
const { ref, obj, path } = item;
if (anchors[ref]) {
throw new Error(`$id : '${ref}' defined more than once at ${path}`);
}
anchors[ref] = obj;
}
for (const item of pointers.$anchor) {
const { ref, obj, path, id } = item;
const fullRef = `${id}#${ref}`;
if (anchors[fullRef]) {
throw new Error(`$anchor : '${ref}' defined more than once at '${path}'`);
}
anchors[fullRef] = obj;
}
for (const item of pointers.$dynamicAnchor) {
const { ref, obj, path } = item;
if (dynamicAnchors[`#${ref}`]) {
throw new Error(
`$dynamicAnchor : '${ref}' defined more than once at '${path}'`,
);
}
dynamicAnchors[`#${ref}`] = obj;
}
for (const item of pointers.$ref) {
const { ref, id, path } = item;
const decodedRef = decodeURIComponent(ref);
const fullRef = decodedRef[0] !== "#" ? decodedRef : `${id}${decodedRef}`;
applyRef(path, resolveUri(fullRef, anchors));
}
for (const item of pointers.$dynamicRef) {
const { ref, path } = item;
if (!dynamicAnchors[ref]) {
throw new Error(`Can't resolve $dynamicAnchor : '${ref}'`);
}
applyRef(path, dynamicAnchors[ref]);
}
return treeObj;
}