@apidevtools/json-schema-ref-parser 14.0.3 → 14.1.1

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.
@@ -69,7 +69,9 @@ function bundle(parser, options) {
69
69
  */
70
70
  function crawl(parent, key, path, pathFromRoot, indirections, inventory, $refs, options) {
71
71
  const obj = key === null ? parent : parent[key];
72
- if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) {
72
+ const bundleOptions = (options.bundle || {});
73
+ const isExcludedPath = bundleOptions.excludedPathMatcher || (() => false);
74
+ if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
73
75
  if (ref_js_1.default.isAllowed$Ref(obj)) {
74
76
  inventory$Ref(parent, key, path, pathFromRoot, indirections, inventory, $refs, options);
75
77
  }
@@ -102,6 +104,13 @@ function crawl(parent, key, path, pathFromRoot, indirections, inventory, $refs,
102
104
  else {
103
105
  crawl(obj, key, keyPath, keyPathFromRoot, indirections, inventory, $refs, options);
104
106
  }
107
+ // We need to ensure that we have an object to work with here because we may be crawling
108
+ // an `examples` schema and `value` may be nullish.
109
+ if (value && typeof value === "object" && !Array.isArray(value)) {
110
+ if ("$ref" in value) {
111
+ bundleOptions?.onBundle?.(value["$ref"], obj[key], obj, key);
112
+ }
113
+ }
105
114
  }
106
115
  }
107
116
  }
@@ -2,6 +2,23 @@ import type { HTTPResolverOptions, JSONSchema, JSONSchemaObject, Plugin, Resolve
2
2
  export type DeepPartial<T> = T extends object ? {
3
3
  [P in keyof T]?: DeepPartial<T[P]>;
4
4
  } : T;
5
+ export interface BundleOptions {
6
+ /**
7
+ * A function, called for each path, which can return true to stop this path and all
8
+ * subpaths from being processed further. This is useful in schemas where some
9
+ * subpaths contain literal $ref keys that should not be changed.
10
+ */
11
+ excludedPathMatcher?(path: string): boolean;
12
+ /**
13
+ * Callback invoked during bundling.
14
+ *
15
+ * @argument {string} path - The path being processed (ie. the `$ref` string)
16
+ * @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
17
+ * @argument {JSONSchemaObject} parent - The parent of the processed object
18
+ * @argument {string} parentPropName - The prop name of the parent object whose value was processed
19
+ */
20
+ onBundle?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
21
+ }
5
22
  export interface DereferenceOptions {
6
23
  /**
7
24
  * Determines whether circular `$ref` pointers are handled.
@@ -88,6 +105,10 @@ export interface $RefParserOptions<S extends object = JSONSchema> {
88
105
  * that were encountered.
89
106
  */
90
107
  continueOnError: boolean;
108
+ /**
109
+ * The `bundle` options control how JSON Schema `$Ref` Parser will process `$ref` pointers within the JSON schema.
110
+ */
111
+ bundle: BundleOptions;
91
112
  /**
92
113
  * The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema.
93
114
  */
@@ -48,6 +48,19 @@ const getJsonSchemaRefParserDefaultOptions = () => {
48
48
  * that were encountered.
49
49
  */
50
50
  continueOnError: false,
51
+ /**
52
+ * Determines the types of JSON references that are allowed.
53
+ */
54
+ bundle: {
55
+ /**
56
+ * A function, called for each path, which can return true to stop this path and all
57
+ * subpaths from being processed further. This is useful in schemas where some
58
+ * subpaths contain literal $ref keys that should not be changed.
59
+ *
60
+ * @type {function}
61
+ */
62
+ excludedPathMatcher: () => false,
63
+ },
51
64
  /**
52
65
  * Determines the types of JSON references that are allowed.
53
66
  */
package/lib/bundle.ts CHANGED
@@ -5,6 +5,7 @@ import type $Refs from "./refs.js";
5
5
  import type $RefParser from "./index";
6
6
  import type { ParserOptions } from "./index";
7
7
  import type { JSONSchema } from "./index";
8
+ import type { BundleOptions } from "./options";
8
9
 
9
10
  export interface InventoryEntry {
10
11
  $ref: any;
@@ -65,8 +66,10 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
65
66
  options: O,
66
67
  ) {
67
68
  const obj = key === null ? parent : parent[key as keyof typeof parent];
69
+ const bundleOptions = (options.bundle || {}) as BundleOptions;
70
+ const isExcludedPath = bundleOptions.excludedPathMatcher || (() => false);
68
71
 
69
- if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj)) {
72
+ if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
70
73
  if ($Ref.isAllowed$Ref(obj)) {
71
74
  inventory$Ref(parent, key, path, pathFromRoot, indirections, inventory, $refs, options);
72
75
  } else {
@@ -97,6 +100,14 @@ function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = Parse
97
100
  } else {
98
101
  crawl(obj, key, keyPath, keyPathFromRoot, indirections, inventory, $refs, options);
99
102
  }
103
+
104
+ // We need to ensure that we have an object to work with here because we may be crawling
105
+ // an `examples` schema and `value` may be nullish.
106
+ if (value && typeof value === "object" && !Array.isArray(value)) {
107
+ if ("$ref" in value) {
108
+ bundleOptions?.onBundle?.(value["$ref"], obj[key], obj as any, key);
109
+ }
110
+ }
100
111
  }
101
112
  }
102
113
  }
package/lib/options.ts CHANGED
@@ -12,6 +12,26 @@ export type DeepPartial<T> = T extends object
12
12
  [P in keyof T]?: DeepPartial<T[P]>;
13
13
  }
14
14
  : T;
15
+
16
+ export interface BundleOptions {
17
+ /**
18
+ * A function, called for each path, which can return true to stop this path and all
19
+ * subpaths from being processed further. This is useful in schemas where some
20
+ * subpaths contain literal $ref keys that should not be changed.
21
+ */
22
+ excludedPathMatcher?(path: string): boolean;
23
+
24
+ /**
25
+ * Callback invoked during bundling.
26
+ *
27
+ * @argument {string} path - The path being processed (ie. the `$ref` string)
28
+ * @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
29
+ * @argument {JSONSchemaObject} parent - The parent of the processed object
30
+ * @argument {string} parentPropName - The prop name of the parent object whose value was processed
31
+ */
32
+ onBundle?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
33
+ }
34
+
15
35
  export interface DereferenceOptions {
16
36
  /**
17
37
  * Determines whether circular `$ref` pointers are handled.
@@ -107,6 +127,11 @@ export interface $RefParserOptions<S extends object = JSONSchema> {
107
127
  */
108
128
  continueOnError: boolean;
109
129
 
130
+ /**
131
+ * The `bundle` options control how JSON Schema `$Ref` Parser will process `$ref` pointers within the JSON schema.
132
+ */
133
+ bundle: BundleOptions;
134
+
110
135
  /**
111
136
  * The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema.
112
137
  */
@@ -168,6 +193,20 @@ export const getJsonSchemaRefParserDefaultOptions = () => {
168
193
  */
169
194
  continueOnError: false,
170
195
 
196
+ /**
197
+ * Determines the types of JSON references that are allowed.
198
+ */
199
+ bundle: {
200
+ /**
201
+ * A function, called for each path, which can return true to stop this path and all
202
+ * subpaths from being processed further. This is useful in schemas where some
203
+ * subpaths contain literal $ref keys that should not be changed.
204
+ *
205
+ * @type {function}
206
+ */
207
+ excludedPathMatcher: () => false,
208
+ },
209
+
171
210
  /**
172
211
  * Determines the types of JSON references that are allowed.
173
212
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apidevtools/json-schema-ref-parser",
3
- "version": "14.0.3",
3
+ "version": "14.1.1",
4
4
  "description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
5
5
  "scripts": {
6
6
  "prepublishOnly": "yarn build",