@apidevtools/json-schema-ref-parser 14.2.0 → 15.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -4
- package/dist/lib/bundle.d.ts +3 -3
- package/dist/lib/bundle.js +16 -54
- package/dist/lib/dereference.d.ts +1 -2
- package/dist/lib/dereference.js +12 -50
- package/dist/lib/index.js +42 -95
- package/dist/lib/normalize-args.d.ts +1 -1
- package/dist/lib/normalize-args.js +4 -7
- package/dist/lib/options.d.ts +6 -0
- package/dist/lib/options.js +16 -23
- package/dist/lib/parse.js +11 -46
- package/dist/lib/parsers/binary.js +1 -3
- package/dist/lib/parsers/json.js +4 -6
- package/dist/lib/parsers/text.js +3 -5
- package/dist/lib/parsers/yaml.js +12 -11
- package/dist/lib/pointer.d.ts +3 -2
- package/dist/lib/pointer.js +18 -63
- package/dist/lib/ref.d.ts +5 -3
- package/dist/lib/ref.js +60 -52
- package/dist/lib/refs.d.ts +1 -1
- package/dist/lib/refs.js +7 -46
- package/dist/lib/resolve-external.js +10 -48
- package/dist/lib/resolvers/file.js +7 -45
- package/dist/lib/resolvers/http.js +5 -40
- package/dist/lib/types/index.d.ts +1 -1
- package/dist/lib/types/index.js +1 -2
- package/dist/lib/util/convert-path-to-posix.js +3 -9
- package/dist/lib/util/errors.d.ts +1 -1
- package/dist/lib/util/errors.js +17 -33
- package/dist/lib/util/is-windows.js +1 -5
- package/dist/lib/util/maybe.js +4 -10
- package/dist/lib/util/next.js +1 -3
- package/dist/lib/util/plugins.js +4 -10
- package/dist/lib/util/url.d.ts +1 -1
- package/dist/lib/util/url.js +40 -88
- package/lib/bundle.ts +12 -9
- package/lib/dereference.ts +3 -4
- package/lib/normalize-args.ts +1 -1
- package/lib/options.ts +8 -0
- package/lib/parsers/yaml.ts +6 -1
- package/lib/pointer.ts +19 -20
- package/lib/ref.ts +55 -5
- package/lib/refs.ts +2 -2
- package/lib/types/index.ts +1 -1
- package/lib/util/errors.ts +2 -2
- package/lib/util/url.ts +13 -7
- package/package.json +21 -20
package/README.md
CHANGED
|
@@ -29,19 +29,15 @@ format. Maybe some of the files contain cross-references to each other.
|
|
|
29
29
|
{
|
|
30
30
|
"definitions": {
|
|
31
31
|
"person": {
|
|
32
|
-
// references an external file
|
|
33
32
|
"$ref": "schemas/people/Bruce-Wayne.json"
|
|
34
33
|
},
|
|
35
34
|
"place": {
|
|
36
|
-
// references a sub-schema in an external file
|
|
37
35
|
"$ref": "schemas/places.yaml#/definitions/Gotham-City"
|
|
38
36
|
},
|
|
39
37
|
"thing": {
|
|
40
|
-
// references a URL
|
|
41
38
|
"$ref": "http://wayne-enterprises.com/things/batmobile"
|
|
42
39
|
},
|
|
43
40
|
"color": {
|
|
44
|
-
// references a value in an external file via an internal reference
|
|
45
41
|
"$ref": "#/definitions/thing/properties/colors/black-as-the-night"
|
|
46
42
|
}
|
|
47
43
|
}
|
package/dist/lib/bundle.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type $RefParser from "./index";
|
|
2
|
-
import type { ParserOptions } from "./index";
|
|
3
|
-
import type { JSONSchema } from "./index";
|
|
1
|
+
import type $RefParser from "./index.js";
|
|
2
|
+
import type { ParserOptions } from "./index.js";
|
|
3
|
+
import type { JSONSchema } from "./index.js";
|
|
4
4
|
export interface InventoryEntry {
|
|
5
5
|
$ref: any;
|
|
6
6
|
parent: any;
|
package/dist/lib/bundle.js
CHANGED
|
@@ -1,44 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
const ref_js_1 = __importDefault(require("./ref.js"));
|
|
40
|
-
const pointer_js_1 = __importDefault(require("./pointer.js"));
|
|
41
|
-
const url = __importStar(require("./util/url.js"));
|
|
1
|
+
import $Ref from "./ref.js";
|
|
2
|
+
import Pointer from "./pointer.js";
|
|
3
|
+
import * as url from "./util/url.js";
|
|
42
4
|
/**
|
|
43
5
|
* Bundles all external JSON references into the main JSON schema, thus resulting in a schema that
|
|
44
6
|
* only has *internal* references, not any *external* references.
|
|
@@ -53,7 +15,7 @@ function bundle(parser, options) {
|
|
|
53
15
|
const inventory = [];
|
|
54
16
|
crawl(parser, "schema", parser.$refs._root$Ref.path + "#", "#", 0, inventory, parser.$refs, options);
|
|
55
17
|
// Remap all $ref pointers
|
|
56
|
-
remap(inventory);
|
|
18
|
+
remap(inventory, options);
|
|
57
19
|
}
|
|
58
20
|
/**
|
|
59
21
|
* Recursively crawls the given value, and inventories all JSON references.
|
|
@@ -72,7 +34,7 @@ function crawl(parent, key, path, pathFromRoot, indirections, inventory, $refs,
|
|
|
72
34
|
const bundleOptions = (options.bundle || {});
|
|
73
35
|
const isExcludedPath = bundleOptions.excludedPathMatcher || (() => false);
|
|
74
36
|
if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
|
|
75
|
-
if (
|
|
37
|
+
if ($Ref.isAllowed$Ref(obj)) {
|
|
76
38
|
inventory$Ref(parent, key, path, pathFromRoot, indirections, inventory, $refs, options);
|
|
77
39
|
}
|
|
78
40
|
else {
|
|
@@ -95,10 +57,10 @@ function crawl(parent, key, path, pathFromRoot, indirections, inventory, $refs,
|
|
|
95
57
|
}
|
|
96
58
|
});
|
|
97
59
|
for (const key of keys) {
|
|
98
|
-
const keyPath =
|
|
99
|
-
const keyPathFromRoot =
|
|
60
|
+
const keyPath = Pointer.join(path, key);
|
|
61
|
+
const keyPathFromRoot = Pointer.join(pathFromRoot, key);
|
|
100
62
|
const value = obj[key];
|
|
101
|
-
if (
|
|
63
|
+
if ($Ref.isAllowed$Ref(value)) {
|
|
102
64
|
inventory$Ref(obj, key, path, keyPathFromRoot, indirections, inventory, $refs, options);
|
|
103
65
|
}
|
|
104
66
|
else {
|
|
@@ -135,12 +97,12 @@ function inventory$Ref($refParent, $refKey, path, pathFromRoot, indirections, in
|
|
|
135
97
|
if (pointer === null) {
|
|
136
98
|
return;
|
|
137
99
|
}
|
|
138
|
-
const parsed =
|
|
100
|
+
const parsed = Pointer.parse(pathFromRoot);
|
|
139
101
|
const depth = parsed.length;
|
|
140
102
|
const file = url.stripHash(pointer.path);
|
|
141
103
|
const hash = url.getHash(pointer.path);
|
|
142
104
|
const external = file !== $refs._root$Ref.path;
|
|
143
|
-
const extended =
|
|
105
|
+
const extended = $Ref.isExtended$Ref($ref);
|
|
144
106
|
indirections += pointer.indirections;
|
|
145
107
|
const existingEntry = findInInventory(inventory, $refParent, $refKey);
|
|
146
108
|
if (existingEntry) {
|
|
@@ -193,7 +155,7 @@ function inventory$Ref($refParent, $refKey, path, pathFromRoot, indirections, in
|
|
|
193
155
|
*
|
|
194
156
|
* @param inventory
|
|
195
157
|
*/
|
|
196
|
-
function remap(inventory) {
|
|
158
|
+
function remap(inventory, options) {
|
|
197
159
|
// Group & sort all the $ref pointers, so they're in the order that we need to dereference/remap them
|
|
198
160
|
inventory.sort((a, b) => {
|
|
199
161
|
if (a.file !== b.file) {
|
|
@@ -243,12 +205,12 @@ function remap(inventory) {
|
|
|
243
205
|
entry.$ref.$ref = entry.hash;
|
|
244
206
|
}
|
|
245
207
|
else if (entry.file === file && entry.hash === hash) {
|
|
246
|
-
// This $ref points to the same value as the
|
|
208
|
+
// This $ref points to the same value as the previous $ref, so remap it to the same path
|
|
247
209
|
entry.$ref.$ref = pathFromRoot;
|
|
248
210
|
}
|
|
249
211
|
else if (entry.file === file && entry.hash.indexOf(hash + "/") === 0) {
|
|
250
|
-
// This $ref points to a sub-value of the
|
|
251
|
-
entry.$ref.$ref =
|
|
212
|
+
// This $ref points to a sub-value of the previous $ref, so remap it beneath that path
|
|
213
|
+
entry.$ref.$ref = Pointer.join(pathFromRoot, Pointer.parse(entry.hash.replace(hash, "#")));
|
|
252
214
|
}
|
|
253
215
|
else {
|
|
254
216
|
// We've moved to a new file or new hash
|
|
@@ -257,7 +219,7 @@ function remap(inventory) {
|
|
|
257
219
|
pathFromRoot = entry.pathFromRoot;
|
|
258
220
|
// This is the first $ref to point to this value, so dereference the value.
|
|
259
221
|
// Any other $refs that point to the same value will point to this $ref instead
|
|
260
|
-
entry.$ref = entry.parent[entry.key] =
|
|
222
|
+
entry.$ref = entry.parent[entry.key] = $Ref.dereference(entry.$ref, entry.value, options);
|
|
261
223
|
if (entry.circular) {
|
|
262
224
|
// This $ref points to itself
|
|
263
225
|
entry.$ref.$ref = entry.pathFromRoot;
|
|
@@ -299,4 +261,4 @@ function removeFromInventory(inventory, entry) {
|
|
|
299
261
|
const index = inventory.indexOf(entry);
|
|
300
262
|
inventory.splice(index, 1);
|
|
301
263
|
}
|
|
302
|
-
|
|
264
|
+
export default bundle;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { ParserOptions } from "./options.js";
|
|
2
|
-
import type
|
|
3
|
-
import type $RefParser from "./index";
|
|
2
|
+
import { type $RefParser, type JSONSchema } from "./index.js";
|
|
4
3
|
export default dereference;
|
|
5
4
|
/**
|
|
6
5
|
* Crawls the JSON schema, finds all JSON references, and dereferences them.
|
package/dist/lib/dereference.js
CHANGED
|
@@ -1,46 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
const ref_js_1 = __importDefault(require("./ref.js"));
|
|
40
|
-
const pointer_js_1 = __importDefault(require("./pointer.js"));
|
|
41
|
-
const url = __importStar(require("./util/url.js"));
|
|
42
|
-
const errors_1 = require("./util/errors");
|
|
43
|
-
exports.default = dereference;
|
|
1
|
+
import $Ref from "./ref.js";
|
|
2
|
+
import Pointer from "./pointer.js";
|
|
3
|
+
import * as url from "./util/url.js";
|
|
4
|
+
import { TimeoutError } from "./util/errors.js";
|
|
5
|
+
export default dereference;
|
|
44
6
|
/**
|
|
45
7
|
* Crawls the JSON schema, finds all JSON references, and dereferences them.
|
|
46
8
|
* This method mutates the JSON schema object, replacing JSON references with their resolved value.
|
|
@@ -82,7 +44,7 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
|
|
|
82
44
|
if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
|
|
83
45
|
parents.add(obj);
|
|
84
46
|
processedObjects.add(obj);
|
|
85
|
-
if (
|
|
47
|
+
if ($Ref.isAllowed$Ref(obj, options)) {
|
|
86
48
|
dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime);
|
|
87
49
|
result.circular = dereferenced.circular;
|
|
88
50
|
result.value = dereferenced.value;
|
|
@@ -90,14 +52,14 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
|
|
|
90
52
|
else {
|
|
91
53
|
for (const key of Object.keys(obj)) {
|
|
92
54
|
checkDereferenceTimeout(startTime, options);
|
|
93
|
-
const keyPath =
|
|
94
|
-
const keyPathFromRoot =
|
|
55
|
+
const keyPath = Pointer.join(path, key);
|
|
56
|
+
const keyPathFromRoot = Pointer.join(pathFromRoot, key);
|
|
95
57
|
if (isExcludedPath(keyPathFromRoot)) {
|
|
96
58
|
continue;
|
|
97
59
|
}
|
|
98
60
|
const value = obj[key];
|
|
99
61
|
let circular = false;
|
|
100
|
-
if (
|
|
62
|
+
if ($Ref.isAllowed$Ref(value, options)) {
|
|
101
63
|
dereferenced = dereference$Ref(value, keyPath, keyPathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime);
|
|
102
64
|
circular = dereferenced.circular;
|
|
103
65
|
// Avoid pointless mutations; breaks frozen objects to no profit
|
|
@@ -163,7 +125,7 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
|
|
|
163
125
|
* @returns
|
|
164
126
|
*/
|
|
165
127
|
function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime) {
|
|
166
|
-
const isExternalRef =
|
|
128
|
+
const isExternalRef = $Ref.isExternal$Ref($ref);
|
|
167
129
|
const shouldResolveOnCwd = isExternalRef && options?.dereference?.externalReferenceResolution === "root";
|
|
168
130
|
const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
|
|
169
131
|
const cache = dereferencedCache.get($refPath);
|
|
@@ -228,7 +190,7 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
|
|
|
228
190
|
foundCircularReference(path, $refs, options);
|
|
229
191
|
}
|
|
230
192
|
// Dereference the JSON reference
|
|
231
|
-
let dereferencedValue =
|
|
193
|
+
let dereferencedValue = $Ref.dereference($ref, pointer.value, options);
|
|
232
194
|
// Crawl the dereferenced value (unless it's circular)
|
|
233
195
|
if (!circular) {
|
|
234
196
|
// Determine if the dereferenced value is circular
|
|
@@ -264,7 +226,7 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
|
|
|
264
226
|
function checkDereferenceTimeout(startTime, options) {
|
|
265
227
|
if (options && options.timeoutMs) {
|
|
266
228
|
if (Date.now() - startTime > options.timeoutMs) {
|
|
267
|
-
throw new
|
|
229
|
+
throw new TimeoutError(options.timeoutMs);
|
|
268
230
|
}
|
|
269
231
|
}
|
|
270
232
|
}
|
package/dist/lib/index.js
CHANGED
|
@@ -1,74 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.isUnsafeUrl = exports.$Refs = exports.getJsonSchemaRefParserDefaultOptions = exports.jsonSchemaParserNormalizeArgs = exports.dereferenceInternal = exports.JSONParserErrorGroup = exports.isHandledError = exports.UnmatchedParserError = exports.ParserError = exports.ResolverError = exports.MissingPointerError = exports.InvalidPointerError = exports.JSONParserError = exports.UnmatchedResolverError = exports.dereference = exports.bundle = exports.resolve = exports.parse = exports.$RefParser = void 0;
|
|
40
|
-
const refs_js_1 = __importDefault(require("./refs.js"));
|
|
41
|
-
exports.$Refs = refs_js_1.default;
|
|
42
|
-
const parse_js_1 = __importDefault(require("./parse.js"));
|
|
43
|
-
const normalize_args_js_1 = __importDefault(require("./normalize-args.js"));
|
|
44
|
-
exports.jsonSchemaParserNormalizeArgs = normalize_args_js_1.default;
|
|
45
|
-
const resolve_external_js_1 = __importDefault(require("./resolve-external.js"));
|
|
46
|
-
const bundle_js_1 = __importDefault(require("./bundle.js"));
|
|
47
|
-
const dereference_js_1 = __importDefault(require("./dereference.js"));
|
|
48
|
-
exports.dereferenceInternal = dereference_js_1.default;
|
|
49
|
-
const url = __importStar(require("./util/url.js"));
|
|
50
|
-
const errors_js_1 = require("./util/errors.js");
|
|
51
|
-
Object.defineProperty(exports, "JSONParserError", { enumerable: true, get: function () { return errors_js_1.JSONParserError; } });
|
|
52
|
-
Object.defineProperty(exports, "InvalidPointerError", { enumerable: true, get: function () { return errors_js_1.InvalidPointerError; } });
|
|
53
|
-
Object.defineProperty(exports, "MissingPointerError", { enumerable: true, get: function () { return errors_js_1.MissingPointerError; } });
|
|
54
|
-
Object.defineProperty(exports, "ResolverError", { enumerable: true, get: function () { return errors_js_1.ResolverError; } });
|
|
55
|
-
Object.defineProperty(exports, "ParserError", { enumerable: true, get: function () { return errors_js_1.ParserError; } });
|
|
56
|
-
Object.defineProperty(exports, "UnmatchedParserError", { enumerable: true, get: function () { return errors_js_1.UnmatchedParserError; } });
|
|
57
|
-
Object.defineProperty(exports, "UnmatchedResolverError", { enumerable: true, get: function () { return errors_js_1.UnmatchedResolverError; } });
|
|
58
|
-
Object.defineProperty(exports, "isHandledError", { enumerable: true, get: function () { return errors_js_1.isHandledError; } });
|
|
59
|
-
Object.defineProperty(exports, "JSONParserErrorGroup", { enumerable: true, get: function () { return errors_js_1.JSONParserErrorGroup; } });
|
|
60
|
-
const maybe_js_1 = __importDefault(require("./util/maybe.js"));
|
|
61
|
-
const options_js_1 = require("./options.js");
|
|
62
|
-
Object.defineProperty(exports, "getJsonSchemaRefParserDefaultOptions", { enumerable: true, get: function () { return options_js_1.getJsonSchemaRefParserDefaultOptions; } });
|
|
63
|
-
const url_js_1 = require("./util/url.js");
|
|
64
|
-
Object.defineProperty(exports, "isUnsafeUrl", { enumerable: true, get: function () { return url_js_1.isUnsafeUrl; } });
|
|
1
|
+
import $Refs from "./refs.js";
|
|
2
|
+
import _parse from "./parse.js";
|
|
3
|
+
import normalizeArgs from "./normalize-args.js";
|
|
4
|
+
import resolveExternal from "./resolve-external.js";
|
|
5
|
+
import _bundle from "./bundle.js";
|
|
6
|
+
import _dereference from "./dereference.js";
|
|
7
|
+
import * as url from "./util/url.js";
|
|
8
|
+
import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup, } from "./util/errors.js";
|
|
9
|
+
import maybe from "./util/maybe.js";
|
|
10
|
+
import { getJsonSchemaRefParserDefaultOptions } from "./options.js";
|
|
11
|
+
import { isUnsafeUrl } from "./util/url.js";
|
|
65
12
|
/**
|
|
66
13
|
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,
|
|
67
14
|
* and provides methods for traversing, manipulating, and dereferencing those references.
|
|
68
15
|
*
|
|
69
16
|
* @class
|
|
70
17
|
*/
|
|
71
|
-
class $RefParser {
|
|
18
|
+
export class $RefParser {
|
|
72
19
|
/**
|
|
73
20
|
* The parsed (and possibly dereferenced) JSON schema object
|
|
74
21
|
*
|
|
@@ -82,17 +29,17 @@ class $RefParser {
|
|
|
82
29
|
* @type {$Refs}
|
|
83
30
|
* @readonly
|
|
84
31
|
*/
|
|
85
|
-
$refs = new
|
|
32
|
+
$refs = new $Refs();
|
|
86
33
|
async parse() {
|
|
87
|
-
const args = (
|
|
34
|
+
const args = normalizeArgs(arguments);
|
|
88
35
|
let promise;
|
|
89
36
|
if (!args.path && !args.schema) {
|
|
90
37
|
const err = new Error(`Expected a file path, URL, or object. Got ${args.path || args.schema}`);
|
|
91
|
-
return (
|
|
38
|
+
return maybe(args.callback, Promise.reject(err));
|
|
92
39
|
}
|
|
93
40
|
// Reset everything
|
|
94
41
|
this.schema = null;
|
|
95
|
-
this.$refs = new
|
|
42
|
+
this.$refs = new $Refs();
|
|
96
43
|
// If the path is a filesystem path, then convert it to a URL.
|
|
97
44
|
// NOTE: According to the JSON Reference spec, these should already be URLs,
|
|
98
45
|
// but, in practice, many people use local filesystem paths instead.
|
|
@@ -123,30 +70,30 @@ class $RefParser {
|
|
|
123
70
|
}
|
|
124
71
|
else {
|
|
125
72
|
// Parse the schema file/url
|
|
126
|
-
promise = (
|
|
73
|
+
promise = _parse(args.path, this.$refs, args.options);
|
|
127
74
|
}
|
|
128
75
|
try {
|
|
129
76
|
const result = await promise;
|
|
130
77
|
if (result !== null && typeof result === "object" && !Buffer.isBuffer(result)) {
|
|
131
78
|
this.schema = result;
|
|
132
|
-
return (
|
|
79
|
+
return maybe(args.callback, Promise.resolve(this.schema));
|
|
133
80
|
}
|
|
134
81
|
else if (args.options.continueOnError) {
|
|
135
82
|
this.schema = null; // it's already set to null at line 79, but let's set it again for the sake of readability
|
|
136
|
-
return (
|
|
83
|
+
return maybe(args.callback, Promise.resolve(this.schema));
|
|
137
84
|
}
|
|
138
85
|
else {
|
|
139
86
|
throw new SyntaxError(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
|
|
140
87
|
}
|
|
141
88
|
}
|
|
142
89
|
catch (err) {
|
|
143
|
-
if (!args.options.continueOnError || !
|
|
144
|
-
return (
|
|
90
|
+
if (!args.options.continueOnError || !isHandledError(err)) {
|
|
91
|
+
return maybe(args.callback, Promise.reject(err));
|
|
145
92
|
}
|
|
146
93
|
if (this.$refs._$refs[url.stripHash(args.path)]) {
|
|
147
94
|
this.$refs._$refs[url.stripHash(args.path)].addError(err);
|
|
148
95
|
}
|
|
149
|
-
return (
|
|
96
|
+
return maybe(args.callback, Promise.resolve(null));
|
|
150
97
|
}
|
|
151
98
|
}
|
|
152
99
|
static parse() {
|
|
@@ -154,15 +101,15 @@ class $RefParser {
|
|
|
154
101
|
return parser.parse.apply(parser, arguments);
|
|
155
102
|
}
|
|
156
103
|
async resolve() {
|
|
157
|
-
const args = (
|
|
104
|
+
const args = normalizeArgs(arguments);
|
|
158
105
|
try {
|
|
159
106
|
await this.parse(args.path, args.schema, args.options);
|
|
160
|
-
await (
|
|
107
|
+
await resolveExternal(this, args.options);
|
|
161
108
|
finalize(this);
|
|
162
|
-
return (
|
|
109
|
+
return maybe(args.callback, Promise.resolve(this.$refs));
|
|
163
110
|
}
|
|
164
111
|
catch (err) {
|
|
165
|
-
return (
|
|
112
|
+
return maybe(args.callback, Promise.reject(err));
|
|
166
113
|
}
|
|
167
114
|
}
|
|
168
115
|
static resolve() {
|
|
@@ -174,15 +121,15 @@ class $RefParser {
|
|
|
174
121
|
return instance.bundle.apply(instance, arguments);
|
|
175
122
|
}
|
|
176
123
|
async bundle() {
|
|
177
|
-
const args = (
|
|
124
|
+
const args = normalizeArgs(arguments);
|
|
178
125
|
try {
|
|
179
126
|
await this.resolve(args.path, args.schema, args.options);
|
|
180
|
-
(
|
|
127
|
+
_bundle(this, args.options);
|
|
181
128
|
finalize(this);
|
|
182
|
-
return (
|
|
129
|
+
return maybe(args.callback, Promise.resolve(this.schema));
|
|
183
130
|
}
|
|
184
131
|
catch (err) {
|
|
185
|
-
return (
|
|
132
|
+
return maybe(args.callback, Promise.reject(err));
|
|
186
133
|
}
|
|
187
134
|
}
|
|
188
135
|
static dereference() {
|
|
@@ -190,27 +137,27 @@ class $RefParser {
|
|
|
190
137
|
return instance.dereference.apply(instance, arguments);
|
|
191
138
|
}
|
|
192
139
|
async dereference() {
|
|
193
|
-
const args = (
|
|
140
|
+
const args = normalizeArgs(arguments);
|
|
194
141
|
try {
|
|
195
142
|
await this.resolve(args.path, args.schema, args.options);
|
|
196
|
-
(
|
|
143
|
+
_dereference(this, args.options);
|
|
197
144
|
finalize(this);
|
|
198
|
-
return (
|
|
145
|
+
return maybe(args.callback, Promise.resolve(this.schema));
|
|
199
146
|
}
|
|
200
147
|
catch (err) {
|
|
201
|
-
return (
|
|
148
|
+
return maybe(args.callback, Promise.reject(err));
|
|
202
149
|
}
|
|
203
150
|
}
|
|
204
151
|
}
|
|
205
|
-
|
|
206
|
-
exports.default = $RefParser;
|
|
152
|
+
export default $RefParser;
|
|
207
153
|
function finalize(parser) {
|
|
208
|
-
const errors =
|
|
154
|
+
const errors = JSONParserErrorGroup.getParserErrors(parser);
|
|
209
155
|
if (errors.length > 0) {
|
|
210
|
-
throw new
|
|
156
|
+
throw new JSONParserErrorGroup(parser);
|
|
211
157
|
}
|
|
212
158
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
159
|
+
export const parse = $RefParser.parse;
|
|
160
|
+
export const resolve = $RefParser.resolve;
|
|
161
|
+
export const bundle = $RefParser.bundle;
|
|
162
|
+
export const dereference = $RefParser.dereference;
|
|
163
|
+
export { UnmatchedResolverError, JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, isHandledError, JSONParserErrorGroup, _dereference as dereferenceInternal, normalizeArgs as jsonSchemaParserNormalizeArgs, getJsonSchemaRefParserDefaultOptions, $Refs, isUnsafeUrl, };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Options, ParserOptions } from "./options.js";
|
|
2
|
-
import type { JSONSchema, SchemaCallback } from "./
|
|
2
|
+
import type { JSONSchema, SchemaCallback } from "./index.js";
|
|
3
3
|
export interface NormalizedArguments<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
|
|
4
4
|
path: string;
|
|
5
5
|
schema: S;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizeArgs = normalizeArgs;
|
|
4
|
-
const options_js_1 = require("./options.js");
|
|
1
|
+
import { getNewOptions } from "./options.js";
|
|
5
2
|
/**
|
|
6
3
|
* Normalizes the given arguments, accounting for optional args.
|
|
7
4
|
*/
|
|
8
|
-
function normalizeArgs(_args) {
|
|
5
|
+
export function normalizeArgs(_args) {
|
|
9
6
|
let path;
|
|
10
7
|
let schema;
|
|
11
8
|
let options;
|
|
@@ -36,7 +33,7 @@ function normalizeArgs(_args) {
|
|
|
36
33
|
options = args[1];
|
|
37
34
|
}
|
|
38
35
|
try {
|
|
39
|
-
options =
|
|
36
|
+
options = getNewOptions(options);
|
|
40
37
|
}
|
|
41
38
|
catch (e) {
|
|
42
39
|
console.error(`JSON Schema Ref Parser: Error normalizing options: ${e}`);
|
|
@@ -52,4 +49,4 @@ function normalizeArgs(_args) {
|
|
|
52
49
|
callback,
|
|
53
50
|
};
|
|
54
51
|
}
|
|
55
|
-
|
|
52
|
+
export default normalizeArgs;
|
package/dist/lib/options.d.ts
CHANGED
|
@@ -64,6 +64,12 @@ export interface DereferenceOptions {
|
|
|
64
64
|
* Default: `relative`
|
|
65
65
|
*/
|
|
66
66
|
externalReferenceResolution?: "relative" | "root";
|
|
67
|
+
/**
|
|
68
|
+
* Whether duplicate keys should be merged when dereferencing objects.
|
|
69
|
+
*
|
|
70
|
+
* Default: `true`
|
|
71
|
+
*/
|
|
72
|
+
mergeKeys?: boolean;
|
|
67
73
|
}
|
|
68
74
|
/**
|
|
69
75
|
* Options that determine how JSON schemas are parsed, resolved, and dereferenced.
|
package/dist/lib/options.js
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const yaml_js_1 = __importDefault(require("./parsers/yaml.js"));
|
|
9
|
-
const text_js_1 = __importDefault(require("./parsers/text.js"));
|
|
10
|
-
const binary_js_1 = __importDefault(require("./parsers/binary.js"));
|
|
11
|
-
const file_js_1 = __importDefault(require("./resolvers/file.js"));
|
|
12
|
-
const http_js_1 = __importDefault(require("./resolvers/http.js"));
|
|
13
|
-
const getJsonSchemaRefParserDefaultOptions = () => {
|
|
1
|
+
import jsonParser from "./parsers/json.js";
|
|
2
|
+
import yamlParser from "./parsers/yaml.js";
|
|
3
|
+
import textParser from "./parsers/text.js";
|
|
4
|
+
import binaryParser from "./parsers/binary.js";
|
|
5
|
+
import fileResolver from "./resolvers/file.js";
|
|
6
|
+
import httpResolver from "./resolvers/http.js";
|
|
7
|
+
export const getJsonSchemaRefParserDefaultOptions = () => {
|
|
14
8
|
const defaults = {
|
|
15
9
|
/**
|
|
16
10
|
* Determines how different types of files will be parsed.
|
|
@@ -19,10 +13,10 @@ const getJsonSchemaRefParserDefaultOptions = () => {
|
|
|
19
13
|
* your own implementation, or disable any parser by setting it to false.
|
|
20
14
|
*/
|
|
21
15
|
parse: {
|
|
22
|
-
json: { ...
|
|
23
|
-
yaml: { ...
|
|
24
|
-
text: { ...
|
|
25
|
-
binary: { ...
|
|
16
|
+
json: { ...jsonParser },
|
|
17
|
+
yaml: { ...yamlParser },
|
|
18
|
+
text: { ...textParser },
|
|
19
|
+
binary: { ...binaryParser },
|
|
26
20
|
},
|
|
27
21
|
/**
|
|
28
22
|
* Determines how JSON References will be resolved.
|
|
@@ -31,8 +25,8 @@ const getJsonSchemaRefParserDefaultOptions = () => {
|
|
|
31
25
|
* your own implementation, or disable any resolver by setting it to false.
|
|
32
26
|
*/
|
|
33
27
|
resolve: {
|
|
34
|
-
file: { ...
|
|
35
|
-
http: { ...
|
|
28
|
+
file: { ...fileResolver },
|
|
29
|
+
http: { ...httpResolver },
|
|
36
30
|
/**
|
|
37
31
|
* Determines whether external $ref pointers will be resolved.
|
|
38
32
|
* If this option is disabled, then none of above resolvers will be called.
|
|
@@ -82,20 +76,19 @@ const getJsonSchemaRefParserDefaultOptions = () => {
|
|
|
82
76
|
*/
|
|
83
77
|
excludedPathMatcher: () => false,
|
|
84
78
|
referenceResolution: "relative",
|
|
79
|
+
mergeKeys: true,
|
|
85
80
|
},
|
|
86
81
|
mutateInputSchema: true,
|
|
87
82
|
};
|
|
88
83
|
return defaults;
|
|
89
84
|
};
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
const newOptions = (0, exports.getJsonSchemaRefParserDefaultOptions)();
|
|
85
|
+
export const getNewOptions = (options) => {
|
|
86
|
+
const newOptions = getJsonSchemaRefParserDefaultOptions();
|
|
93
87
|
if (options) {
|
|
94
88
|
merge(newOptions, options);
|
|
95
89
|
}
|
|
96
90
|
return newOptions;
|
|
97
91
|
};
|
|
98
|
-
exports.getNewOptions = getNewOptions;
|
|
99
92
|
/**
|
|
100
93
|
* Merges the properties of the source object into the target object.
|
|
101
94
|
*
|