@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/pointer.js
DELETED
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
export default Pointer;
|
|
2
|
-
|
|
3
|
-
import $Ref from "./ref.js";
|
|
4
|
-
import * as url from "./util/url.js";
|
|
5
|
-
import { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } from "./util/errors.js";
|
|
6
|
-
const slashes = /\//g;
|
|
7
|
-
const tildes = /~/g;
|
|
8
|
-
const escapedSlash = /~1/g;
|
|
9
|
-
const escapedTilde = /~0/g;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* This class represents a single JSON pointer and its resolved value.
|
|
13
|
-
*
|
|
14
|
-
* @param {$Ref} $ref
|
|
15
|
-
* @param {string} path
|
|
16
|
-
* @param {string} [friendlyPath] - The original user-specified path (used for error messages)
|
|
17
|
-
* @constructor
|
|
18
|
-
*/
|
|
19
|
-
function Pointer ($ref, path, friendlyPath) {
|
|
20
|
-
/**
|
|
21
|
-
* The {@link $Ref} object that contains this {@link Pointer} object.
|
|
22
|
-
* @type {$Ref}
|
|
23
|
-
*/
|
|
24
|
-
this.$ref = $ref;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The file path or URL, containing the JSON pointer in the hash.
|
|
28
|
-
* This path is relative to the path of the main JSON schema file.
|
|
29
|
-
* @type {string}
|
|
30
|
-
*/
|
|
31
|
-
this.path = path;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The original path or URL, used for error messages.
|
|
35
|
-
* @type {string}
|
|
36
|
-
*/
|
|
37
|
-
this.originalPath = friendlyPath || path;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The value of the JSON pointer.
|
|
41
|
-
* Can be any JSON type, not just objects. Unknown file types are represented as Buffers (byte arrays).
|
|
42
|
-
* @type {?*}
|
|
43
|
-
*/
|
|
44
|
-
this.value = undefined;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Indicates whether the pointer references itself.
|
|
48
|
-
* @type {boolean}
|
|
49
|
-
*/
|
|
50
|
-
this.circular = false;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* The number of indirect references that were traversed to resolve the value.
|
|
54
|
-
* Resolving a single pointer may require resolving multiple $Refs.
|
|
55
|
-
* @type {number}
|
|
56
|
-
*/
|
|
57
|
-
this.indirections = 0;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Resolves the value of a nested property within the given object.
|
|
62
|
-
*
|
|
63
|
-
* @param {*} obj - The object that will be crawled
|
|
64
|
-
* @param {$RefParserOptions} options
|
|
65
|
-
* @param {string} pathFromRoot - the path of place that initiated resolving
|
|
66
|
-
*
|
|
67
|
-
* @returns {Pointer}
|
|
68
|
-
* Returns a JSON pointer whose {@link Pointer#value} is the resolved value.
|
|
69
|
-
* If resolving this value required resolving other JSON references, then
|
|
70
|
-
* the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
|
|
71
|
-
* of the resolved value.
|
|
72
|
-
*/
|
|
73
|
-
Pointer.prototype.resolve = function (obj, options, pathFromRoot) {
|
|
74
|
-
let tokens = Pointer.parse(this.path, this.originalPath);
|
|
75
|
-
|
|
76
|
-
// Crawl the object, one token at a time
|
|
77
|
-
this.value = unwrapOrThrow(obj);
|
|
78
|
-
|
|
79
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
80
|
-
if (resolveIf$Ref(this, options)) {
|
|
81
|
-
// The $ref path has changed, so append the remaining tokens to the path
|
|
82
|
-
this.path = Pointer.join(this.path, tokens.slice(i));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (typeof this.value === "object" && this.value !== null && "$ref" in this.value) {
|
|
86
|
-
return this;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
let token = tokens[i];
|
|
90
|
-
if (this.value[token] === undefined || this.value[token] === null) {
|
|
91
|
-
this.value = null;
|
|
92
|
-
throw new MissingPointerError(token, decodeURI(this.originalPath));
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
this.value = this.value[token];
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Resolve the final value
|
|
100
|
-
if (!this.value || this.value.$ref && url.resolve(this.path, this.value.$ref) !== pathFromRoot) {
|
|
101
|
-
resolveIf$Ref(this, options);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return this;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Sets the value of a nested property within the given object.
|
|
109
|
-
*
|
|
110
|
-
* @param {*} obj - The object that will be crawled
|
|
111
|
-
* @param {*} value - the value to assign
|
|
112
|
-
* @param {$RefParserOptions} options
|
|
113
|
-
*
|
|
114
|
-
* @returns {*}
|
|
115
|
-
* Returns the modified object, or an entirely new object if the entire object is overwritten.
|
|
116
|
-
*/
|
|
117
|
-
Pointer.prototype.set = function (obj, value, options) {
|
|
118
|
-
let tokens = Pointer.parse(this.path);
|
|
119
|
-
let token;
|
|
120
|
-
|
|
121
|
-
if (tokens.length === 0) {
|
|
122
|
-
// There are no tokens, replace the entire object with the new value
|
|
123
|
-
this.value = value;
|
|
124
|
-
return value;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Crawl the object, one token at a time
|
|
128
|
-
this.value = unwrapOrThrow(obj);
|
|
129
|
-
|
|
130
|
-
for (let i = 0; i < tokens.length - 1; i++) {
|
|
131
|
-
resolveIf$Ref(this, options);
|
|
132
|
-
|
|
133
|
-
token = tokens[i];
|
|
134
|
-
if (this.value && this.value[token] !== undefined) {
|
|
135
|
-
// The token exists
|
|
136
|
-
this.value = this.value[token];
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
// The token doesn't exist, so create it
|
|
140
|
-
this.value = setValue(this, token, {});
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Set the value of the final token
|
|
145
|
-
resolveIf$Ref(this, options);
|
|
146
|
-
token = tokens[tokens.length - 1];
|
|
147
|
-
setValue(this, token, value);
|
|
148
|
-
|
|
149
|
-
// Return the updated object
|
|
150
|
-
return obj;
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Parses a JSON pointer (or a path containing a JSON pointer in the hash)
|
|
155
|
-
* and returns an array of the pointer's tokens.
|
|
156
|
-
* (e.g. "schema.json#/definitions/person/name" => ["definitions", "person", "name"])
|
|
157
|
-
*
|
|
158
|
-
* The pointer is parsed according to RFC 6901
|
|
159
|
-
* {@link https://tools.ietf.org/html/rfc6901#section-3}
|
|
160
|
-
*
|
|
161
|
-
* @param {string} path
|
|
162
|
-
* @param {string} [originalPath]
|
|
163
|
-
* @returns {string[]}
|
|
164
|
-
*/
|
|
165
|
-
Pointer.parse = function (path, originalPath) {
|
|
166
|
-
// Get the JSON pointer from the path's hash
|
|
167
|
-
let pointer = url.getHash(path).substr(1);
|
|
168
|
-
|
|
169
|
-
// If there's no pointer, then there are no tokens,
|
|
170
|
-
// so return an empty array
|
|
171
|
-
if (!pointer) {
|
|
172
|
-
return [];
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Split into an array
|
|
176
|
-
pointer = pointer.split("/");
|
|
177
|
-
|
|
178
|
-
// Decode each part, according to RFC 6901
|
|
179
|
-
for (let i = 0; i < pointer.length; i++) {
|
|
180
|
-
pointer[i] = decodeURIComponent(pointer[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (pointer[0] !== "") {
|
|
184
|
-
throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return pointer.slice(1);
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Creates a JSON pointer path, by joining one or more tokens to a base path.
|
|
192
|
-
*
|
|
193
|
-
* @param {string} base - The base path (e.g. "schema.json#/definitions/person")
|
|
194
|
-
* @param {string|string[]} tokens - The token(s) to append (e.g. ["name", "first"])
|
|
195
|
-
* @returns {string}
|
|
196
|
-
*/
|
|
197
|
-
Pointer.join = function (base, tokens) {
|
|
198
|
-
// Ensure that the base path contains a hash
|
|
199
|
-
if (base.indexOf("#") === -1) {
|
|
200
|
-
base += "#";
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Append each token to the base path
|
|
204
|
-
tokens = Array.isArray(tokens) ? tokens : [tokens];
|
|
205
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
206
|
-
let token = tokens[i];
|
|
207
|
-
// Encode the token, according to RFC 6901
|
|
208
|
-
base += "/" + encodeURIComponent(token.replace(tildes, "~0").replace(slashes, "~1"));
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return base;
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* If the given pointer's {@link Pointer#value} is a JSON reference,
|
|
216
|
-
* then the reference is resolved and {@link Pointer#value} is replaced with the resolved value.
|
|
217
|
-
* In addition, {@link Pointer#path} and {@link Pointer#$ref} are updated to reflect the
|
|
218
|
-
* resolution path of the new value.
|
|
219
|
-
*
|
|
220
|
-
* @param {Pointer} pointer
|
|
221
|
-
* @param {$RefParserOptions} options
|
|
222
|
-
* @returns {boolean} - Returns `true` if the resolution path changed
|
|
223
|
-
*/
|
|
224
|
-
function resolveIf$Ref (pointer, options) {
|
|
225
|
-
// Is the value a JSON reference? (and allowed?)
|
|
226
|
-
|
|
227
|
-
if ($Ref.isAllowed$Ref(pointer.value, options)) {
|
|
228
|
-
let $refPath = url.resolve(pointer.path, pointer.value.$ref);
|
|
229
|
-
|
|
230
|
-
if ($refPath === pointer.path) {
|
|
231
|
-
// The value is a reference to itself, so there's nothing to do.
|
|
232
|
-
pointer.circular = true;
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
let resolved = pointer.$ref.$refs._resolve($refPath, pointer.path, options);
|
|
236
|
-
if (resolved === null) {
|
|
237
|
-
return false;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
pointer.indirections += resolved.indirections + 1;
|
|
241
|
-
|
|
242
|
-
if ($Ref.isExtended$Ref(pointer.value)) {
|
|
243
|
-
// This JSON reference "extends" the resolved value, rather than simply pointing to it.
|
|
244
|
-
// So the resolved path does NOT change. Just the value does.
|
|
245
|
-
pointer.value = $Ref.dereference(pointer.value, resolved.value);
|
|
246
|
-
return false;
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
// Resolve the reference
|
|
250
|
-
pointer.$ref = resolved.$ref;
|
|
251
|
-
pointer.path = resolved.path;
|
|
252
|
-
pointer.value = resolved.value;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
return true;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Sets the specified token value of the {@link Pointer#value}.
|
|
262
|
-
*
|
|
263
|
-
* The token is evaluated according to RFC 6901.
|
|
264
|
-
* {@link https://tools.ietf.org/html/rfc6901#section-4}
|
|
265
|
-
*
|
|
266
|
-
* @param {Pointer} pointer - The JSON Pointer whose value will be modified
|
|
267
|
-
* @param {string} token - A JSON Pointer token that indicates how to modify `obj`
|
|
268
|
-
* @param {*} value - The value to assign
|
|
269
|
-
* @returns {*} - Returns the assigned value
|
|
270
|
-
*/
|
|
271
|
-
function setValue (pointer, token, value) {
|
|
272
|
-
if (pointer.value && typeof pointer.value === "object") {
|
|
273
|
-
if (token === "-" && Array.isArray(pointer.value)) {
|
|
274
|
-
pointer.value.push(value);
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
pointer.value[token] = value;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
throw new JSONParserError(`Error assigning $ref pointer "${pointer.path}". \nCannot set "${token}" of a non-object.`);
|
|
282
|
-
}
|
|
283
|
-
return value;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
function unwrapOrThrow (value) {
|
|
288
|
-
if (isHandledError(value)) {
|
|
289
|
-
throw value;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
return value;
|
|
293
|
-
}
|
package/lib/ref.js
DELETED
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
export default $Ref;
|
|
2
|
-
|
|
3
|
-
import Pointer from "./pointer.js";
|
|
4
|
-
import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js";
|
|
5
|
-
import { safePointerToPath, stripHash, getHash } from "./util/url.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* This class represents a single JSON reference and its resolved value.
|
|
9
|
-
*
|
|
10
|
-
* @class
|
|
11
|
-
*/
|
|
12
|
-
function $Ref () {
|
|
13
|
-
/**
|
|
14
|
-
* The file path or URL of the referenced file.
|
|
15
|
-
* This path is relative to the path of the main JSON schema file.
|
|
16
|
-
*
|
|
17
|
-
* This path does NOT contain document fragments (JSON pointers). It always references an ENTIRE file.
|
|
18
|
-
* Use methods such as {@link $Ref#get}, {@link $Ref#resolve}, and {@link $Ref#exists} to get
|
|
19
|
-
* specific JSON pointers within the file.
|
|
20
|
-
*
|
|
21
|
-
* @type {string}
|
|
22
|
-
*/
|
|
23
|
-
this.path = undefined;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The resolved value of the JSON reference.
|
|
27
|
-
* Can be any JSON type, not just objects. Unknown file types are represented as Buffers (byte arrays).
|
|
28
|
-
*
|
|
29
|
-
* @type {?*}
|
|
30
|
-
*/
|
|
31
|
-
this.value = undefined;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The {@link $Refs} object that contains this {@link $Ref} object.
|
|
35
|
-
*
|
|
36
|
-
* @type {$Refs}
|
|
37
|
-
*/
|
|
38
|
-
this.$refs = undefined;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Indicates the type of {@link $Ref#path} (e.g. "file", "http", etc.)
|
|
42
|
-
*
|
|
43
|
-
* @type {?string}
|
|
44
|
-
*/
|
|
45
|
-
this.pathType = undefined;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* List of all errors. Undefined if no errors.
|
|
49
|
-
*
|
|
50
|
-
* @type {Array<JSONParserError | ResolverError | ParserError | MissingPointerError>}
|
|
51
|
-
*/
|
|
52
|
-
this.errors = undefined;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Pushes an error to errors array.
|
|
57
|
-
*
|
|
58
|
-
* @param {Array<JSONParserError | JSONParserErrorGroup>} err - The error to be pushed
|
|
59
|
-
* @returns {void}
|
|
60
|
-
*/
|
|
61
|
-
$Ref.prototype.addError = function (err) {
|
|
62
|
-
if (this.errors === undefined) {
|
|
63
|
-
this.errors = [];
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const existingErrors = this.errors.map(({ footprint }) => footprint);
|
|
67
|
-
|
|
68
|
-
// the path has been almost certainly set at this point,
|
|
69
|
-
// but just in case something went wrong, normalizeError injects path if necessary
|
|
70
|
-
// moreover, certain errors might point at the same spot, so filter them out to reduce noise
|
|
71
|
-
if (Array.isArray(err.errors)) {
|
|
72
|
-
this.errors.push(...err.errors
|
|
73
|
-
.map(normalizeError)
|
|
74
|
-
.filter(({ footprint }) => !existingErrors.includes(footprint)),
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
else if (!existingErrors.includes(err.footprint)) {
|
|
78
|
-
this.errors.push(normalizeError(err));
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Determines whether the given JSON reference exists within this {@link $Ref#value}.
|
|
84
|
-
*
|
|
85
|
-
* @param {string} path - The full path being resolved, optionally with a JSON pointer in the hash
|
|
86
|
-
* @param {$RefParserOptions} options
|
|
87
|
-
* @returns {boolean}
|
|
88
|
-
*/
|
|
89
|
-
$Ref.prototype.exists = function (path, options) {
|
|
90
|
-
try {
|
|
91
|
-
this.resolve(path, options);
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
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 {string} path - The full path being resolved, optionally with a JSON pointer in the hash
|
|
103
|
-
* @param {$RefParserOptions} options
|
|
104
|
-
* @returns {*} - Returns the resolved value
|
|
105
|
-
*/
|
|
106
|
-
$Ref.prototype.get = function (path, options) {
|
|
107
|
-
return this.resolve(path, options).value;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Resolves the given JSON reference within this {@link $Ref#value}.
|
|
112
|
-
*
|
|
113
|
-
* @param {string} path - The full path being resolved, optionally with a JSON pointer in the hash
|
|
114
|
-
* @param {$RefParserOptions} options
|
|
115
|
-
* @param {string} friendlyPath - The original user-specified path (used for error messages)
|
|
116
|
-
* @param {string} pathFromRoot - The path of `obj` from the schema root
|
|
117
|
-
* @returns {Pointer | null}
|
|
118
|
-
*/
|
|
119
|
-
$Ref.prototype.resolve = function (path, options, friendlyPath, pathFromRoot) {
|
|
120
|
-
let pointer = new Pointer(this, path, friendlyPath);
|
|
121
|
-
try {
|
|
122
|
-
return pointer.resolve(this.value, options, pathFromRoot);
|
|
123
|
-
}
|
|
124
|
-
catch (err) {
|
|
125
|
-
if (!options || !options.continueOnError || !isHandledError(err)) {
|
|
126
|
-
throw err;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (err.path === null) {
|
|
130
|
-
err.path = safePointerToPath(getHash(pathFromRoot));
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (err instanceof InvalidPointerError) {
|
|
134
|
-
// this is a special case - InvalidPointerError is thrown when dereferencing external file,
|
|
135
|
-
// but the issue is caused by the source file that referenced the file that undergoes dereferencing
|
|
136
|
-
err.source = decodeURI(stripHash(pathFromRoot));
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
this.addError(err);
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Sets the value of a nested property within this {@link $Ref#value}.
|
|
146
|
-
* If the property, or any of its parents don't exist, they will be created.
|
|
147
|
-
*
|
|
148
|
-
* @param {string} path - The full path of the property to set, optionally with a JSON pointer in the hash
|
|
149
|
-
* @param {*} value - The value to assign
|
|
150
|
-
*/
|
|
151
|
-
$Ref.prototype.set = function (path, value) {
|
|
152
|
-
let pointer = new Pointer(this, path);
|
|
153
|
-
this.value = pointer.set(this.value, value);
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Determines whether the given value is a JSON reference.
|
|
158
|
-
*
|
|
159
|
-
* @param {*} value - The value to inspect
|
|
160
|
-
* @returns {boolean}
|
|
161
|
-
*/
|
|
162
|
-
$Ref.is$Ref = function (value) {
|
|
163
|
-
return value && typeof value === "object" && typeof value.$ref === "string" && value.$ref.length > 0;
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Determines whether the given value is an external JSON reference.
|
|
168
|
-
*
|
|
169
|
-
* @param {*} value - The value to inspect
|
|
170
|
-
* @returns {boolean}
|
|
171
|
-
*/
|
|
172
|
-
$Ref.isExternal$Ref = function (value) {
|
|
173
|
-
return $Ref.is$Ref(value) && value.$ref[0] !== "#";
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Determines whether the given value is a JSON reference, and whether it is allowed by the options.
|
|
178
|
-
* For example, if it references an external file, then options.resolve.external must be true.
|
|
179
|
-
*
|
|
180
|
-
* @param {*} value - The value to inspect
|
|
181
|
-
* @param {$RefParserOptions} options
|
|
182
|
-
* @returns {boolean}
|
|
183
|
-
*/
|
|
184
|
-
$Ref.isAllowed$Ref = function (value, options) {
|
|
185
|
-
if ($Ref.is$Ref(value)) {
|
|
186
|
-
if (value.$ref.substr(0, 2) === "#/" || value.$ref === "#") {
|
|
187
|
-
// It's a JSON Pointer reference, which is always allowed
|
|
188
|
-
return true;
|
|
189
|
-
}
|
|
190
|
-
else if (value.$ref[0] !== "#" && (!options || options.resolve.external)) {
|
|
191
|
-
// It's an external reference, which is allowed by the options
|
|
192
|
-
return true;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Determines whether the given value is a JSON reference that "extends" its resolved value.
|
|
199
|
-
* That is, it has extra properties (in addition to "$ref"), so rather than simply pointing to
|
|
200
|
-
* an existing value, this $ref actually creates a NEW value that is a shallow copy of the resolved
|
|
201
|
-
* value, plus the extra properties.
|
|
202
|
-
*
|
|
203
|
-
* @example:
|
|
204
|
-
* {
|
|
205
|
-
* person: {
|
|
206
|
-
* properties: {
|
|
207
|
-
* firstName: { type: string }
|
|
208
|
-
* lastName: { type: string }
|
|
209
|
-
* }
|
|
210
|
-
* }
|
|
211
|
-
* employee: {
|
|
212
|
-
* properties: {
|
|
213
|
-
* $ref: #/person/properties
|
|
214
|
-
* salary: { type: number }
|
|
215
|
-
* }
|
|
216
|
-
* }
|
|
217
|
-
* }
|
|
218
|
-
*
|
|
219
|
-
* In this example, "employee" is an extended $ref, since it extends "person" with an additional
|
|
220
|
-
* property (salary). The result is a NEW value that looks like this:
|
|
221
|
-
*
|
|
222
|
-
* {
|
|
223
|
-
* properties: {
|
|
224
|
-
* firstName: { type: string }
|
|
225
|
-
* lastName: { type: string }
|
|
226
|
-
* salary: { type: number }
|
|
227
|
-
* }
|
|
228
|
-
* }
|
|
229
|
-
*
|
|
230
|
-
* @param {*} value - The value to inspect
|
|
231
|
-
* @returns {boolean}
|
|
232
|
-
*/
|
|
233
|
-
$Ref.isExtended$Ref = function (value) {
|
|
234
|
-
return $Ref.is$Ref(value) && Object.keys(value).length > 1;
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Returns the resolved value of a JSON Reference.
|
|
239
|
-
* If necessary, the resolved value is merged with the JSON Reference to create a new object
|
|
240
|
-
*
|
|
241
|
-
* @example:
|
|
242
|
-
* {
|
|
243
|
-
* person: {
|
|
244
|
-
* properties: {
|
|
245
|
-
* firstName: { type: string }
|
|
246
|
-
* lastName: { type: string }
|
|
247
|
-
* }
|
|
248
|
-
* }
|
|
249
|
-
* employee: {
|
|
250
|
-
* properties: {
|
|
251
|
-
* $ref: #/person/properties
|
|
252
|
-
* salary: { type: number }
|
|
253
|
-
* }
|
|
254
|
-
* }
|
|
255
|
-
* }
|
|
256
|
-
*
|
|
257
|
-
* When "person" and "employee" are merged, you end up with the following object:
|
|
258
|
-
*
|
|
259
|
-
* {
|
|
260
|
-
* properties: {
|
|
261
|
-
* firstName: { type: string }
|
|
262
|
-
* lastName: { type: string }
|
|
263
|
-
* salary: { type: number }
|
|
264
|
-
* }
|
|
265
|
-
* }
|
|
266
|
-
*
|
|
267
|
-
* @param {object} $ref - The JSON reference object (the one with the "$ref" property)
|
|
268
|
-
* @param {*} resolvedValue - The resolved value, which can be any type
|
|
269
|
-
* @returns {*} - Returns the dereferenced value
|
|
270
|
-
*/
|
|
271
|
-
$Ref.dereference = function ($ref, resolvedValue) {
|
|
272
|
-
if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) {
|
|
273
|
-
let merged = {};
|
|
274
|
-
for (let key of Object.keys($ref)) {
|
|
275
|
-
if (key !== "$ref") {
|
|
276
|
-
merged[key] = $ref[key];
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
for (let key of Object.keys(resolvedValue)) {
|
|
281
|
-
if (!(key in merged)) {
|
|
282
|
-
merged[key] = resolvedValue[key];
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return merged;
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
289
|
-
// Completely replace the original reference with the resolved value
|
|
290
|
-
return resolvedValue;
|
|
291
|
-
}
|
|
292
|
-
};
|