@apidevtools/json-schema-ref-parser 11.5.2 → 11.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.ts CHANGED
@@ -37,7 +37,7 @@ export type RefParserSchema = string | JSONSchema;
37
37
  *
38
38
  * @class
39
39
  */
40
- export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions> {
40
+ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
41
41
  /**
42
42
  * The parsed (and possibly dereferenced) JSON schema object
43
43
  *
@@ -52,7 +52,7 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
52
52
  * @type {$Refs}
53
53
  * @readonly
54
54
  */
55
- $refs = new $Refs<S>();
55
+ $refs = new $Refs<S, O>();
56
56
 
57
57
  /**
58
58
  * Parses the given JSON schema.
@@ -143,32 +143,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
143
143
  }
144
144
  }
145
145
 
146
- public static parse<S extends JSONSchema = JSONSchema>(schema: S | string): Promise<S>;
147
- public static parse<S extends JSONSchema = JSONSchema>(
146
+ public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
147
+ schema: S | string,
148
+ ): Promise<S>;
149
+ public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
148
150
  schema: S | string,
149
151
  callback: SchemaCallback<S>,
150
152
  ): Promise<void>;
151
- public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
153
+ public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
152
154
  schema: S | string,
153
155
  options: O,
154
156
  ): Promise<S>;
155
- public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
157
+ public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
156
158
  schema: S | string,
157
159
  options: O,
158
160
  callback: SchemaCallback<S>,
159
161
  ): Promise<void>;
160
- public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
162
+ public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
161
163
  baseUrl: string,
162
164
  schema: S | string,
163
165
  options: O,
164
166
  ): Promise<S>;
165
- public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
167
+ public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
166
168
  baseUrl: string,
167
169
  schema: S | string,
168
170
  options: O,
169
171
  callback: SchemaCallback<S>,
170
172
  ): Promise<void>;
171
- public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>():
173
+ public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
172
174
  | Promise<S>
173
175
  | Promise<void> {
174
176
  const parser = new $RefParser<S, O>();
@@ -186,12 +188,12 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
186
188
  * @param options (optional)
187
189
  * @param callback (optional) A callback that will receive a `$Refs` object
188
190
  */
189
- public resolve(schema: S | string): Promise<$Refs<S>>;
190
- public resolve(schema: S | string, callback: $RefsCallback<S>): Promise<void>;
191
- public resolve(schema: S | string, options: O): Promise<$Refs<S>>;
192
- public resolve(schema: S | string, options: O, callback: $RefsCallback<S>): Promise<void>;
193
- public resolve(baseUrl: string, schema: S | string, options: O): Promise<$Refs<S>>;
194
- public resolve(baseUrl: string, schema: S | string, options: O, callback: $RefsCallback<S>): Promise<void>;
191
+ public resolve(schema: S | string): Promise<$Refs<S, O>>;
192
+ public resolve(schema: S | string, callback: $RefsCallback<S, O>): Promise<void>;
193
+ public resolve(schema: S | string, options: O): Promise<$Refs<S, O>>;
194
+ public resolve(schema: S | string, options: O, callback: $RefsCallback<S, O>): Promise<void>;
195
+ public resolve(baseUrl: string, schema: S | string, options: O): Promise<$Refs<S, O>>;
196
+ public resolve(baseUrl: string, schema: S | string, options: O, callback: $RefsCallback<S, O>): Promise<void>;
195
197
  async resolve() {
196
198
  const args = normalizeArgs<S, O>(arguments);
197
199
 
@@ -216,32 +218,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
216
218
  * @param options (optional)
217
219
  * @param callback (optional) A callback that will receive a `$Refs` object
218
220
  */
219
- public static resolve<S extends JSONSchema = JSONSchema>(schema: S | string): Promise<$Refs<S>>;
220
- public static resolve<S extends JSONSchema = JSONSchema>(
221
+ public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
222
+ schema: S | string,
223
+ ): Promise<$Refs<S, O>>;
224
+ public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
221
225
  schema: S | string,
222
- callback: $RefsCallback<S>,
226
+ callback: $RefsCallback<S, O>,
223
227
  ): Promise<void>;
224
- public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
228
+ public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
225
229
  schema: S | string,
226
230
  options: O,
227
- ): Promise<$Refs<S>>;
228
- public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
231
+ ): Promise<$Refs<S, O>>;
232
+ public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
229
233
  schema: S | string,
230
234
  options: O,
231
- callback: $RefsCallback<S>,
235
+ callback: $RefsCallback<S, O>,
232
236
  ): Promise<void>;
233
- public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
237
+ public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
234
238
  baseUrl: string,
235
239
  schema: S | string,
236
240
  options: O,
237
- ): Promise<$Refs<S>>;
238
- public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
241
+ ): Promise<$Refs<S, O>>;
242
+ public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
239
243
  baseUrl: string,
240
244
  schema: S | string,
241
245
  options: O,
242
- callback: $RefsCallback<S>,
246
+ callback: $RefsCallback<S, O>,
243
247
  ): Promise<void>;
244
- static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>():
248
+ static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
245
249
  | Promise<S>
246
250
  | Promise<void> {
247
251
  const instance = new $RefParser<S, O>();
@@ -259,34 +263,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
259
263
  * @param options (optional)
260
264
  * @param callback (optional) A callback that will receive the bundled schema object
261
265
  */
262
- public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
266
+ public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
263
267
  schema: S | string,
264
268
  ): Promise<S>;
265
- public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
269
+ public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
266
270
  schema: S | string,
267
271
  callback: SchemaCallback<S>,
268
272
  ): Promise<void>;
269
- public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
273
+ public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
270
274
  schema: S | string,
271
275
  options: O,
272
276
  ): Promise<S>;
273
- public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
277
+ public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
274
278
  schema: S | string,
275
279
  options: O,
276
280
  callback: SchemaCallback<S>,
277
281
  ): Promise<void>;
278
- public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
282
+ public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
279
283
  baseUrl: string,
280
284
  schema: S | string,
281
285
  options: O,
282
286
  ): Promise<S>;
283
- public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
287
+ public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
284
288
  baseUrl: string,
285
289
  schema: S | string,
286
290
  options: O,
287
291
  callback: SchemaCallback<S>,
288
292
  ): Promise<S>;
289
- static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>():
293
+ static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
290
294
  | Promise<S>
291
295
  | Promise<void> {
292
296
  const instance = new $RefParser<S, O>();
@@ -333,34 +337,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
333
337
  * @param options (optional)
334
338
  * @param callback (optional) A callback that will receive the dereferenced schema object
335
339
  */
336
- public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
340
+ public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
337
341
  schema: S | string,
338
342
  ): Promise<S>;
339
- public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
343
+ public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
340
344
  schema: S | string,
341
345
  callback: SchemaCallback<S>,
342
346
  ): Promise<void>;
343
- public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
347
+ public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
344
348
  schema: S | string,
345
349
  options: O,
346
350
  ): Promise<S>;
347
- public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
351
+ public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
348
352
  schema: S | string,
349
353
  options: O,
350
354
  callback: SchemaCallback<S>,
351
355
  ): Promise<void>;
352
- public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
356
+ public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
353
357
  baseUrl: string,
354
358
  schema: S | string,
355
359
  options: O,
356
360
  ): Promise<S>;
357
- public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
361
+ public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
358
362
  baseUrl: string,
359
363
  schema: S | string,
360
364
  options: O,
361
365
  callback: SchemaCallback<S>,
362
366
  ): Promise<void>;
363
- static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>():
367
+ static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
364
368
  | Promise<S>
365
369
  | Promise<void> {
366
370
  const instance = new $RefParser<S, O>();
@@ -400,7 +404,7 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
400
404
  }
401
405
  export default $RefParser;
402
406
 
403
- function finalize<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
407
+ function finalize<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
404
408
  parser: $RefParser<S, O>,
405
409
  ) {
406
410
  const errors = JSONParserErrorGroup.getParserErrors(parser);
@@ -4,21 +4,21 @@ import type { JSONSchema, SchemaCallback } from "./types";
4
4
 
5
5
  // I really dislike this function and the way it's written. It's not clear what it's doing, and it's way too flexible
6
6
  // In the future, I'd like to deprecate the api and accept only named parameters in index.ts
7
- export interface NormalizedArguments<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions> {
7
+ export interface NormalizedArguments<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
8
8
  path: string;
9
9
  schema: S;
10
- options: O & Options;
10
+ options: O & Options<S>;
11
11
  callback: SchemaCallback<S>;
12
12
  }
13
13
  /**
14
14
  * Normalizes the given arguments, accounting for optional args.
15
15
  */
16
- export function normalizeArgs<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
16
+ export function normalizeArgs<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
17
17
  _args: Partial<IArguments>,
18
18
  ): NormalizedArguments<S, O> {
19
19
  let path;
20
20
  let schema;
21
- let options: Options & O;
21
+ let options: Options<S> & O;
22
22
  let callback;
23
23
  const args = Array.prototype.slice.call(_args) as any[];
24
24
 
package/lib/options.ts CHANGED
@@ -174,7 +174,7 @@ export const getJsonSchemaRefParserDefaultOptions = () => {
174
174
  return defaults;
175
175
  };
176
176
 
177
- export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
177
+ export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
178
178
  options: O | undefined,
179
179
  ): O & $RefParserOptions<S> => {
180
180
  const newOptions = getJsonSchemaRefParserDefaultOptions();
@@ -183,8 +183,9 @@ export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends Parse
183
183
  }
184
184
  return newOptions as O & $RefParserOptions<S>;
185
185
  };
186
- export type Options = $RefParserOptions<JSONSchema>;
187
- export type ParserOptions = DeepPartial<$RefParserOptions<JSONSchema>>;
186
+
187
+ export type Options<S extends JSONSchema = JSONSchema> = $RefParserOptions<S>;
188
+ export type ParserOptions<S extends JSONSchema = JSONSchema> = DeepPartial<$RefParserOptions<S>>;
188
189
  /**
189
190
  * Merges the properties of the source object into the target object.
190
191
  *
package/lib/parse.ts CHANGED
@@ -9,15 +9,15 @@ import {
9
9
  isHandledError,
10
10
  } from "./util/errors.js";
11
11
  import type $Refs from "./refs.js";
12
- import type { Options } from "./options.js";
12
+ import type { ParserOptions } from "./options.js";
13
13
  import type { FileInfo, JSONSchema } from "./types/index.js";
14
14
 
15
15
  /**
16
16
  * Reads and parses the specified file path or URL.
17
17
  */
18
- async function parse<S extends JSONSchema = JSONSchema, O extends Options = Options>(
18
+ async function parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
19
19
  path: string,
20
- $refs: $Refs<S>,
20
+ $refs: $Refs<S, O>,
21
21
  options: O,
22
22
  ) {
23
23
  // Remove the URL fragment, if any
@@ -70,10 +70,10 @@ async function parse<S extends JSONSchema = JSONSchema, O extends Options = Opti
70
70
  * @returns
71
71
  * The promise resolves with the raw file contents and the resolver that was used.
72
72
  */
73
- async function readFile<S extends JSONSchema = JSONSchema, O extends Options = Options>(
73
+ async function readFile<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
74
74
  file: FileInfo,
75
75
  options: O,
76
- $refs: $Refs<S>,
76
+ $refs: $Refs<S, O>,
77
77
  ): Promise<any> {
78
78
  // console.log('Reading %s', file.url);
79
79
 
@@ -116,10 +116,10 @@ async function readFile<S extends JSONSchema = JSONSchema, O extends Options = O
116
116
  * @returns
117
117
  * The promise resolves with the parsed file contents and the parser that was used.
118
118
  */
119
- async function parseFile<S extends JSONSchema = JSONSchema, O extends Options = Options>(
119
+ async function parseFile<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
120
120
  file: FileInfo,
121
121
  options: O,
122
- $refs: $Refs<S>,
122
+ $refs: $Refs<S, O>,
123
123
  ) {
124
124
  // Find the parsers that can read this file type.
125
125
  // If none of the parsers are an exact match for this file, then we'll try ALL of them.
@@ -131,7 +131,7 @@ async function parseFile<S extends JSONSchema = JSONSchema, O extends Options =
131
131
  // Run the parsers, in order, until one of them succeeds
132
132
  plugins.sort(parsers);
133
133
  try {
134
- const parser = await plugins.run<S>(parsers, "parse", file, $refs);
134
+ const parser = await plugins.run<S, O>(parsers, "parse", file, $refs);
135
135
  if (!parser.plugin.allowEmpty && isEmpty(parser.result)) {
136
136
  throw ono.syntax(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`);
137
137
  } else {
package/lib/pointer.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type $RefParserOptions from "./options.js";
1
+ import type { ParserOptions } from "./options.js";
2
2
 
3
3
  import $Ref from "./ref.js";
4
4
  import * as url from "./util/url.js";
@@ -26,11 +26,11 @@ const safeDecodeURIComponent = (encodedURIComponent: string): string => {
26
26
  * @param [friendlyPath] - The original user-specified path (used for error messages)
27
27
  * @class
28
28
  */
29
- class Pointer<S extends JSONSchema = JSONSchema> {
29
+ class Pointer<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
30
30
  /**
31
31
  * The {@link $Ref} object that contains this {@link Pointer} object.
32
32
  */
33
- $ref: $Ref<S>;
33
+ $ref: $Ref<S, O>;
34
34
 
35
35
  /**
36
36
  * The file path or URL, containing the JSON pointer in the hash.
@@ -59,7 +59,7 @@ class Pointer<S extends JSONSchema = JSONSchema> {
59
59
  */
60
60
  indirections: number;
61
61
 
62
- constructor($ref: $Ref<S>, path: string, friendlyPath?: string) {
62
+ constructor($ref: $Ref<S, O>, path: string, friendlyPath?: string) {
63
63
  this.$ref = $ref;
64
64
 
65
65
  this.path = path;
@@ -86,7 +86,7 @@ class Pointer<S extends JSONSchema = JSONSchema> {
86
86
  * the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
87
87
  * of the resolved value.
88
88
  */
89
- resolve(obj: any, options?: $RefParserOptions<S>, pathFromRoot?: string) {
89
+ resolve(obj: any, options?: O, pathFromRoot?: string) {
90
90
  const tokens = Pointer.parse(this.path, this.originalPath);
91
91
 
92
92
  // Crawl the object, one token at a time
@@ -144,7 +144,7 @@ class Pointer<S extends JSONSchema = JSONSchema> {
144
144
  * @returns
145
145
  * Returns the modified object, or an entirely new object if the entire object is overwritten.
146
146
  */
147
- set(obj: any, value: any, options?: $RefParserOptions<S>) {
147
+ set(obj: any, value: any, options?: O) {
148
148
  const tokens = Pointer.parse(this.path);
149
149
  let token;
150
150
 
package/lib/ref.ts CHANGED
@@ -3,7 +3,6 @@ import type { JSONParserError, MissingPointerError, ParserError, ResolverError }
3
3
  import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js";
4
4
  import { safePointerToPath, stripHash, getHash } from "./util/url.js";
5
5
  import type $Refs from "./refs.js";
6
- import type $RefParserOptions from "./options.js";
7
6
  import type { ParserOptions } from "./options.js";
8
7
  import type { JSONSchema } from "./types";
9
8
 
@@ -14,7 +13,7 @@ export type $RefError = JSONParserError | ResolverError | ParserError | MissingP
14
13
  *
15
14
  * @class
16
15
  */
17
- class $Ref<S extends JSONSchema = JSONSchema> {
16
+ class $Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
18
17
  /**
19
18
  * The file path or URL of the referenced file.
20
19
  * This path is relative to the path of the main JSON schema file.
@@ -40,7 +39,7 @@ class $Ref<S extends JSONSchema = JSONSchema> {
40
39
  *
41
40
  * @type {$Refs}
42
41
  */
43
- $refs: $Refs<S>;
42
+ $refs: $Refs<S, O>;
44
43
 
45
44
  /**
46
45
  * Indicates the type of {@link $Ref#path} (e.g. "file", "http", etc.)
@@ -52,7 +51,7 @@ class $Ref<S extends JSONSchema = JSONSchema> {
52
51
  */
53
52
  errors: Array<$RefError> = [];
54
53
 
55
- constructor($refs: $Refs<S>) {
54
+ constructor($refs: $Refs<S, O>) {
56
55
  this.$refs = $refs;
57
56
  }
58
57
 
@@ -88,7 +87,7 @@ class $Ref<S extends JSONSchema = JSONSchema> {
88
87
  * @param options
89
88
  * @returns
90
89
  */
91
- exists(path: string, options?: $RefParserOptions<S>) {
90
+ exists(path: string, options?: O) {
92
91
  try {
93
92
  this.resolve(path, options);
94
93
  return true;
@@ -104,7 +103,7 @@ class $Ref<S extends JSONSchema = JSONSchema> {
104
103
  * @param options
105
104
  * @returns - Returns the resolved value
106
105
  */
107
- get(path: string, options?: $RefParserOptions<S>) {
106
+ get(path: string, options?: O) {
108
107
  return this.resolve(path, options)?.value;
109
108
  }
110
109
 
@@ -117,8 +116,8 @@ class $Ref<S extends JSONSchema = JSONSchema> {
117
116
  * @param pathFromRoot - The path of `obj` from the schema root
118
117
  * @returns
119
118
  */
120
- resolve(path: string, options?: $RefParserOptions<S>, friendlyPath?: string, pathFromRoot?: string) {
121
- const pointer = new Pointer<S>(this, path, friendlyPath);
119
+ resolve(path: string, options?: O, friendlyPath?: string, pathFromRoot?: string) {
120
+ const pointer = new Pointer<S, O>(this, path, friendlyPath);
122
121
  try {
123
122
  return pointer.resolve(this.value, options, pathFromRoot);
124
123
  } catch (err: any) {
@@ -186,7 +185,7 @@ class $Ref<S extends JSONSchema = JSONSchema> {
186
185
  * @param options
187
186
  * @returns
188
187
  */
189
- static isAllowed$Ref(value: unknown, options?: ParserOptions) {
188
+ static isAllowed$Ref<S extends JSONSchema = JSONSchema>(value: unknown, options?: ParserOptions<S>) {
190
189
  if (this.is$Ref(value)) {
191
190
  if (value.$ref.substring(0, 2) === "#/" || value.$ref === "#") {
192
191
  // It's a JSON Pointer reference, which is always allowed
@@ -267,7 +266,10 @@ class $Ref<S extends JSONSchema = JSONSchema> {
267
266
  * @param resolvedValue - The resolved value, which can be any type
268
267
  * @returns - Returns the dereferenced value
269
268
  */
270
- static dereference<S extends JSONSchema = JSONSchema>($ref: $Ref<S>, resolvedValue: S): S {
269
+ static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
270
+ $ref: $Ref<S, O>,
271
+ resolvedValue: S,
272
+ ): S {
271
273
  if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) {
272
274
  const merged = {};
273
275
  for (const key of Object.keys($ref)) {
package/lib/refs.ts CHANGED
@@ -2,12 +2,12 @@ import { ono } from "@jsdevtools/ono";
2
2
  import $Ref from "./ref.js";
3
3
  import * as url from "./util/url.js";
4
4
  import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
5
- import type $RefParserOptions from "./options.js";
5
+ import type { ParserOptions } from "./options.js";
6
6
  import convertPathToPosix from "./util/convert-path-to-posix";
7
7
  import type { JSONSchema } from "./types";
8
8
 
9
- interface $RefsMap<S extends JSONSchema = JSONSchema> {
10
- [url: string]: $Ref<S>;
9
+ interface $RefsMap<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
10
+ [url: string]: $Ref<S, O>;
11
11
  }
12
12
  /**
13
13
  * 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.
@@ -16,7 +16,7 @@ interface $RefsMap<S extends JSONSchema = JSONSchema> {
16
16
  *
17
17
  * See https://apitools.dev/json-schema-ref-parser/docs/refs.html
18
18
  */
19
- export default class $Refs<S extends JSONSchema = JSONSchema> {
19
+ export default class $Refs<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
20
20
  /**
21
21
  * 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.
22
22
  *
@@ -84,7 +84,7 @@ export default class $Refs<S extends JSONSchema = JSONSchema> {
84
84
  * @param [options]
85
85
  * @returns - Returns the resolved value
86
86
  */
87
- get(path: string, options?: $RefParserOptions<S>): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type {
87
+ get(path: string, options?: O): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type {
88
88
  return this._resolve(path, "", options)!.value;
89
89
  }
90
90
 
@@ -112,7 +112,7 @@ export default class $Refs<S extends JSONSchema = JSONSchema> {
112
112
  * @returns
113
113
  * @protected
114
114
  */
115
- _get$Ref(path: any) {
115
+ _get$Ref(path: string) {
116
116
  path = url.resolve(this._root$Ref.path!, path);
117
117
  const withoutHash = url.stripHash(path);
118
118
  return this._$refs[withoutHash];
@@ -126,7 +126,7 @@ export default class $Refs<S extends JSONSchema = JSONSchema> {
126
126
  _add(path: string) {
127
127
  const withoutHash = url.stripHash(path);
128
128
 
129
- const $ref = new $Ref<S>(this);
129
+ const $ref = new $Ref<S, O>(this);
130
130
  $ref.path = withoutHash;
131
131
 
132
132
  this._$refs[withoutHash] = $ref;
@@ -144,7 +144,7 @@ export default class $Refs<S extends JSONSchema = JSONSchema> {
144
144
  * @returns
145
145
  * @protected
146
146
  */
147
- _resolve(path: string, pathFromRoot: string, options?: any) {
147
+ _resolve(path: string, pathFromRoot: string, options?: O) {
148
148
  const absPath = url.resolve(this._root$Ref.path!, path);
149
149
  const withoutHash = url.stripHash(absPath);
150
150
  const $ref = this._$refs[withoutHash];
@@ -162,7 +162,7 @@ export default class $Refs<S extends JSONSchema = JSONSchema> {
162
162
  * @type {object}
163
163
  * @protected
164
164
  */
165
- _$refs: $RefsMap<S> = {};
165
+ _$refs: $RefsMap<S, O> = {};
166
166
 
167
167
  /**
168
168
  * The {@link $Ref} object that is the root of the JSON schema.
@@ -170,7 +170,7 @@ export default class $Refs<S extends JSONSchema = JSONSchema> {
170
170
  * @type {$Ref}
171
171
  * @protected
172
172
  */
173
- _root$Ref: $Ref<S>;
173
+ _root$Ref: $Ref<S, O>;
174
174
 
175
175
  constructor() {
176
176
  /**
@@ -215,7 +215,10 @@ export default class $Refs<S extends JSONSchema = JSONSchema> {
215
215
  * @param [types] - Only return paths of the given types ("file", "http", etc.)
216
216
  * @returns
217
217
  */
218
- function getPaths<S extends JSONSchema = JSONSchema>($refs: $RefsMap<S>, types: string[]) {
218
+ function getPaths<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
219
+ $refs: $RefsMap<S, O>,
220
+ types: string[],
221
+ ) {
219
222
  let paths = Object.keys($refs);
220
223
 
221
224
  // Filter the paths by type
@@ -4,7 +4,7 @@ import parse from "./parse.js";
4
4
  import * as url from "./util/url.js";
5
5
  import { isHandledError } from "./util/errors.js";
6
6
  import type $Refs from "./refs.js";
7
- import type { Options, ParserOptions } from "./options.js";
7
+ import type { ParserOptions } from "./options.js";
8
8
  import type { JSONSchema } from "./types/index.js";
9
9
  import type $RefParser from "./index.js";
10
10
 
@@ -18,11 +18,11 @@ import type $RefParser from "./index.js";
18
18
  * The promise resolves once all JSON references in the schema have been resolved,
19
19
  * including nested references that are contained in externally-referenced files.
20
20
  */
21
- function resolveExternal<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
21
+ function resolveExternal<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
22
22
  parser: $RefParser<S, O>,
23
- options: Options,
23
+ options: O,
24
24
  ) {
25
- if (!options.resolve.external) {
25
+ if (!options.resolve?.external) {
26
26
  // Nothing to resolve, so exit early
27
27
  return Promise.resolve();
28
28
  }
@@ -52,11 +52,11 @@ function resolveExternal<S extends JSONSchema = JSONSchema, O extends ParserOpti
52
52
  * If any of the JSON references point to files that contain additional JSON references,
53
53
  * then the corresponding promise will internally reference an array of promises.
54
54
  */
55
- function crawl<S extends JSONSchema = JSONSchema>(
55
+ function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
56
56
  obj: string | Buffer | S | undefined | null,
57
57
  path: string,
58
- $refs: $Refs<S>,
59
- options: Options,
58
+ $refs: $Refs<S, O>,
59
+ options: O,
60
60
  seen?: Set<any>,
61
61
  external?: boolean,
62
62
  ) {
@@ -66,7 +66,7 @@ function crawl<S extends JSONSchema = JSONSchema>(
66
66
  if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !seen.has(obj)) {
67
67
  seen.add(obj); // Track previously seen objects to avoid infinite recursion
68
68
  if ($Ref.isExternal$Ref(obj)) {
69
- promises.push(resolve$Ref<S>(obj, path, $refs, options));
69
+ promises.push(resolve$Ref<S, O>(obj, path, $refs, options));
70
70
  }
71
71
 
72
72
  const keys = Object.keys(obj) as string[];
@@ -92,13 +92,13 @@ function crawl<S extends JSONSchema = JSONSchema>(
92
92
  * The promise resolves once all JSON references in the object have been resolved,
93
93
  * including nested references that are contained in externally-referenced files.
94
94
  */
95
- async function resolve$Ref<S extends JSONSchema = JSONSchema>(
95
+ async function resolve$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
96
96
  $ref: S,
97
97
  path: string,
98
- $refs: $Refs<S>,
99
- options: Options,
98
+ $refs: $Refs<S, O>,
99
+ options: O,
100
100
  ) {
101
- const shouldResolveOnCwd = options.dereference.externalReferenceResolution === "root";
101
+ const shouldResolveOnCwd = options.dereference?.externalReferenceResolution === "root";
102
102
  const resolvedPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref!);
103
103
  const withoutHash = url.stripHash(resolvedPath);
104
104
 
@@ -7,11 +7,15 @@ import type {
7
7
  JSONSchema7Object,
8
8
  } from "json-schema";
9
9
  import type $Refs from "../refs.js";
10
+ import type { ParserOptions } from "../options";
10
11
 
11
12
  export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
12
13
  export type JSONSchemaObject = JSONSchema4Object | JSONSchema6Object | JSONSchema7Object;
13
14
  export type SchemaCallback<S extends JSONSchema = JSONSchema> = (err: Error | null, schema?: S | object | null) => any;
14
- export type $RefsCallback<S extends JSONSchema = JSONSchema> = (err: Error | null, $refs?: $Refs<S>) => any;
15
+ export type $RefsCallback<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> = (
16
+ err: Error | null,
17
+ $refs?: $Refs<S, O>,
18
+ ) => any;
15
19
 
16
20
  /**
17
21
  * See https://apitools.dev/json-schema-ref-parser/docs/options.html
@@ -39,7 +39,7 @@ export class JSONParserError extends Error {
39
39
 
40
40
  export class JSONParserErrorGroup<
41
41
  S extends JSONSchema = JSONSchema,
42
- O extends ParserOptions = ParserOptions,
42
+ O extends ParserOptions<S> = ParserOptions<S>,
43
43
  > extends Error {
44
44
  files: $RefParser<S, O>;
45
45
 
@@ -55,12 +55,12 @@ export class JSONParserErrorGroup<
55
55
  Ono.extend(this);
56
56
  }
57
57
 
58
- static getParserErrors<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
58
+ static getParserErrors<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
59
59
  parser: $RefParser<S, O>,
60
60
  ) {
61
61
  const errors = [];
62
62
 
63
- for (const $ref of Object.values(parser.$refs._$refs) as $Ref<S>[]) {
63
+ for (const $ref of Object.values(parser.$refs._$refs) as $Ref<S, O>[]) {
64
64
  if ($ref.errors) {
65
65
  errors.push(...$ref.errors);
66
66
  }
@@ -78,7 +78,7 @@ export class JSONParserErrorGroup<
78
78
  | UnmatchedParserError
79
79
  | UnmatchedResolverError
80
80
  > {
81
- return JSONParserErrorGroup.getParserErrors<S>(this.files);
81
+ return JSONParserErrorGroup.getParserErrors<S, O>(this.files);
82
82
  }
83
83
  }
84
84