@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.
Files changed (47) hide show
  1. package/README.md +0 -4
  2. package/dist/lib/bundle.d.ts +3 -3
  3. package/dist/lib/bundle.js +16 -54
  4. package/dist/lib/dereference.d.ts +1 -2
  5. package/dist/lib/dereference.js +12 -50
  6. package/dist/lib/index.js +42 -95
  7. package/dist/lib/normalize-args.d.ts +1 -1
  8. package/dist/lib/normalize-args.js +4 -7
  9. package/dist/lib/options.d.ts +6 -0
  10. package/dist/lib/options.js +16 -23
  11. package/dist/lib/parse.js +11 -46
  12. package/dist/lib/parsers/binary.js +1 -3
  13. package/dist/lib/parsers/json.js +4 -6
  14. package/dist/lib/parsers/text.js +3 -5
  15. package/dist/lib/parsers/yaml.js +12 -11
  16. package/dist/lib/pointer.d.ts +3 -2
  17. package/dist/lib/pointer.js +18 -63
  18. package/dist/lib/ref.d.ts +5 -3
  19. package/dist/lib/ref.js +60 -52
  20. package/dist/lib/refs.d.ts +1 -1
  21. package/dist/lib/refs.js +7 -46
  22. package/dist/lib/resolve-external.js +10 -48
  23. package/dist/lib/resolvers/file.js +7 -45
  24. package/dist/lib/resolvers/http.js +5 -40
  25. package/dist/lib/types/index.d.ts +1 -1
  26. package/dist/lib/types/index.js +1 -2
  27. package/dist/lib/util/convert-path-to-posix.js +3 -9
  28. package/dist/lib/util/errors.d.ts +1 -1
  29. package/dist/lib/util/errors.js +17 -33
  30. package/dist/lib/util/is-windows.js +1 -5
  31. package/dist/lib/util/maybe.js +4 -10
  32. package/dist/lib/util/next.js +1 -3
  33. package/dist/lib/util/plugins.js +4 -10
  34. package/dist/lib/util/url.d.ts +1 -1
  35. package/dist/lib/util/url.js +40 -88
  36. package/lib/bundle.ts +12 -9
  37. package/lib/dereference.ts +3 -4
  38. package/lib/normalize-args.ts +1 -1
  39. package/lib/options.ts +8 -0
  40. package/lib/parsers/yaml.ts +6 -1
  41. package/lib/pointer.ts +19 -20
  42. package/lib/ref.ts +55 -5
  43. package/lib/refs.ts +2 -2
  44. package/lib/types/index.ts +1 -1
  45. package/lib/util/errors.ts +2 -2
  46. package/lib/util/url.ts +13 -7
  47. package/package.json +21 -20
package/dist/lib/parse.js CHANGED
@@ -1,41 +1,6 @@
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 () {
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
- Object.defineProperty(exports, "__esModule", { value: true });
36
- const url = __importStar(require("./util/url.js"));
37
- const plugins = __importStar(require("./util/plugins.js"));
38
- const errors_js_1 = require("./util/errors.js");
1
+ import * as url from "./util/url.js";
2
+ import * as plugins from "./util/plugins.js";
3
+ import { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, } from "./util/errors.js";
39
4
  /**
40
5
  * Reads and parses the specified file path or URL.
41
6
  */
@@ -67,7 +32,7 @@ async function parse(path, $refs, options) {
67
32
  return parser.result;
68
33
  }
69
34
  catch (err) {
70
- if ((0, errors_js_1.isHandledError)(err)) {
35
+ if (isHandledError(err)) {
71
36
  $ref.value = err;
72
37
  }
73
38
  throw err;
@@ -98,18 +63,18 @@ async function readFile(file, options, $refs) {
98
63
  catch (err) {
99
64
  if (!err && options.continueOnError) {
100
65
  // No resolver could be matched
101
- throw new errors_js_1.UnmatchedResolverError(file.url);
66
+ throw new UnmatchedResolverError(file.url);
102
67
  }
103
68
  else if (!err || !("error" in err)) {
104
69
  // Throw a generic, friendly error.
105
70
  throw new SyntaxError(`Unable to resolve $ref pointer "${file.url}"`);
106
71
  }
107
72
  // Throw the original error, if it's one of our own (user-friendly) errors.
108
- else if (err.error instanceof errors_js_1.ResolverError) {
73
+ else if (err.error instanceof ResolverError) {
109
74
  throw err.error;
110
75
  }
111
76
  else {
112
- throw new errors_js_1.ResolverError(err, file.url);
77
+ throw new ResolverError(err, file.url);
113
78
  }
114
79
  }
115
80
  }
@@ -147,7 +112,7 @@ async function parseFile(file, options, $refs) {
147
112
  catch (err) {
148
113
  if (!err && options.continueOnError) {
149
114
  // No resolver could be matched
150
- throw new errors_js_1.UnmatchedParserError(file.url);
115
+ throw new UnmatchedParserError(file.url);
151
116
  }
152
117
  else if (err && err.message && err.message.startsWith("Error parsing")) {
153
118
  throw err;
@@ -155,11 +120,11 @@ async function parseFile(file, options, $refs) {
155
120
  else if (!err || !("error" in err)) {
156
121
  throw new SyntaxError(`Unable to parse ${file.url}`);
157
122
  }
158
- else if (err.error instanceof errors_js_1.ParserError) {
123
+ else if (err.error instanceof ParserError) {
159
124
  throw err.error;
160
125
  }
161
126
  else {
162
- throw new errors_js_1.ParserError(err.error.message, file.url);
127
+ throw new ParserError(err.error.message, file.url);
163
128
  }
164
129
  }
165
130
  }
@@ -175,4 +140,4 @@ function isEmpty(value) {
175
140
  (typeof value === "string" && value.trim().length === 0) ||
176
141
  (Buffer.isBuffer(value) && value.length === 0));
177
142
  }
178
- exports.default = parse;
143
+ export default parse;
@@ -1,7 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
1
  const BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i;
4
- exports.default = {
2
+ export default {
5
3
  /**
6
4
  * The order that this parser will run, in relation to other parsers.
7
5
  */
@@ -1,7 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const errors_js_1 = require("../util/errors.js");
4
- exports.default = {
1
+ import { ParserError } from "../util/errors.js";
2
+ export default {
5
3
  /**
6
4
  * The order that this parser will run, in relation to other parsers.
7
5
  */
@@ -47,10 +45,10 @@ exports.default = {
47
45
  return JSON.parse(data);
48
46
  }
49
47
  catch (e) {
50
- throw new errors_js_1.ParserError(e.message, file.url);
48
+ throw new ParserError(e.message, file.url);
51
49
  }
52
50
  }
53
- throw new errors_js_1.ParserError(e.message, file.url);
51
+ throw new ParserError(e.message, file.url);
54
52
  }
55
53
  }
56
54
  }
@@ -1,8 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const errors_js_1 = require("../util/errors.js");
1
+ import { ParserError } from "../util/errors.js";
4
2
  const TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i;
5
- exports.default = {
3
+ export default {
6
4
  /**
7
5
  * The order that this parser will run, in relation to other parsers.
8
6
  */
@@ -36,7 +34,7 @@ exports.default = {
36
34
  return file.data.toString(this.encoding);
37
35
  }
38
36
  else {
39
- throw new errors_js_1.ParserError("data is not text", file.url);
37
+ throw new ParserError("data is not text", file.url);
40
38
  }
41
39
  },
42
40
  };
@@ -1,12 +1,7 @@
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 errors_js_1 = require("../util/errors.js");
7
- const js_yaml_1 = __importDefault(require("js-yaml"));
8
- const js_yaml_2 = require("js-yaml");
9
- exports.default = {
1
+ import { ParserError } from "../util/errors.js";
2
+ import yaml from "js-yaml";
3
+ import { JSON_SCHEMA } from "js-yaml";
4
+ export default {
10
5
  /**
11
6
  * The order that this parser will run, in relation to other parsers.
12
7
  */
@@ -38,10 +33,16 @@ exports.default = {
38
33
  }
39
34
  if (typeof data === "string") {
40
35
  try {
41
- return js_yaml_1.default.load(data, { schema: js_yaml_2.JSON_SCHEMA });
36
+ return yaml.load(data, { schema: JSON_SCHEMA });
42
37
  }
43
38
  catch (e) {
44
- throw new errors_js_1.ParserError(e?.message || "Parser Error", file.url);
39
+ try {
40
+ // fallback to non JSON_SCHEMA
41
+ return yaml.load(data);
42
+ }
43
+ catch (e) {
44
+ throw new ParserError(e?.message || "Parser Error", file.url);
45
+ }
45
46
  }
46
47
  }
47
48
  else {
@@ -1,6 +1,7 @@
1
1
  import type { ParserOptions } from "./options.js";
2
2
  import $Ref from "./ref.js";
3
- import type { JSONSchema } from "./types";
3
+ import type { JSONSchema } from "./index.js";
4
+ import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
4
5
  export declare const nullSymbol: unique symbol;
5
6
  /**
6
7
  * This class represents a single JSON pointer and its resolved value.
@@ -63,7 +64,7 @@ declare class Pointer<S extends object = JSONSchema, O extends ParserOptions<S>
63
64
  * @returns
64
65
  * Returns the modified object, or an entirely new object if the entire object is overwritten.
65
66
  */
66
- set(obj: S, value: any, options?: O): any;
67
+ set(obj: S, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type, options?: O): string | number | boolean | import("json-schema").JSONSchema4Object | import("json-schema").JSONSchema4Array | S | null;
67
68
  /**
68
69
  * Parses a JSON pointer (or a path containing a JSON pointer in the hash)
69
70
  * and returns an array of the pointer's tokens.
@@ -1,58 +1,11 @@
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 () {
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.nullSymbol = void 0;
40
- const ref_js_1 = __importDefault(require("./ref.js"));
41
- const url = __importStar(require("./util/url.js"));
42
- const errors_js_1 = require("./util/errors.js");
43
- exports.nullSymbol = Symbol("null");
1
+ import $Ref from "./ref.js";
2
+ import * as url from "./util/url.js";
3
+ import { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } from "./util/errors.js";
4
+ export const nullSymbol = Symbol("null");
44
5
  const slashes = /\//g;
45
6
  const tildes = /~/g;
46
7
  const escapedSlash = /~1/g;
47
8
  const escapedTilde = /~0/g;
48
- const safeDecodeURIComponent = (encodedURIComponent) => {
49
- try {
50
- return decodeURIComponent(encodedURIComponent);
51
- }
52
- catch {
53
- return encodedURIComponent;
54
- }
55
- };
56
9
  /**
57
10
  * This class represents a single JSON pointer and its resolved value.
58
11
  *
@@ -140,9 +93,9 @@ class Pointer {
140
93
  // actually instead pointing to an existing `null` value then we should use that
141
94
  // `null` value.
142
95
  if (token in this.value && this.value[token] === null) {
143
- // We use a `null` symbol for internal tracking to differntiate between a general `null`
96
+ // We use a `null` symbol for internal tracking to differentiate between a general `null`
144
97
  // value and our expected `null` value.
145
- this.value = exports.nullSymbol;
98
+ this.value = nullSymbol;
146
99
  continue;
147
100
  }
148
101
  this.value = null;
@@ -150,7 +103,7 @@ class Pointer {
150
103
  const targetRef = this.path.replace(path, "");
151
104
  const targetFound = Pointer.join("", found);
152
105
  const parentPath = pathFromRoot?.replace(path, "");
153
- throw new errors_js_1.MissingPointerError(token, decodeURI(this.originalPath), targetRef, targetFound, parentPath);
106
+ throw new MissingPointerError(token, decodeURI(this.originalPath), targetRef, targetFound, parentPath);
154
107
  }
155
108
  else {
156
109
  this.value = this.value[token];
@@ -226,10 +179,10 @@ class Pointer {
226
179
  const split = pointer.split("/");
227
180
  // Decode each part, according to RFC 6901
228
181
  for (let i = 0; i < split.length; i++) {
229
- split[i] = safeDecodeURIComponent(split[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
182
+ split[i] = split[i].replace(escapedSlash, "/").replace(escapedTilde, "~");
230
183
  }
231
184
  if (split[0] !== "") {
232
- throw new errors_js_1.InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
185
+ throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
233
186
  }
234
187
  return split.slice(1);
235
188
  }
@@ -250,7 +203,9 @@ class Pointer {
250
203
  for (let i = 0; i < tokens.length; i++) {
251
204
  const token = tokens[i];
252
205
  // Encode the token, according to RFC 6901
253
- base += "/" + encodeURIComponent(token.replace(tildes, "~0").replace(slashes, "~1"));
206
+ // RFC 6901 only requires encoding ~ as ~0 and / as ~1
207
+ // We do NOT use encodeURIComponent as it encodes characters like $ which should remain literal
208
+ base += "/" + token.replace(tildes, "~0").replace(slashes, "~1");
254
209
  }
255
210
  return base;
256
211
  }
@@ -268,7 +223,7 @@ class Pointer {
268
223
  */
269
224
  function resolveIf$Ref(pointer, options, pathFromRoot) {
270
225
  // Is the value a JSON reference? (and allowed?)
271
- if (ref_js_1.default.isAllowed$Ref(pointer.value, options)) {
226
+ if ($Ref.isAllowed$Ref(pointer.value, options)) {
272
227
  const $refPath = url.resolve(pointer.path, pointer.value.$ref);
273
228
  if ($refPath === pointer.path && !isRootPath(pathFromRoot)) {
274
229
  // The value is a reference to itself, so there's nothing to do.
@@ -280,10 +235,10 @@ function resolveIf$Ref(pointer, options, pathFromRoot) {
280
235
  return false;
281
236
  }
282
237
  pointer.indirections += resolved.indirections + 1;
283
- if (ref_js_1.default.isExtended$Ref(pointer.value)) {
238
+ if ($Ref.isExtended$Ref(pointer.value)) {
284
239
  // This JSON reference "extends" the resolved value, rather than simply pointing to it.
285
240
  // So the resolved path does NOT change. Just the value does.
286
- pointer.value = ref_js_1.default.dereference(pointer.value, resolved.value);
241
+ pointer.value = $Ref.dereference(pointer.value, resolved.value, options);
287
242
  return false;
288
243
  }
289
244
  else {
@@ -297,7 +252,7 @@ function resolveIf$Ref(pointer, options, pathFromRoot) {
297
252
  }
298
253
  return undefined;
299
254
  }
300
- exports.default = Pointer;
255
+ export default Pointer;
301
256
  /**
302
257
  * Sets the specified token value of the {@link Pointer#value}.
303
258
  *
@@ -319,12 +274,12 @@ function setValue(pointer, token, value) {
319
274
  }
320
275
  }
321
276
  else {
322
- throw new errors_js_1.JSONParserError(`Error assigning $ref pointer "${pointer.path}". \nCannot set "${token}" of a non-object.`);
277
+ throw new JSONParserError(`Error assigning $ref pointer "${pointer.path}". \nCannot set "${token}" of a non-object.`);
323
278
  }
324
279
  return value;
325
280
  }
326
281
  function unwrapOrThrow(value) {
327
- if ((0, errors_js_1.isHandledError)(value)) {
282
+ if (isHandledError(value)) {
328
283
  throw value;
329
284
  }
330
285
  return value;
package/dist/lib/ref.d.ts CHANGED
@@ -2,7 +2,8 @@ import Pointer from "./pointer.js";
2
2
  import type { JSONParserError, MissingPointerError, ParserError, ResolverError } from "./util/errors.js";
3
3
  import type $Refs from "./refs.js";
4
4
  import type { ParserOptions } from "./options.js";
5
- import type { JSONSchema } from "./types";
5
+ import type { JSONSchema } from "./index.js";
6
+ import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
6
7
  export type $RefError = JSONParserError | ResolverError | ParserError | MissingPointerError;
7
8
  /**
8
9
  * This class represents a single JSON reference and its resolved value.
@@ -83,7 +84,7 @@ declare class $Ref<S extends object = JSONSchema, O extends ParserOptions<S> = P
83
84
  * @param path - The full path of the property to set, optionally with a JSON pointer in the hash
84
85
  * @param value - The value to assign
85
86
  */
86
- set(path: string, value: any): void;
87
+ set(path: string, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type): void;
87
88
  /**
88
89
  * Determines whether the given value is a JSON reference.
89
90
  *
@@ -174,8 +175,9 @@ declare class $Ref<S extends object = JSONSchema, O extends ParserOptions<S> = P
174
175
  *
175
176
  * @param $ref - The JSON reference object (the one with the "$ref" property)
176
177
  * @param resolvedValue - The resolved value, which can be any type
178
+ * @param options - The options
177
179
  * @returns - Returns the dereferenced value
178
180
  */
179
- static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>($ref: $Ref<S, O>, resolvedValue: S): S;
181
+ static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>($ref: $Ref<S, O>, resolvedValue: S, options?: O): S;
180
182
  }
181
183
  export default $Ref;
package/dist/lib/ref.js CHANGED
@@ -1,41 +1,6 @@
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 () {
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
- Object.defineProperty(exports, "__esModule", { value: true });
36
- const pointer_js_1 = __importStar(require("./pointer.js"));
37
- const errors_js_1 = require("./util/errors.js");
38
- const url_js_1 = require("./util/url.js");
1
+ import Pointer, { nullSymbol } from "./pointer.js";
2
+ import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js";
3
+ import { safePointerToPath, stripHash, getHash } from "./util/url.js";
39
4
  /**
40
5
  * This class represents a single JSON reference and its resolved value.
41
6
  *
@@ -92,10 +57,10 @@ class $Ref {
92
57
  // but just in case something went wrong, normalizeError injects path if necessary
93
58
  // moreover, certain errors might point at the same spot, so filter them out to reduce noise
94
59
  if ("errors" in err && Array.isArray(err.errors)) {
95
- this.errors.push(...err.errors.map(errors_js_1.normalizeError).filter(({ footprint }) => !existingErrors.includes(footprint)));
60
+ this.errors.push(...err.errors.map(normalizeError).filter(({ footprint }) => !existingErrors.includes(footprint)));
96
61
  }
97
62
  else if (!("footprint" in err) || !existingErrors.includes(err.footprint)) {
98
- this.errors.push((0, errors_js_1.normalizeError)(err));
63
+ this.errors.push(normalizeError(err));
99
64
  }
100
65
  }
101
66
  /**
@@ -134,23 +99,23 @@ class $Ref {
134
99
  * @returns
135
100
  */
136
101
  resolve(path, options, friendlyPath, pathFromRoot) {
137
- const pointer = new pointer_js_1.default(this, path, friendlyPath);
102
+ const pointer = new Pointer(this, path, friendlyPath);
138
103
  try {
139
104
  const resolved = pointer.resolve(this.value, options, pathFromRoot);
140
- if (resolved.value === pointer_js_1.nullSymbol) {
105
+ if (resolved.value === nullSymbol) {
141
106
  resolved.value = null;
142
107
  }
143
108
  return resolved;
144
109
  }
145
110
  catch (err) {
146
- if (!options || !options.continueOnError || !(0, errors_js_1.isHandledError)(err)) {
111
+ if (!options || !options.continueOnError || !isHandledError(err)) {
147
112
  throw err;
148
113
  }
149
114
  if (err.path === null) {
150
- err.path = (0, url_js_1.safePointerToPath)((0, url_js_1.getHash)(pathFromRoot));
115
+ err.path = safePointerToPath(getHash(pathFromRoot));
151
116
  }
152
- if (err instanceof errors_js_1.InvalidPointerError) {
153
- err.source = decodeURI((0, url_js_1.stripHash)(pathFromRoot));
117
+ if (err instanceof InvalidPointerError) {
118
+ err.source = decodeURI(stripHash(pathFromRoot));
154
119
  }
155
120
  this.addError(err);
156
121
  return null;
@@ -164,9 +129,9 @@ class $Ref {
164
129
  * @param value - The value to assign
165
130
  */
166
131
  set(path, value) {
167
- const pointer = new pointer_js_1.default(this, path);
132
+ const pointer = new Pointer(this, path);
168
133
  this.value = pointer.set(this.value, value);
169
- if (this.value === pointer_js_1.nullSymbol) {
134
+ if (this.value === nullSymbol) {
170
135
  this.value = null;
171
136
  }
172
137
  }
@@ -280,9 +245,10 @@ class $Ref {
280
245
  *
281
246
  * @param $ref - The JSON reference object (the one with the "$ref" property)
282
247
  * @param resolvedValue - The resolved value, which can be any type
248
+ * @param options - The options
283
249
  * @returns - Returns the dereferenced value
284
250
  */
285
- static dereference($ref, resolvedValue) {
251
+ static dereference($ref, resolvedValue, options) {
286
252
  if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) {
287
253
  const merged = {};
288
254
  for (const key of Object.keys($ref)) {
@@ -291,11 +257,23 @@ class $Ref {
291
257
  merged[key] = $ref[key];
292
258
  }
293
259
  }
294
- for (const key of Object.keys(resolvedValue)) {
260
+ const mergeKeys = options?.dereference?.mergeKeys ?? true;
261
+ for (const _key of Object.keys(resolvedValue)) {
262
+ const key = _key;
295
263
  if (!(key in merged)) {
296
- // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
297
264
  merged[key] = resolvedValue[key];
298
265
  }
266
+ else {
267
+ // TODO: this behavior should be configurable from options on the CLI
268
+ // Key is already in merged, so we should merge them if both are objects
269
+ if (mergeKeys &&
270
+ typeof merged[key] === "object" &&
271
+ merged[key] !== null &&
272
+ typeof resolvedValue[key] === "object" &&
273
+ resolvedValue[key] !== null) {
274
+ merged[key] = deepMerge(resolvedValue[key], merged[key]);
275
+ }
276
+ }
299
277
  }
300
278
  return merged;
301
279
  }
@@ -305,4 +283,34 @@ class $Ref {
305
283
  }
306
284
  }
307
285
  }
308
- exports.default = $Ref;
286
+ function deepMerge(target, source) {
287
+ //return {...target, ...source};
288
+ // If either isn't an object, just return source (overwrite)
289
+ if (typeof target !== "object" || target === null) {
290
+ return source;
291
+ }
292
+ if (typeof source !== "object" || source === null) {
293
+ return source;
294
+ }
295
+ // Ensure we don't mutate target directly
296
+ const output = Array.isArray(target) ? [...target] : { ...target };
297
+ for (const key of Object.keys(source)) {
298
+ // @ts-expect-error
299
+ if (Array.isArray(source[key])) {
300
+ // If it's an array, replace entirely (you can customize this to concat instead)
301
+ // @ts-expect-error
302
+ output[key] = [...source[key]];
303
+ // @ts-expect-error
304
+ }
305
+ else if (typeof source[key] === "object" && source[key] !== null) {
306
+ // @ts-expect-error
307
+ output[key] = deepMerge(target[key], source[key]);
308
+ }
309
+ else {
310
+ // @ts-expect-error
311
+ output[key] = source[key];
312
+ }
313
+ }
314
+ return output;
315
+ }
316
+ export default $Ref;
@@ -1,7 +1,7 @@
1
1
  import $Ref from "./ref.js";
2
2
  import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
3
3
  import type { ParserOptions } from "./options.js";
4
- import type { JSONSchema } from "./types";
4
+ import type { JSONSchema } from "./index.js";
5
5
  interface $RefsMap<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
6
6
  [url: string]: $Ref<S, O>;
7
7
  }
package/dist/lib/refs.js CHANGED
@@ -1,44 +1,6 @@
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 () {
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 url = __importStar(require("./util/url.js"));
41
- const convert_path_to_posix_1 = __importDefault(require("./util/convert-path-to-posix"));
1
+ import $Ref from "./ref.js";
2
+ import * as url from "./util/url.js";
3
+ import convertPathToPosix from "./util/convert-path-to-posix.js";
42
4
  /**
43
5
  * 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.
44
6
  *
@@ -46,7 +8,7 @@ const convert_path_to_posix_1 = __importDefault(require("./util/convert-path-to-
46
8
  *
47
9
  * See https://apidevtools.com/json-schema-ref-parser/docs/refs.html
48
10
  */
49
- class $Refs {
11
+ export default class $Refs {
50
12
  /**
51
13
  * 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.
52
14
  *
@@ -63,7 +25,7 @@ class $Refs {
63
25
  paths(...types) {
64
26
  const paths = getPaths(this._$refs, types.flat());
65
27
  return paths.map((path) => {
66
- return (0, convert_path_to_posix_1.default)(path.decoded);
28
+ return convertPathToPosix(path.decoded);
67
29
  });
68
30
  }
69
31
  /**
@@ -77,7 +39,7 @@ class $Refs {
77
39
  const $refs = this._$refs;
78
40
  const paths = getPaths($refs, types.flat());
79
41
  return paths.reduce((obj, path) => {
80
- obj[(0, convert_path_to_posix_1.default)(path.decoded)] = $refs[path.encoded].value;
42
+ obj[convertPathToPosix(path.decoded)] = $refs[path.encoded].value;
81
43
  return obj;
82
44
  }, {});
83
45
  }
@@ -148,7 +110,7 @@ class $Refs {
148
110
  */
149
111
  _add(path) {
150
112
  const withoutHash = url.stripHash(path);
151
- const $ref = new ref_js_1.default(this);
113
+ const $ref = new $Ref(this);
152
114
  $ref.path = withoutHash;
153
115
  this._$refs[withoutHash] = $ref;
154
116
  this._root$Ref = this._root$Ref || $ref;
@@ -217,7 +179,6 @@ class $Refs {
217
179
  */
218
180
  toJSON = this.values;
219
181
  }
220
- exports.default = $Refs;
221
182
  /**
222
183
  * Returns the encoded and decoded paths keys of the given object.
223
184
  *