@apidevtools/json-schema-ref-parser 10.0.0 → 10.1.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/dist/lib/bundle.d.ts +10 -0
- package/dist/lib/bundle.js +265 -0
- package/dist/lib/dereference.d.ts +9 -0
- package/dist/lib/dereference.js +206 -0
- package/dist/lib/index.d.ts +206 -0
- package/dist/lib/index.js +223 -0
- package/dist/lib/normalize-args.d.ts +10 -0
- package/dist/lib/normalize-args.js +42 -0
- package/dist/lib/options.d.ts +77 -0
- package/dist/lib/options.js +119 -0
- package/dist/lib/parse.d.ts +8 -0
- package/dist/lib/parse.js +177 -0
- package/dist/lib/parsers/binary.d.ts +3 -0
- package/dist/lib/parsers/binary.js +35 -0
- package/dist/lib/parsers/json.d.ts +3 -0
- package/dist/lib/parsers/json.js +57 -0
- package/dist/lib/parsers/text.d.ts +3 -0
- package/dist/lib/parsers/text.js +42 -0
- package/dist/lib/parsers/yaml.d.ts +3 -0
- package/dist/lib/parsers/yaml.js +65 -0
- package/dist/lib/pointer.d.ts +87 -0
- package/dist/lib/pointer.js +256 -0
- package/dist/lib/ref.d.ts +181 -0
- package/dist/lib/ref.js +239 -0
- package/dist/lib/refs.d.ts +127 -0
- package/dist/lib/refs.js +223 -0
- package/dist/lib/resolve-external.d.ts +14 -0
- package/dist/lib/resolve-external.js +148 -0
- package/dist/lib/resolvers/file.d.ts +3 -0
- package/dist/lib/resolvers/file.js +76 -0
- package/dist/lib/resolvers/http.d.ts +3 -0
- package/dist/lib/resolvers/http.js +159 -0
- package/dist/lib/types/index.d.ts +104 -0
- package/dist/lib/types/index.js +2 -0
- package/dist/lib/util/errors.d.ts +50 -0
- package/dist/lib/util/errors.js +106 -0
- package/dist/lib/util/maybe.d.ts +3 -0
- package/dist/lib/util/maybe.js +24 -0
- package/dist/lib/util/next.d.ts +2 -0
- package/dist/lib/util/next.js +16 -0
- package/dist/lib/util/plugins.d.ts +36 -0
- package/dist/lib/util/plugins.js +144 -0
- package/dist/lib/util/url.d.ts +93 -0
- package/{cjs → dist/lib}/util/url.js +134 -102
- package/dist/vite.config.d.ts +2 -0
- package/dist/vite.config.js +23 -0
- package/lib/{bundle.js → bundle.ts} +105 -101
- package/lib/{dereference.js → dereference.ts} +113 -52
- package/lib/index.ts +413 -0
- package/lib/{normalize-args.js → normalize-args.ts} +7 -14
- package/lib/options.ts +202 -0
- package/lib/parse.ts +153 -0
- package/lib/parsers/binary.ts +39 -0
- package/lib/parsers/{json.js → json.ts} +9 -22
- package/lib/parsers/text.ts +46 -0
- package/lib/parsers/{yaml.js → yaml.ts} +15 -19
- package/lib/pointer.ts +296 -0
- package/lib/ref.ts +288 -0
- package/lib/refs.ts +238 -0
- package/lib/{resolve-external.js → resolve-external.ts} +39 -36
- package/lib/resolvers/file.ts +40 -0
- package/lib/resolvers/http.ts +136 -0
- package/lib/tsconfig.json +103 -0
- package/lib/types/index.ts +135 -0
- package/lib/util/errors.ts +141 -0
- package/lib/util/maybe.ts +22 -0
- package/lib/util/next.ts +13 -0
- package/lib/util/{plugins.js → plugins.ts} +58 -57
- package/lib/util/{url.js → url.ts} +64 -83
- package/package.json +44 -45
- package/cjs/bundle.js +0 -304
- package/cjs/dereference.js +0 -258
- package/cjs/index.js +0 -603
- package/cjs/normalize-args.js +0 -64
- package/cjs/options.js +0 -125
- package/cjs/package.json +0 -3
- package/cjs/parse.js +0 -338
- package/cjs/parsers/binary.js +0 -54
- package/cjs/parsers/json.js +0 -199
- package/cjs/parsers/text.js +0 -61
- package/cjs/parsers/yaml.js +0 -239
- package/cjs/pointer.js +0 -290
- package/cjs/ref.js +0 -333
- package/cjs/refs.js +0 -214
- package/cjs/resolve-external.js +0 -333
- package/cjs/resolvers/file.js +0 -106
- package/cjs/resolvers/http.js +0 -184
- package/cjs/util/errors.js +0 -401
- package/cjs/util/plugins.js +0 -159
- package/cjs/util/projectDir.cjs +0 -6
- package/lib/index.d.ts +0 -496
- package/lib/index.js +0 -290
- package/lib/options.js +0 -128
- package/lib/parse.js +0 -162
- package/lib/parsers/binary.js +0 -53
- package/lib/parsers/text.js +0 -64
- package/lib/pointer.js +0 -293
- package/lib/ref.js +0 -292
- package/lib/refs.js +0 -196
- package/lib/resolvers/file.js +0 -63
- package/lib/resolvers/http.js +0 -155
- package/lib/util/errors.js +0 -134
- package/lib/util/projectDir.cjs +0 -6
package/lib/ref.ts
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import Pointer from "./pointer.js";
|
|
2
|
+
import type { JSONParserError, MissingPointerError, ParserError, ResolverError } from "./util/errors.js";
|
|
3
|
+
import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js";
|
|
4
|
+
import { safePointerToPath, stripHash, getHash } from "./util/url.js";
|
|
5
|
+
import type $Refs from "./refs.js";
|
|
6
|
+
import type $RefParserOptions from "./options.js";
|
|
7
|
+
import type { JSONSchema } from "./types";
|
|
8
|
+
|
|
9
|
+
type $RefError = JSONParserError | ResolverError | ParserError | MissingPointerError;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* This class represents a single JSON reference and its resolved value.
|
|
13
|
+
*
|
|
14
|
+
* @class
|
|
15
|
+
*/
|
|
16
|
+
class $Ref {
|
|
17
|
+
/**
|
|
18
|
+
* The file path or URL of the referenced file.
|
|
19
|
+
* This path is relative to the path of the main JSON schema file.
|
|
20
|
+
*
|
|
21
|
+
* This path does NOT contain document fragments (JSON pointers). It always references an ENTIRE file.
|
|
22
|
+
* Use methods such as {@link $Ref#get}, {@link $Ref#resolve}, and {@link $Ref#exists} to get
|
|
23
|
+
* specific JSON pointers within the file.
|
|
24
|
+
*
|
|
25
|
+
* @type {string}
|
|
26
|
+
*/
|
|
27
|
+
path: undefined | string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The resolved value of the JSON reference.
|
|
31
|
+
* Can be any JSON type, not just objects. Unknown file types are represented as Buffers (byte arrays).
|
|
32
|
+
*
|
|
33
|
+
* @type {?*}
|
|
34
|
+
*/
|
|
35
|
+
value: any;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The {@link $Refs} object that contains this {@link $Ref} object.
|
|
39
|
+
*
|
|
40
|
+
* @type {$Refs}
|
|
41
|
+
*/
|
|
42
|
+
$refs: $Refs;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Indicates the type of {@link $Ref#path} (e.g. "file", "http", etc.)
|
|
46
|
+
*/
|
|
47
|
+
pathType: string | unknown;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* List of all errors. Undefined if no errors.
|
|
51
|
+
*/
|
|
52
|
+
errors: Array<$RefError> = [];
|
|
53
|
+
|
|
54
|
+
constructor($refs: $Refs) {
|
|
55
|
+
this.$refs = $refs;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Pushes an error to errors array.
|
|
60
|
+
*
|
|
61
|
+
* @param err - The error to be pushed
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
addError(err: $RefError) {
|
|
65
|
+
if (this.errors === undefined) {
|
|
66
|
+
this.errors = [];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const existingErrors = this.errors.map(({ footprint }: any) => footprint);
|
|
70
|
+
|
|
71
|
+
// the path has been almost certainly set at this point,
|
|
72
|
+
// but just in case something went wrong, normalizeError injects path if necessary
|
|
73
|
+
// moreover, certain errors might point at the same spot, so filter them out to reduce noise
|
|
74
|
+
if ("errors" in err && Array.isArray(err.errors)) {
|
|
75
|
+
this.errors.push(
|
|
76
|
+
...err.errors.map(normalizeError).filter(({ footprint }: any) => !existingErrors.includes(footprint)),
|
|
77
|
+
);
|
|
78
|
+
} else if (!("footprint" in err) || !existingErrors.includes(err.footprint)) {
|
|
79
|
+
this.errors.push(normalizeError(err));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Determines whether the given JSON reference exists within this {@link $Ref#value}.
|
|
85
|
+
*
|
|
86
|
+
* @param path - The full path being resolved, optionally with a JSON pointer in the hash
|
|
87
|
+
* @param options
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
exists(path: string, options: any) {
|
|
91
|
+
try {
|
|
92
|
+
this.resolve(path, options);
|
|
93
|
+
return true;
|
|
94
|
+
} catch (e) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Resolves the given JSON reference within this {@link $Ref#value} and returns the resolved value.
|
|
101
|
+
*
|
|
102
|
+
* @param path - The full path being resolved, optionally with a JSON pointer in the hash
|
|
103
|
+
* @param options
|
|
104
|
+
* @returns - Returns the resolved value
|
|
105
|
+
*/
|
|
106
|
+
get(path: any, options: any) {
|
|
107
|
+
return this.resolve(path, options)?.value;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Resolves the given JSON reference within this {@link $Ref#value}.
|
|
112
|
+
*
|
|
113
|
+
* @param path - The full path being resolved, optionally with a JSON pointer in the hash
|
|
114
|
+
* @param options
|
|
115
|
+
* @param friendlyPath - The original user-specified path (used for error messages)
|
|
116
|
+
* @param pathFromRoot - The path of `obj` from the schema root
|
|
117
|
+
* @returns
|
|
118
|
+
*/
|
|
119
|
+
resolve(path: any, options?: $RefParserOptions, friendlyPath?: string, pathFromRoot?: string) {
|
|
120
|
+
const pointer = new Pointer(this, path, friendlyPath);
|
|
121
|
+
try {
|
|
122
|
+
return pointer.resolve(this.value, options, pathFromRoot);
|
|
123
|
+
} catch (err: any) {
|
|
124
|
+
if (!options || !options.continueOnError || !isHandledError(err)) {
|
|
125
|
+
throw err;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (err.path === null) {
|
|
129
|
+
err.path = safePointerToPath(getHash(pathFromRoot));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (err instanceof InvalidPointerError) {
|
|
133
|
+
err.source = decodeURI(stripHash(pathFromRoot));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.addError(err);
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Sets the value of a nested property within this {@link $Ref#value}.
|
|
143
|
+
* If the property, or any of its parents don't exist, they will be created.
|
|
144
|
+
*
|
|
145
|
+
* @param path - The full path of the property to set, optionally with a JSON pointer in the hash
|
|
146
|
+
* @param value - The value to assign
|
|
147
|
+
*/
|
|
148
|
+
set(path: any, value: any) {
|
|
149
|
+
// @ts-expect-error TS(2554): Expected 3 arguments, but got 2.
|
|
150
|
+
const pointer = new Pointer(this, path);
|
|
151
|
+
this.value = pointer.set(this.value, value);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Determines whether the given value is a JSON reference.
|
|
156
|
+
*
|
|
157
|
+
* @param value - The value to inspect
|
|
158
|
+
* @returns
|
|
159
|
+
*/
|
|
160
|
+
static is$Ref(value: any): value is { $ref: string; length?: number } {
|
|
161
|
+
return value && typeof value === "object" && typeof value.$ref === "string" && value.$ref.length > 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Determines whether the given value is an external JSON reference.
|
|
166
|
+
*
|
|
167
|
+
* @param value - The value to inspect
|
|
168
|
+
* @returns
|
|
169
|
+
*/
|
|
170
|
+
static isExternal$Ref(value: any): value is JSONSchema {
|
|
171
|
+
return $Ref.is$Ref(value) && value.$ref![0] !== "#";
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Determines whether the given value is a JSON reference, and whether it is allowed by the options.
|
|
176
|
+
* For example, if it references an external file, then options.resolve.external must be true.
|
|
177
|
+
*
|
|
178
|
+
* @param value - The value to inspect
|
|
179
|
+
* @param options
|
|
180
|
+
* @returns
|
|
181
|
+
*/
|
|
182
|
+
static isAllowed$Ref(value: any, options: any) {
|
|
183
|
+
if (this.is$Ref(value)) {
|
|
184
|
+
if (value.$ref.substring(0, 2) === "#/" || value.$ref === "#") {
|
|
185
|
+
// It's a JSON Pointer reference, which is always allowed
|
|
186
|
+
return true;
|
|
187
|
+
} else if (value.$ref[0] !== "#" && (!options || options.resolve.external)) {
|
|
188
|
+
// It's an external reference, which is allowed by the options
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Determines whether the given value is a JSON reference that "extends" its resolved value.
|
|
196
|
+
* That is, it has extra properties (in addition to "$ref"), so rather than simply pointing to
|
|
197
|
+
* an existing value, this $ref actually creates a NEW value that is a shallow copy of the resolved
|
|
198
|
+
* value, plus the extra properties.
|
|
199
|
+
*
|
|
200
|
+
* @example: {
|
|
201
|
+
person: {
|
|
202
|
+
properties: {
|
|
203
|
+
firstName: { type: string }
|
|
204
|
+
lastName: { type: string }
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
employee: {
|
|
208
|
+
properties: {
|
|
209
|
+
$ref: #/person/properties
|
|
210
|
+
salary: { type: number }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
* In this example, "employee" is an extended $ref, since it extends "person" with an additional
|
|
215
|
+
* property (salary). The result is a NEW value that looks like this:
|
|
216
|
+
*
|
|
217
|
+
* {
|
|
218
|
+
* properties: {
|
|
219
|
+
* firstName: { type: string }
|
|
220
|
+
* lastName: { type: string }
|
|
221
|
+
* salary: { type: number }
|
|
222
|
+
* }
|
|
223
|
+
* }
|
|
224
|
+
*
|
|
225
|
+
* @param value - The value to inspect
|
|
226
|
+
* @returns
|
|
227
|
+
*/
|
|
228
|
+
static isExtended$Ref(value: any) {
|
|
229
|
+
return $Ref.is$Ref(value) && Object.keys(value).length > 1;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Returns the resolved value of a JSON Reference.
|
|
234
|
+
* If necessary, the resolved value is merged with the JSON Reference to create a new object
|
|
235
|
+
*
|
|
236
|
+
* @example: {
|
|
237
|
+
person: {
|
|
238
|
+
properties: {
|
|
239
|
+
firstName: { type: string }
|
|
240
|
+
lastName: { type: string }
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
employee: {
|
|
244
|
+
properties: {
|
|
245
|
+
$ref: #/person/properties
|
|
246
|
+
salary: { type: number }
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} When "person" and "employee" are merged, you end up with the following object:
|
|
250
|
+
*
|
|
251
|
+
* {
|
|
252
|
+
* properties: {
|
|
253
|
+
* firstName: { type: string }
|
|
254
|
+
* lastName: { type: string }
|
|
255
|
+
* salary: { type: number }
|
|
256
|
+
* }
|
|
257
|
+
* }
|
|
258
|
+
*
|
|
259
|
+
* @param $ref - The JSON reference object (the one with the "$ref" property)
|
|
260
|
+
* @param resolvedValue - The resolved value, which can be any type
|
|
261
|
+
* @returns - Returns the dereferenced value
|
|
262
|
+
*/
|
|
263
|
+
static dereference($ref: $Ref, resolvedValue: any) {
|
|
264
|
+
if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) {
|
|
265
|
+
const merged = {};
|
|
266
|
+
for (const key of Object.keys($ref)) {
|
|
267
|
+
if (key !== "$ref") {
|
|
268
|
+
// @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
|
|
269
|
+
merged[key] = $ref[key];
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
for (const key of Object.keys(resolvedValue)) {
|
|
274
|
+
if (!(key in merged)) {
|
|
275
|
+
// @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
|
|
276
|
+
merged[key] = resolvedValue[key];
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return merged;
|
|
281
|
+
} else {
|
|
282
|
+
// Completely replace the original reference with the resolved value
|
|
283
|
+
return resolvedValue;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export default $Ref;
|
package/lib/refs.ts
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { ono } from "@jsdevtools/ono";
|
|
2
|
+
import $Ref from "./ref.js";
|
|
3
|
+
import * as url from "./util/url.js";
|
|
4
|
+
import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
|
|
5
|
+
import type { JSONSchema } from "./types/index.js";
|
|
6
|
+
import type $RefParserOptions from "./options.js";
|
|
7
|
+
|
|
8
|
+
const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : "");
|
|
9
|
+
const getPathFromOs = (filePath: string): string => (isWindows ? filePath.replace(/\\/g, "/") : filePath);
|
|
10
|
+
|
|
11
|
+
interface $RefsMap {
|
|
12
|
+
[url: string]: $Ref;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* When you call the resolve method, the value that gets passed to the callback function (or Promise) is a $Refs object. This same object is accessible via the parser.$refs property of $RefParser objects.
|
|
16
|
+
*
|
|
17
|
+
* This object is a map of JSON References and their resolved values. It also has several convenient helper methods that make it easy for you to navigate and manipulate the JSON References.
|
|
18
|
+
*
|
|
19
|
+
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html
|
|
20
|
+
*/
|
|
21
|
+
export default class $Refs {
|
|
22
|
+
/**
|
|
23
|
+
* This property is true if the schema contains any circular references. You may want to check this property before serializing the dereferenced schema as JSON, since JSON.stringify() does not support circular references by default.
|
|
24
|
+
*
|
|
25
|
+
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html#circular
|
|
26
|
+
*/
|
|
27
|
+
public circular: boolean;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns the paths/URLs of all the files in your schema (including the main schema file).
|
|
31
|
+
*
|
|
32
|
+
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html#pathstypes
|
|
33
|
+
*
|
|
34
|
+
* @param types (optional) Optionally only return certain types of paths ("file", "http", etc.)
|
|
35
|
+
*/
|
|
36
|
+
paths(...types: string[]): string[] {
|
|
37
|
+
const paths = getPaths(this._$refs, types);
|
|
38
|
+
return paths.map((path) => {
|
|
39
|
+
return getPathFromOs(path.decoded);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns a map of paths/URLs and their correspond values.
|
|
45
|
+
*
|
|
46
|
+
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html#valuestypes
|
|
47
|
+
*
|
|
48
|
+
* @param types (optional) Optionally only return values from certain locations ("file", "http", etc.)
|
|
49
|
+
*/
|
|
50
|
+
values(...types: string[]): JSONSchema {
|
|
51
|
+
const $refs = this._$refs;
|
|
52
|
+
const paths = getPaths($refs, types);
|
|
53
|
+
return paths.reduce<Record<string, any>>((obj, path) => {
|
|
54
|
+
obj[getPathFromOs(path.decoded)] = $refs[path.encoded].value;
|
|
55
|
+
return obj;
|
|
56
|
+
}, {});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Returns `true` if the given path exists in the schema; otherwise, returns `false`
|
|
61
|
+
*
|
|
62
|
+
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html#existsref
|
|
63
|
+
*
|
|
64
|
+
* @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
|
|
65
|
+
*/
|
|
66
|
+
/**
|
|
67
|
+
* Determines whether the given JSON reference exists.
|
|
68
|
+
*
|
|
69
|
+
* @param path - The path being resolved, optionally with a JSON pointer in the hash
|
|
70
|
+
* @param [options]
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
exists(path: string, options: any) {
|
|
74
|
+
try {
|
|
75
|
+
this._resolve(path, "", options);
|
|
76
|
+
return true;
|
|
77
|
+
} catch (e) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Resolves the given JSON reference and returns the resolved value.
|
|
84
|
+
*
|
|
85
|
+
* @param path - The path being resolved, with a JSON pointer in the hash
|
|
86
|
+
* @param [options]
|
|
87
|
+
* @returns - Returns the resolved value
|
|
88
|
+
*/
|
|
89
|
+
get(path: string, options?: $RefParserOptions): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type {
|
|
90
|
+
return this._resolve(path, "", options)!.value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Sets the value at the given path in the schema. If the property, or any of its parents, don't exist, they will be created.
|
|
95
|
+
*
|
|
96
|
+
* @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
|
|
97
|
+
* @param value The value to assign. Can be anything (object, string, number, etc.)
|
|
98
|
+
*/
|
|
99
|
+
set(path: any, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type) {
|
|
100
|
+
const absPath = url.resolve(this._root$Ref.path, path);
|
|
101
|
+
const withoutHash = url.stripHash(absPath);
|
|
102
|
+
const $ref = this._$refs[withoutHash];
|
|
103
|
+
|
|
104
|
+
if (!$ref) {
|
|
105
|
+
throw ono(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
$ref.set(absPath, value);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Returns the specified {@link $Ref} object, or undefined.
|
|
112
|
+
*
|
|
113
|
+
* @param path - The path being resolved, optionally with a JSON pointer in the hash
|
|
114
|
+
* @returns
|
|
115
|
+
* @protected
|
|
116
|
+
*/
|
|
117
|
+
_get$Ref(path: any) {
|
|
118
|
+
path = url.resolve(this._root$Ref.path, path);
|
|
119
|
+
const withoutHash = url.stripHash(path);
|
|
120
|
+
return this._$refs[withoutHash];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Creates a new {@link $Ref} object and adds it to this {@link $Refs} object.
|
|
125
|
+
*
|
|
126
|
+
* @param path - The file path or URL of the referenced file
|
|
127
|
+
*/
|
|
128
|
+
_add(path: string) {
|
|
129
|
+
const withoutHash = url.stripHash(path);
|
|
130
|
+
|
|
131
|
+
const $ref = new $Ref(this);
|
|
132
|
+
$ref.path = withoutHash;
|
|
133
|
+
|
|
134
|
+
this._$refs[withoutHash] = $ref;
|
|
135
|
+
this._root$Ref = this._root$Ref || $ref;
|
|
136
|
+
|
|
137
|
+
return $ref;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Resolves the given JSON reference.
|
|
142
|
+
*
|
|
143
|
+
* @param path - The path being resolved, optionally with a JSON pointer in the hash
|
|
144
|
+
* @param pathFromRoot - The path of `obj` from the schema root
|
|
145
|
+
* @param [options]
|
|
146
|
+
* @returns
|
|
147
|
+
* @protected
|
|
148
|
+
*/
|
|
149
|
+
_resolve(path: string, pathFromRoot: string, options?: any) {
|
|
150
|
+
const absPath = url.resolve(this._root$Ref.path, path);
|
|
151
|
+
const withoutHash = url.stripHash(absPath);
|
|
152
|
+
const $ref = this._$refs[withoutHash];
|
|
153
|
+
|
|
154
|
+
if (!$ref) {
|
|
155
|
+
throw ono(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return $ref.resolve(absPath, options, path, pathFromRoot);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* A map of paths/urls to {@link $Ref} objects
|
|
163
|
+
*
|
|
164
|
+
* @type {object}
|
|
165
|
+
* @protected
|
|
166
|
+
*/
|
|
167
|
+
_$refs: $RefsMap = {};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The {@link $Ref} object that is the root of the JSON schema.
|
|
171
|
+
*
|
|
172
|
+
* @type {$Ref}
|
|
173
|
+
* @protected
|
|
174
|
+
*/
|
|
175
|
+
_root$Ref: $Ref;
|
|
176
|
+
|
|
177
|
+
constructor() {
|
|
178
|
+
/**
|
|
179
|
+
* Indicates whether the schema contains any circular references.
|
|
180
|
+
*
|
|
181
|
+
* @type {boolean}
|
|
182
|
+
*/
|
|
183
|
+
this.circular = false;
|
|
184
|
+
|
|
185
|
+
this._$refs = {};
|
|
186
|
+
|
|
187
|
+
// @ts-ignore
|
|
188
|
+
this._root$Ref = null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Returns the paths of all the files/URLs that are referenced by the JSON schema,
|
|
193
|
+
* including the schema itself.
|
|
194
|
+
*
|
|
195
|
+
* @param [types] - Only return paths of the given types ("file", "http", etc.)
|
|
196
|
+
* @returns
|
|
197
|
+
*/
|
|
198
|
+
/**
|
|
199
|
+
* Returns the map of JSON references and their resolved values.
|
|
200
|
+
*
|
|
201
|
+
* @param [types] - Only return references of the given types ("file", "http", etc.)
|
|
202
|
+
* @returns
|
|
203
|
+
*/
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Returns a POJO (plain old JavaScript object) for serialization as JSON.
|
|
207
|
+
*
|
|
208
|
+
* @returns {object}
|
|
209
|
+
*/
|
|
210
|
+
toJSON = this.values;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Returns the encoded and decoded paths keys of the given object.
|
|
215
|
+
*
|
|
216
|
+
* @param $refs - The object whose keys are URL-encoded paths
|
|
217
|
+
* @param [types] - Only return paths of the given types ("file", "http", etc.)
|
|
218
|
+
* @returns
|
|
219
|
+
*/
|
|
220
|
+
function getPaths($refs: $RefsMap, types: string[]) {
|
|
221
|
+
let paths = Object.keys($refs);
|
|
222
|
+
|
|
223
|
+
// Filter the paths by type
|
|
224
|
+
types = Array.isArray(types[0]) ? types[0] : Array.prototype.slice.call(types);
|
|
225
|
+
if (types.length > 0 && types[0]) {
|
|
226
|
+
paths = paths.filter((key) => {
|
|
227
|
+
return types.includes($refs[key].pathType as string);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Decode local filesystem paths
|
|
232
|
+
return paths.map((path) => {
|
|
233
|
+
return {
|
|
234
|
+
encoded: path,
|
|
235
|
+
decoded: $refs[path].pathType === "file" ? url.toFileSystemPath(path, true) : path,
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
}
|
|
@@ -3,6 +3,10 @@ import Pointer from "./pointer.js";
|
|
|
3
3
|
import parse from "./parse.js";
|
|
4
4
|
import * as url from "./util/url.js";
|
|
5
5
|
import { isHandledError } from "./util/errors.js";
|
|
6
|
+
import type $Refs from "./refs.js";
|
|
7
|
+
import type { Options } from "./options.js";
|
|
8
|
+
import type { JSONSchema } from "./types/index.js";
|
|
9
|
+
import type $RefParser from "./index.js";
|
|
6
10
|
|
|
7
11
|
export default resolveExternal;
|
|
8
12
|
|
|
@@ -12,14 +16,11 @@ export default resolveExternal;
|
|
|
12
16
|
*
|
|
13
17
|
* NOTE: We only care about EXTERNAL references here. INTERNAL references are only relevant when dereferencing.
|
|
14
18
|
*
|
|
15
|
-
* @
|
|
16
|
-
* @param {$RefParserOptions} options
|
|
17
|
-
*
|
|
18
|
-
* @returns {Promise}
|
|
19
|
+
* @returns
|
|
19
20
|
* The promise resolves once all JSON references in the schema have been resolved,
|
|
20
21
|
* including nested references that are contained in externally-referenced files.
|
|
21
22
|
*/
|
|
22
|
-
function resolveExternal
|
|
23
|
+
function resolveExternal(parser: $RefParser, options: Options) {
|
|
23
24
|
if (!options.resolve.external) {
|
|
24
25
|
// Nothing to resolve, so exit early
|
|
25
26
|
return Promise.resolve();
|
|
@@ -27,10 +28,9 @@ function resolveExternal (parser, options) {
|
|
|
27
28
|
|
|
28
29
|
try {
|
|
29
30
|
// console.log('Resolving $ref pointers in %s', parser.$refs._root$Ref.path);
|
|
30
|
-
|
|
31
|
+
const promises = crawl(parser.schema, parser.$refs._root$Ref.path + "#", parser.$refs, options);
|
|
31
32
|
return Promise.all(promises);
|
|
32
|
-
}
|
|
33
|
-
catch (e) {
|
|
33
|
+
} catch (e) {
|
|
34
34
|
return Promise.reject(e);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -38,36 +38,40 @@ function resolveExternal (parser, options) {
|
|
|
38
38
|
/**
|
|
39
39
|
* Recursively crawls the given value, and resolves any external JSON references.
|
|
40
40
|
*
|
|
41
|
-
* @param
|
|
42
|
-
* @param
|
|
43
|
-
* @param
|
|
44
|
-
* @param
|
|
45
|
-
* @param
|
|
41
|
+
* @param obj - The value to crawl. If it's not an object or array, it will be ignored.
|
|
42
|
+
* @param path - The full path of `obj`, possibly with a JSON Pointer in the hash
|
|
43
|
+
* @param $refs
|
|
44
|
+
* @param options
|
|
45
|
+
* @param seen - Internal.
|
|
46
46
|
*
|
|
47
|
-
* @returns
|
|
47
|
+
* @returns
|
|
48
48
|
* Returns an array of promises. There will be one promise for each JSON reference in `obj`.
|
|
49
49
|
* If `obj` does not contain any JSON references, then the array will be empty.
|
|
50
50
|
* If any of the JSON references point to files that contain additional JSON references,
|
|
51
51
|
* then the corresponding promise will internally reference an array of promises.
|
|
52
52
|
*/
|
|
53
|
-
function crawl
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
function crawl(
|
|
54
|
+
obj: string | Buffer | JSONSchema | undefined | null,
|
|
55
|
+
path: string,
|
|
56
|
+
$refs: $Refs,
|
|
57
|
+
options: Options,
|
|
58
|
+
seen?: Set<any>,
|
|
59
|
+
) {
|
|
60
|
+
seen ||= new Set();
|
|
61
|
+
let promises: any = [];
|
|
56
62
|
|
|
57
63
|
if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !seen.has(obj)) {
|
|
58
64
|
seen.add(obj); // Track previously seen objects to avoid infinite recursion
|
|
59
65
|
if ($Ref.isExternal$Ref(obj)) {
|
|
60
66
|
promises.push(resolve$Ref(obj, path, $refs, options));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
let value = obj[key];
|
|
67
|
+
} else {
|
|
68
|
+
for (const key of Object.keys(obj)) {
|
|
69
|
+
const keyPath = Pointer.join(path, key);
|
|
70
|
+
const value = obj[key] as string | JSONSchema | Buffer | undefined;
|
|
66
71
|
|
|
67
72
|
if ($Ref.isExternal$Ref(value)) {
|
|
68
73
|
promises.push(resolve$Ref(value, keyPath, $refs, options));
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
74
|
+
} else {
|
|
71
75
|
promises = promises.concat(crawl(value, keyPath, $refs, options, seen));
|
|
72
76
|
}
|
|
73
77
|
}
|
|
@@ -80,20 +84,20 @@ function crawl (obj, path, $refs, options, seen) {
|
|
|
80
84
|
/**
|
|
81
85
|
* Resolves the given JSON Reference, and then crawls the resulting value.
|
|
82
86
|
*
|
|
83
|
-
* @param
|
|
84
|
-
* @param
|
|
85
|
-
* @param
|
|
86
|
-
* @param
|
|
87
|
+
* @param $ref - The JSON Reference to resolve
|
|
88
|
+
* @param path - The full path of `$ref`, possibly with a JSON Pointer in the hash
|
|
89
|
+
* @param $refs
|
|
90
|
+
* @param options
|
|
87
91
|
*
|
|
88
|
-
* @returns
|
|
92
|
+
* @returns
|
|
89
93
|
* The promise resolves once all JSON references in the object have been resolved,
|
|
90
94
|
* including nested references that are contained in externally-referenced files.
|
|
91
95
|
*/
|
|
92
|
-
async function resolve$Ref
|
|
96
|
+
async function resolve$Ref($ref: JSONSchema, path: string, $refs: $Refs, options: Options) {
|
|
93
97
|
// console.log('Resolving $ref pointer "%s" at %s', $ref.$ref, path);
|
|
94
98
|
|
|
95
|
-
|
|
96
|
-
|
|
99
|
+
const resolvedPath = url.resolve(path, $ref.$ref);
|
|
100
|
+
const withoutHash = url.stripHash(resolvedPath);
|
|
97
101
|
|
|
98
102
|
// Do we already have this $ref?
|
|
99
103
|
$ref = $refs._$refs[withoutHash];
|
|
@@ -108,12 +112,11 @@ async function resolve$Ref ($ref, path, $refs, options) {
|
|
|
108
112
|
|
|
109
113
|
// Crawl the parsed value
|
|
110
114
|
// console.log('Resolving $ref pointers in %s', withoutHash);
|
|
111
|
-
|
|
115
|
+
const promises = crawl(result, withoutHash + "#", $refs, options);
|
|
112
116
|
|
|
113
117
|
return Promise.all(promises);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (!options.continueOnError || !isHandledError(err)) {
|
|
118
|
+
} catch (err) {
|
|
119
|
+
if (!options?.continueOnError || !isHandledError(err)) {
|
|
117
120
|
throw err;
|
|
118
121
|
}
|
|
119
122
|
|