@apidevtools/json-schema-ref-parser 11.4.2 → 11.5.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.
@@ -1,4 +1,6 @@
1
- export default bundle;
1
+ import type $RefParser from "./index";
2
+ import type { ParserOptions } from "./index";
3
+ import type { JSONSchema } from "./index";
2
4
  /**
3
5
  * Bundles all external JSON references into the main JSON schema, thus resulting in a schema that
4
6
  * only has *internal* references, not any *external* references.
@@ -7,4 +9,5 @@ export default bundle;
7
9
  * @param parser
8
10
  * @param options
9
11
  */
10
- declare function bundle(parser: any, options: any): void;
12
+ declare function bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(parser: $RefParser<S, O>, options: O): void;
13
+ export default bundle;
@@ -29,7 +29,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const ref_js_1 = __importDefault(require("./ref.js"));
30
30
  const pointer_js_1 = __importDefault(require("./pointer.js"));
31
31
  const url = __importStar(require("./util/url.js"));
32
- exports.default = bundle;
33
32
  /**
34
33
  * Bundles all external JSON references into the main JSON schema, thus resulting in a schema that
35
34
  * only has *internal* references, not any *external* references.
@@ -53,6 +52,7 @@ function bundle(parser, options) {
53
52
  * @param key - The property key of `parent` to be crawled
54
53
  * @param path - The full path of the property being crawled, possibly with a JSON Pointer in the hash
55
54
  * @param pathFromRoot - The path of the property being crawled, from the schema root
55
+ * @param indirections
56
56
  * @param inventory - An array of already-inventoried $ref pointers
57
57
  * @param $refs
58
58
  * @param options
@@ -251,9 +251,8 @@ function remap(inventory) {
251
251
  * TODO
252
252
  */
253
253
  function findInInventory(inventory, $refParent, $refKey) {
254
- for (let i = 0; i < inventory.length; i++) {
255
- const existingEntry = inventory[i];
256
- if (existingEntry.parent === $refParent && existingEntry.key === $refKey) {
254
+ for (const existingEntry of inventory) {
255
+ if (existingEntry && existingEntry.parent === $refParent && existingEntry.key === $refKey) {
257
256
  return existingEntry;
258
257
  }
259
258
  }
@@ -262,3 +261,4 @@ function removeFromInventory(inventory, entry) {
262
261
  const index = inventory.indexOf(entry);
263
262
  inventory.splice(index, 1);
264
263
  }
264
+ exports.default = bundle;
@@ -1,3 +1,6 @@
1
+ import type { ParserOptions } from "./options.js";
2
+ import type { JSONSchema } from "./types";
3
+ import type $RefParser from "./index";
1
4
  export default dereference;
2
5
  /**
3
6
  * Crawls the JSON schema, finds all JSON references, and dereferences them.
@@ -6,4 +9,4 @@ export default dereference;
6
9
  * @param parser
7
10
  * @param options
8
11
  */
9
- declare function dereference(parser: any, options: any): void;
12
+ declare function dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(parser: $RefParser<S, O>, options: O): void;
@@ -63,8 +63,9 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
63
63
  value: obj,
64
64
  circular: false,
65
65
  };
66
- const isExcludedPath = options.dereference.excludedPathMatcher || (() => false);
67
- if (options.dereference.circular === "ignore" || !processedObjects.has(obj)) {
66
+ const derefOptions = (options.dereference || {});
67
+ const isExcludedPath = derefOptions.excludedPathMatcher || (() => false);
68
+ if (derefOptions?.circular === "ignore" || !processedObjects.has(obj)) {
68
69
  if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
69
70
  parents.add(obj);
70
71
  processedObjects.add(obj);
@@ -88,9 +89,7 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
88
89
  // Avoid pointless mutations; breaks frozen objects to no profit
89
90
  if (obj[key] !== dereferenced.value) {
90
91
  obj[key] = dereferenced.value;
91
- if (options.dereference.onDereference) {
92
- options.dereference.onDereference(value.$ref, obj[key], obj, key);
93
- }
92
+ derefOptions?.onDereference?.(value.$ref, obj[key], obj, key);
94
93
  }
95
94
  }
96
95
  else {
@@ -130,7 +129,7 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
130
129
  */
131
130
  function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options) {
132
131
  const isExternalRef = ref_js_1.default.isExternal$Ref($ref);
133
- const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root";
132
+ const shouldResolveOnCwd = isExternalRef && options?.dereference?.externalReferenceResolution === "root";
134
133
  const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
135
134
  const cache = dereferencedCache.get($refPath);
136
135
  if (cache) {
@@ -170,7 +169,7 @@ function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, de
170
169
  circular = dereferenced.circular;
171
170
  dereferencedValue = dereferenced.value;
172
171
  }
173
- if (circular && !directCircular && options.dereference.circular === "ignore") {
172
+ if (circular && !directCircular && options.dereference?.circular === "ignore") {
174
173
  // The user has chosen to "ignore" circular references, so don't change the value
175
174
  dereferencedValue = $ref;
176
175
  }
@@ -1,7 +1,7 @@
1
1
  import $Refs from "./refs.js";
2
2
  import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } from "./util/errors.js";
3
3
  import type { ParserOptions } from "./options.js";
4
- import type { $RefsCallback, JSONSchema, SchemaCallback } from "./types/index.js";
4
+ import type { $RefsCallback, JSONSchema, SchemaCallback, FileInfo, Plugin, ResolverOptions, HTTPResolverOptions } from "./types/index.js";
5
5
  export type RefParserSchema = string | JSONSchema;
6
6
  /**
7
7
  * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
@@ -9,21 +9,21 @@ export type RefParserSchema = string | JSONSchema;
9
9
  *
10
10
  * @class
11
11
  */
12
- export declare class $RefParser {
12
+ export declare class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions> {
13
13
  /**
14
14
  * The parsed (and possibly dereferenced) JSON schema object
15
15
  *
16
16
  * @type {object}
17
17
  * @readonly
18
18
  */
19
- schema: JSONSchema | null;
19
+ schema: S | null;
20
20
  /**
21
21
  * The resolved JSON references
22
22
  *
23
23
  * @type {$Refs}
24
24
  * @readonly
25
25
  */
26
- $refs: $Refs;
26
+ $refs: $Refs<S>;
27
27
  /**
28
28
  * Parses the given JSON schema.
29
29
  * This method does not resolve any JSON references.
@@ -35,18 +35,18 @@ export declare class $RefParser {
35
35
  * @param [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
36
36
  * @returns - The returned promise resolves with the parsed JSON schema object.
37
37
  */
38
- parse(schema: RefParserSchema): Promise<JSONSchema>;
39
- parse(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
40
- parse(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
41
- parse(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
42
- parse(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
43
- parse(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
44
- static parse(schema: RefParserSchema): Promise<JSONSchema>;
45
- static parse(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
46
- static parse(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
47
- static parse(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
48
- static parse(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
49
- static parse(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
38
+ parse(schema: S | string): Promise<S>;
39
+ parse(schema: S | string, callback: SchemaCallback<S>): Promise<void>;
40
+ parse(schema: S | string, options: O): Promise<S>;
41
+ parse(schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
42
+ parse(baseUrl: string, schema: S | string, options: O): Promise<S>;
43
+ parse(baseUrl: string, schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
44
+ static parse<S extends JSONSchema = JSONSchema>(schema: S | string): Promise<S>;
45
+ static parse<S extends JSONSchema = JSONSchema>(schema: S | string, callback: SchemaCallback<S>): Promise<void>;
46
+ static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, options: O): Promise<S>;
47
+ static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
48
+ static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(baseUrl: string, schema: S | string, options: O): Promise<S>;
49
+ static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(baseUrl: string, schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
50
50
  /**
51
51
  * *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
52
52
  *
@@ -58,12 +58,12 @@ export declare class $RefParser {
58
58
  * @param options (optional)
59
59
  * @param callback (optional) A callback that will receive a `$Refs` object
60
60
  */
61
- resolve(schema: RefParserSchema): Promise<$Refs>;
62
- resolve(schema: RefParserSchema, callback: $RefsCallback): Promise<void>;
63
- resolve(schema: RefParserSchema, options: ParserOptions): Promise<$Refs>;
64
- resolve(schema: RefParserSchema, options: ParserOptions, callback: $RefsCallback): Promise<void>;
65
- resolve(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<$Refs>;
66
- resolve(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: $RefsCallback): Promise<void>;
61
+ resolve(schema: S | string): Promise<$Refs<S>>;
62
+ resolve(schema: S | string, callback: $RefsCallback<S>): Promise<void>;
63
+ resolve(schema: S | string, options: O): Promise<$Refs<S>>;
64
+ resolve(schema: S | string, options: O, callback: $RefsCallback<S>): Promise<void>;
65
+ resolve(baseUrl: string, schema: S | string, options: O): Promise<$Refs<S>>;
66
+ resolve(baseUrl: string, schema: S | string, options: O, callback: $RefsCallback<S>): Promise<void>;
67
67
  /**
68
68
  * *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
69
69
  *
@@ -75,12 +75,12 @@ export declare class $RefParser {
75
75
  * @param options (optional)
76
76
  * @param callback (optional) A callback that will receive a `$Refs` object
77
77
  */
78
- static resolve(schema: RefParserSchema): Promise<$Refs>;
79
- static resolve(schema: RefParserSchema, callback: $RefsCallback): Promise<void>;
80
- static resolve(schema: RefParserSchema, options: ParserOptions): Promise<$Refs>;
81
- static resolve(schema: RefParserSchema, options: ParserOptions, callback: $RefsCallback): Promise<void>;
82
- static resolve(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<$Refs>;
83
- static resolve(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: $RefsCallback): Promise<void>;
78
+ static resolve<S extends JSONSchema = JSONSchema>(schema: S | string): Promise<$Refs<S>>;
79
+ static resolve<S extends JSONSchema = JSONSchema>(schema: S | string, callback: $RefsCallback<S>): Promise<void>;
80
+ static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, options: O): Promise<$Refs<S>>;
81
+ static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, options: O, callback: $RefsCallback<S>): Promise<void>;
82
+ static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(baseUrl: string, schema: S | string, options: O): Promise<$Refs<S>>;
83
+ static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(baseUrl: string, schema: S | string, options: O, callback: $RefsCallback<S>): Promise<void>;
84
84
  /**
85
85
  * Bundles all referenced files/URLs into a single schema that only has internal `$ref` pointers. This lets you split-up your schema however you want while you're building it, but easily combine all those files together when it's time to package or distribute the schema to other people. The resulting schema size will be small, since it will still contain internal JSON references rather than being fully-dereferenced.
86
86
  *
@@ -92,12 +92,12 @@ export declare class $RefParser {
92
92
  * @param options (optional)
93
93
  * @param callback (optional) A callback that will receive the bundled schema object
94
94
  */
95
- static bundle(schema: RefParserSchema): Promise<JSONSchema>;
96
- static bundle(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
97
- static bundle(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
98
- static bundle(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
99
- static bundle(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
100
- static bundle(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<JSONSchema>;
95
+ static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string): Promise<S>;
96
+ static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, callback: SchemaCallback<S>): Promise<void>;
97
+ static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, options: O): Promise<S>;
98
+ static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
99
+ static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(baseUrl: string, schema: S | string, options: O): Promise<S>;
100
+ static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(baseUrl: string, schema: S | string, options: O, callback: SchemaCallback<S>): Promise<S>;
101
101
  /**
102
102
  * Bundles all referenced files/URLs into a single schema that only has internal `$ref` pointers. This lets you split-up your schema however you want while you're building it, but easily combine all those files together when it's time to package or distribute the schema to other people. The resulting schema size will be small, since it will still contain internal JSON references rather than being fully-dereferenced.
103
103
  *
@@ -109,12 +109,12 @@ export declare class $RefParser {
109
109
  * @param options (optional)
110
110
  * @param callback (optional) A callback that will receive the bundled schema object
111
111
  */
112
- bundle(schema: RefParserSchema): Promise<JSONSchema>;
113
- bundle(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
114
- bundle(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
115
- bundle(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
116
- bundle(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
117
- bundle(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
112
+ bundle(schema: S | string): Promise<S>;
113
+ bundle(schema: S | string, callback: SchemaCallback<S>): Promise<void>;
114
+ bundle(schema: S | string, options: O): Promise<S>;
115
+ bundle(schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
116
+ bundle(baseUrl: string, schema: S | string, options: O): Promise<S>;
117
+ bundle(baseUrl: string, schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
118
118
  /**
119
119
  * Dereferences all `$ref` pointers in the JSON Schema, replacing each reference with its resolved value. This results in a schema object that does not contain any `$ref` pointers. Instead, it's a normal JavaScript object tree that can easily be crawled and used just like any other JavaScript object. This is great for programmatic usage, especially when using tools that don't understand JSON references.
120
120
  *
@@ -126,12 +126,12 @@ export declare class $RefParser {
126
126
  * @param options (optional)
127
127
  * @param callback (optional) A callback that will receive the dereferenced schema object
128
128
  */
129
- static dereference(schema: RefParserSchema): Promise<JSONSchema>;
130
- static dereference(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
131
- static dereference(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
132
- static dereference(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
133
- static dereference(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
134
- static dereference(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
129
+ static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string): Promise<S>;
130
+ static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, callback: SchemaCallback<S>): Promise<void>;
131
+ static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, options: O): Promise<S>;
132
+ static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
133
+ static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(baseUrl: string, schema: S | string, options: O): Promise<S>;
134
+ static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(baseUrl: string, schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
135
135
  /**
136
136
  * Dereferences all `$ref` pointers in the JSON Schema, replacing each reference with its resolved value. This results in a schema object that does not contain any `$ref` pointers. Instead, it's a normal JavaScript object tree that can easily be crawled and used just like any other JavaScript object. This is great for programmatic usage, especially when using tools that don't understand JSON references.
137
137
  *
@@ -139,20 +139,21 @@ export declare class $RefParser {
139
139
  *
140
140
  * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferenceschema-options-callback
141
141
  *
142
+ * @param baseUrl
142
143
  * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
143
144
  * @param options (optional)
144
145
  * @param callback (optional) A callback that will receive the dereferenced schema object
145
146
  */
146
- dereference(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
147
- dereference(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
148
- dereference(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
149
- dereference(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
150
- dereference(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
151
- dereference(schema: RefParserSchema): Promise<JSONSchema>;
147
+ dereference(baseUrl: string, schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
148
+ dereference(schema: S | string, options: O, callback: SchemaCallback<S>): Promise<void>;
149
+ dereference(schema: S | string, callback: SchemaCallback<S>): Promise<void>;
150
+ dereference(baseUrl: string, schema: S | string, options: O): Promise<S>;
151
+ dereference(schema: S | string, options: O): Promise<S>;
152
+ dereference(schema: S | string): Promise<S>;
152
153
  }
153
154
  export default $RefParser;
154
155
  export declare const parse: typeof $RefParser.parse;
155
156
  export declare const resolve: typeof $RefParser.resolve;
156
157
  export declare const bundle: typeof $RefParser.bundle;
157
158
  export declare const dereference: typeof $RefParser.dereference;
158
- export { UnmatchedResolverError, JSONParserError, JSONSchema, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, ParserOptions, $RefsCallback, isHandledError, JSONParserErrorGroup, SchemaCallback, };
159
+ export { UnmatchedResolverError, JSONParserError, JSONSchema, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, ParserOptions, $RefsCallback, isHandledError, JSONParserErrorGroup, SchemaCallback, FileInfo, Plugin, ResolverOptions, HTTPResolverOptions, };
@@ -1,13 +1,13 @@
1
+ import type { Options, ParserOptions } from "./options.js";
1
2
  import type { JSONSchema, SchemaCallback } from "./types";
2
- import type $RefParserOptions from "./options";
3
- export interface NormalizedArguments<T = $RefParserOptions> {
3
+ export interface NormalizedArguments<S, O> {
4
4
  path: string;
5
- schema: JSONSchema;
6
- options: T;
7
- callback: SchemaCallback;
5
+ schema: S;
6
+ options: O & Options;
7
+ callback: SchemaCallback<S>;
8
8
  }
9
9
  /**
10
10
  * Normalizes the given arguments, accounting for optional args.
11
11
  */
12
- export declare function normalizeArgs<T = $RefParserOptions>(_args: Partial<IArguments>): NormalizedArguments<T>;
12
+ export declare function normalizeArgs<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(_args: Partial<IArguments>): NormalizedArguments<S, O>;
13
13
  export default normalizeArgs;
@@ -6,7 +6,10 @@ const options_js_1 = require("./options.js");
6
6
  * Normalizes the given arguments, accounting for optional args.
7
7
  */
8
8
  function normalizeArgs(_args) {
9
- let path, schema, options, callback;
9
+ let path;
10
+ let schema;
11
+ let options;
12
+ let callback;
10
13
  const args = Array.prototype.slice.call(_args);
11
14
  if (typeof args[args.length - 1] === "function") {
12
15
  // The last parameter is a callback function
@@ -1,14 +1,45 @@
1
- import type { HTTPResolverOptions, JSONSchemaObject, Plugin, ResolverOptions } from "./types/index.js";
1
+ import type { HTTPResolverOptions, JSONSchema, JSONSchemaObject, Plugin, ResolverOptions } from "./types/index.js";
2
2
  export type DeepPartial<T> = T extends object ? {
3
3
  [P in keyof T]?: DeepPartial<T[P]>;
4
4
  } : T;
5
+ export interface DereferenceOptions {
6
+ /**
7
+ * Determines whether circular `$ref` pointers are handled.
8
+ *
9
+ * If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.
10
+ *
11
+ * If set to `"ignore"`, then circular references will simply be ignored. No error will be thrown, but the `$Refs.circular` property will still be set to `true`.
12
+ */
13
+ circular?: boolean | "ignore";
14
+ /**
15
+ * A function, called for each path, which can return true to stop this path and all
16
+ * subpaths from being dereferenced further. This is useful in schemas where some
17
+ * subpaths contain literal $ref keys that should not be dereferenced.
18
+ */
19
+ excludedPathMatcher?(path: string): boolean;
20
+ /**
21
+ * Callback invoked during dereferencing.
22
+ *
23
+ * @argument {string} path - The path being dereferenced (ie. the `$ref` string)
24
+ * @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
25
+ * @argument {JSONSchemaObject} parent - The parent of the dereferenced object
26
+ * @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced
27
+ */
28
+ onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
29
+ /**
30
+ * Whether a reference should resolve relative to its directory/path, or from the cwd
31
+ *
32
+ * Default: `relative`
33
+ */
34
+ externalReferenceResolution?: "relative" | "root";
35
+ }
5
36
  /**
6
37
  * Options that determine how JSON schemas are parsed, resolved, and dereferenced.
7
38
  *
8
39
  * @param [options] - Overridden options
9
40
  * @class
10
41
  */
11
- export interface $RefParserOptions {
42
+ export interface $RefParserOptions<S> {
12
43
  /**
13
44
  * The `parse` options determine how different types of files will be parsed.
14
45
  *
@@ -33,10 +64,10 @@ export interface $RefParserOptions {
33
64
  * Determines whether external $ref pointers will be resolved. If this option is disabled, then external `$ref` pointers will simply be ignored.
34
65
  */
35
66
  external?: boolean;
36
- file?: Partial<ResolverOptions> | boolean;
37
- http?: HTTPResolverOptions | boolean;
67
+ file?: Partial<ResolverOptions<S>> | boolean;
68
+ http?: HTTPResolverOptions<S> | boolean;
38
69
  } & {
39
- [key: string]: Partial<ResolverOptions> | HTTPResolverOptions | boolean | undefined;
70
+ [key: string]: Partial<ResolverOptions<S>> | HTTPResolverOptions<S> | boolean | undefined;
40
71
  };
41
72
  /**
42
73
  * By default, JSON Schema $Ref Parser throws the first error it encounters. Setting `continueOnError` to `true`
@@ -47,46 +78,16 @@ export interface $RefParserOptions {
47
78
  /**
48
79
  * The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema.
49
80
  */
50
- dereference: {
51
- /**
52
- * Determines whether circular `$ref` pointers are handled.
53
- *
54
- * If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.
55
- *
56
- * If set to `"ignore"`, then circular references will simply be ignored. No error will be thrown, but the `$Refs.circular` property will still be set to `true`.
57
- */
58
- circular?: boolean | "ignore";
59
- /**
60
- * A function, called for each path, which can return true to stop this path and all
61
- * subpaths from being dereferenced further. This is useful in schemas where some
62
- * subpaths contain literal $ref keys that should not be dereferenced.
63
- */
64
- excludedPathMatcher?(path: string): boolean;
65
- /**
66
- * Callback invoked during dereferencing.
67
- *
68
- * @argument {string} path - The path being dereferenced (ie. the `$ref` string)
69
- * @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
70
- * @argument {JSONSchemaObject} parent - The parent of the dereferenced object
71
- * @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced
72
- */
73
- onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
74
- /**
75
- * Whether a reference should resolve relative to its directory/path, or from the cwd
76
- *
77
- * Default: `relative`
78
- */
79
- externalReferenceResolution?: "relative" | "root";
80
- /**
81
- * Whether to clone the schema before dereferencing it.
82
- * This is useful when you want to dereference the same schema multiple times, but you don't want to modify the original schema.
83
- * Default: `true` due to mutating the input being the default behavior historically
84
- */
85
- mutateInputSchema?: boolean;
86
- };
81
+ dereference: DereferenceOptions;
82
+ /**
83
+ * Whether to clone the schema before dereferencing it.
84
+ * This is useful when you want to dereference the same schema multiple times, but you don't want to modify the original schema.
85
+ * Default: `true` due to mutating the input being the default behavior historically
86
+ */
87
+ mutateInputSchema?: boolean;
87
88
  }
88
- export declare const getJsonSchemaRefParserDefaultOptions: () => $RefParserOptions;
89
- export declare const getNewOptions: (options: DeepPartial<$RefParserOptions> | undefined) => $RefParserOptions;
90
- export type Options = $RefParserOptions;
91
- export type ParserOptions = DeepPartial<$RefParserOptions>;
89
+ export declare const getJsonSchemaRefParserDefaultOptions: () => $RefParserOptions<JSONSchema>;
90
+ export declare const getNewOptions: <S, O>(options: O | undefined) => O & $RefParserOptions<S>;
91
+ export type Options = $RefParserOptions<JSONSchema>;
92
+ export type ParserOptions = DeepPartial<$RefParserOptions<JSONSchema>>;
92
93
  export default $RefParserOptions;
@@ -1,8 +1,9 @@
1
1
  /// <reference types="node" />
2
2
  import type $Refs from "./refs.js";
3
3
  import type { Options } from "./options.js";
4
- export default parse;
4
+ import type { JSONSchema } from "./types/index.js";
5
5
  /**
6
6
  * Reads and parses the specified file path or URL.
7
7
  */
8
- declare function parse(path: string, $refs: $Refs, options: Options): Promise<string | import("./types/index.js").JSONSchema | Buffer | undefined>;
8
+ declare function parse<S extends JSONSchema = JSONSchema, O extends Options = Options>(path: string, $refs: $Refs<S>, options: O): Promise<string | Buffer | S | undefined>;
9
+ export default parse;
package/dist/lib/parse.js CHANGED
@@ -27,7 +27,6 @@ const ono_1 = require("@jsdevtools/ono");
27
27
  const url = __importStar(require("./util/url.js"));
28
28
  const plugins = __importStar(require("./util/plugins.js"));
29
29
  const errors_js_1 = require("./util/errors.js");
30
- exports.default = parse;
31
30
  /**
32
31
  * Reads and parses the specified file path or URL.
33
32
  */
@@ -72,7 +71,7 @@ async function parse(path, $refs, options) {
72
71
  * @param file.url - The full URL of the referenced file
73
72
  * @param file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
74
73
  * @param options
75
- *
74
+ * @param $refs
76
75
  * @returns
77
76
  * The promise resolves with the raw file contents and the resolver that was used.
78
77
  */
@@ -113,6 +112,7 @@ async function readFile(file, options, $refs) {
113
112
  * @param file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
114
113
  * @param file.data - The file contents. This will be whatever data type was returned by the resolver
115
114
  * @param options
115
+ * @param $refs
116
116
  *
117
117
  * @returns
118
118
  * The promise resolves with the parsed file contents and the parser that was used.
@@ -166,3 +166,4 @@ function isEmpty(value) {
166
166
  (typeof value === "string" && value.trim().length === 0) ||
167
167
  (Buffer.isBuffer(value) && value.length === 0));
168
168
  }
169
+ exports.default = parse;
@@ -1,5 +1,6 @@
1
1
  import type $RefParserOptions from "./options.js";
2
2
  import $Ref from "./ref.js";
3
+ import type { JSONSchema } from "./types";
3
4
  /**
4
5
  * This class represents a single JSON pointer and its resolved value.
5
6
  *
@@ -8,11 +9,11 @@ import $Ref from "./ref.js";
8
9
  * @param [friendlyPath] - The original user-specified path (used for error messages)
9
10
  * @class
10
11
  */
11
- declare class Pointer {
12
+ declare class Pointer<S = JSONSchema> {
12
13
  /**
13
14
  * The {@link $Ref} object that contains this {@link Pointer} object.
14
15
  */
15
- $ref: $Ref;
16
+ $ref: $Ref<S>;
16
17
  /**
17
18
  * The file path or URL, containing the JSON pointer in the hash.
18
19
  * This path is relative to the path of the main JSON schema file.
@@ -36,7 +37,7 @@ declare class Pointer {
36
37
  * Resolving a single pointer may require resolving multiple $Refs.
37
38
  */
38
39
  indirections: number;
39
- constructor($ref: $Ref, path: string, friendlyPath?: string);
40
+ constructor($ref: $Ref<S>, path: string, friendlyPath?: string);
40
41
  /**
41
42
  * Resolves the value of a nested property within the given object.
42
43
  *
@@ -50,7 +51,7 @@ declare class Pointer {
50
51
  * the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
51
52
  * of the resolved value.
52
53
  */
53
- resolve(obj: any, options?: $RefParserOptions, pathFromRoot?: string): this;
54
+ resolve(obj: any, options?: $RefParserOptions<S>, pathFromRoot?: string): this;
54
55
  /**
55
56
  * Sets the value of a nested property within the given object.
56
57
  *
@@ -61,7 +62,7 @@ declare class Pointer {
61
62
  * @returns
62
63
  * Returns the modified object, or an entirely new object if the entire object is overwritten.
63
64
  */
64
- set(obj: any, value: any, options?: $RefParserOptions): any;
65
+ set(obj: any, value: any, options?: $RefParserOptions<S>): any;
65
66
  /**
66
67
  * Parses a JSON pointer (or a path containing a JSON pointer in the hash)
67
68
  * and returns an array of the pointer's tokens.
package/dist/lib/ref.d.ts CHANGED
@@ -2,6 +2,7 @@ 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 $RefParserOptions from "./options.js";
5
+ import type { ParserOptions } from "./options.js";
5
6
  import type { JSONSchema } from "./types";
6
7
  export type $RefError = JSONParserError | ResolverError | ParserError | MissingPointerError;
7
8
  /**
@@ -9,7 +10,7 @@ export type $RefError = JSONParserError | ResolverError | ParserError | MissingP
9
10
  *
10
11
  * @class
11
12
  */
12
- declare class $Ref {
13
+ declare class $Ref<S = JSONSchema> {
13
14
  /**
14
15
  * The file path or URL of the referenced file.
15
16
  * This path is relative to the path of the main JSON schema file.
@@ -33,7 +34,7 @@ declare class $Ref {
33
34
  *
34
35
  * @type {$Refs}
35
36
  */
36
- $refs: $Refs;
37
+ $refs: $Refs<S>;
37
38
  /**
38
39
  * Indicates the type of {@link $Ref#path} (e.g. "file", "http", etc.)
39
40
  */
@@ -42,7 +43,7 @@ declare class $Ref {
42
43
  * List of all errors. Undefined if no errors.
43
44
  */
44
45
  errors: Array<$RefError>;
45
- constructor($refs: $Refs);
46
+ constructor($refs: $Refs<S>);
46
47
  /**
47
48
  * Pushes an error to errors array.
48
49
  *
@@ -57,7 +58,7 @@ declare class $Ref {
57
58
  * @param options
58
59
  * @returns
59
60
  */
60
- exists(path: string, options?: $RefParserOptions): boolean;
61
+ exists(path: string, options?: $RefParserOptions<S>): boolean;
61
62
  /**
62
63
  * Resolves the given JSON reference within this {@link $Ref#value} and returns the resolved value.
63
64
  *
@@ -65,7 +66,7 @@ declare class $Ref {
65
66
  * @param options
66
67
  * @returns - Returns the resolved value
67
68
  */
68
- get(path: any, options: $RefParserOptions): any;
69
+ get(path: string, options?: $RefParserOptions<S>): any;
69
70
  /**
70
71
  * Resolves the given JSON reference within this {@link $Ref#value}.
71
72
  *
@@ -75,7 +76,7 @@ declare class $Ref {
75
76
  * @param pathFromRoot - The path of `obj` from the schema root
76
77
  * @returns
77
78
  */
78
- resolve(path: any, options?: $RefParserOptions, friendlyPath?: string, pathFromRoot?: string): Pointer | null;
79
+ resolve(path: string, options?: $RefParserOptions<S>, friendlyPath?: string, pathFromRoot?: string): Pointer<S> | null;
79
80
  /**
80
81
  * Sets the value of a nested property within this {@link $Ref#value}.
81
82
  * If the property, or any of its parents don't exist, they will be created.
@@ -109,7 +110,7 @@ declare class $Ref {
109
110
  * @param options
110
111
  * @returns
111
112
  */
112
- static isAllowed$Ref(value: unknown, options?: $RefParserOptions): true | undefined;
113
+ static isAllowed$Ref(value: unknown, options?: ParserOptions): true | undefined;
113
114
  /**
114
115
  * Determines whether the given value is a JSON reference that "extends" its resolved value.
115
116
  * That is, it has extra properties (in addition to "$ref"), so rather than simply pointing to
@@ -176,6 +177,6 @@ declare class $Ref {
176
177
  * @param resolvedValue - The resolved value, which can be any type
177
178
  * @returns - Returns the dereferenced value
178
179
  */
179
- static dereference($ref: $Ref, resolvedValue: JSONSchema): JSONSchema;
180
+ static dereference<S>($ref: $Ref<S>, resolvedValue: S): S;
180
181
  }
181
182
  export default $Ref;
package/dist/lib/ref.js CHANGED
@@ -142,7 +142,7 @@ class $Ref {
142
142
  // It's a JSON Pointer reference, which is always allowed
143
143
  return true;
144
144
  }
145
- else if (value.$ref[0] !== "#" && (!options || options.resolve.external)) {
145
+ else if (value.$ref[0] !== "#" && (!options || options.resolve?.external)) {
146
146
  // It's an external reference, which is allowed by the options
147
147
  return true;
148
148
  }