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