Skip to content

Commit 7a8806d

Browse files
committed
fix(types): make default type extend base object instead of JSONSchema to support non-spec compliant type overloads
1 parent 482dff1 commit 7a8806d

16 files changed

+72
-76
lines changed

lib/bundle.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { JSONSchema } from "./index";
1414
* @param parser
1515
* @param options
1616
*/
17-
function bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
17+
function bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
1818
parser: $RefParser<S, O>,
1919
options: O,
2020
) {
@@ -40,7 +40,7 @@ function bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> =
4040
* @param $refs
4141
* @param options
4242
*/
43-
function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
43+
function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
4444
parent: any,
4545
key: string | null,
4646
path: string,
@@ -102,7 +102,7 @@ function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = P
102102
* @param $refs
103103
* @param options
104104
*/
105-
function inventory$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
105+
function inventory$Ref<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
106106
$refParent: any,
107107
$refKey: any,
108108
path: string,

lib/dereference.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default dereference;
1616
* @param parser
1717
* @param options
1818
*/
19-
function dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
19+
function dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
2020
parser: $RefParser<S, O>,
2121
options: O,
2222
) {
@@ -48,7 +48,7 @@ function dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<
4848
* @param options
4949
* @returns
5050
*/
51-
function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
51+
function crawl<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
5252
obj: any,
5353
path: string,
5454
pathFromRoot: string,
@@ -161,7 +161,7 @@ function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = P
161161
* @param options
162162
* @returns
163163
*/
164-
function dereference$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
164+
function dereference$Ref<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
165165
$ref: any,
166166
path: string,
167167
pathFromRoot: string,

lib/index.ts

+32-32
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type RefParserSchema = string | JSONSchema;
3737
*
3838
* @class
3939
*/
40-
export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
40+
export class $RefParser<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
4141
/**
4242
* The parsed (and possibly dereferenced) JSON schema object
4343
*
@@ -94,10 +94,10 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
9494
if (url.isFileSystemPath(args.path)) {
9595
args.path = url.fromFileSystemPath(args.path);
9696
pathType = "file";
97-
} else if (!args.path && args.schema && args.schema.$id) {
97+
} else if (!args.path && args.schema && "$id" in args.schema && args.schema.$id) {
9898
// when schema id has defined an URL should use that hostname to request the references,
9999
// instead of using the current page URL
100-
const params = url.parse(args.schema.$id);
100+
const params = url.parse(args.schema.$id as string);
101101
const port = params.protocol === "https:" ? 443 : 80;
102102

103103
args.path = `${params.protocol}//${params.hostname}:${port}`;
@@ -143,34 +143,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
143143
}
144144
}
145145

146-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
146+
public static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
147147
schema: S | string,
148148
): Promise<S>;
149-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
149+
public static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
150150
schema: S | string,
151151
callback: SchemaCallback<S>,
152152
): Promise<void>;
153-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
153+
public static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
154154
schema: S | string,
155155
options: O,
156156
): Promise<S>;
157-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
157+
public static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
158158
schema: S | string,
159159
options: O,
160160
callback: SchemaCallback<S>,
161161
): Promise<void>;
162-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
162+
public static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
163163
baseUrl: string,
164164
schema: S | string,
165165
options: O,
166166
): Promise<S>;
167-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
167+
public static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
168168
baseUrl: string,
169169
schema: S | string,
170170
options: O,
171171
callback: SchemaCallback<S>,
172172
): Promise<void>;
173-
public static parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
173+
public static parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
174174
| Promise<S>
175175
| Promise<void> {
176176
const parser = new $RefParser<S, O>();
@@ -218,34 +218,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
218218
* @param options (optional)
219219
* @param callback (optional) A callback that will receive a `$Refs` object
220220
*/
221-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
221+
public static resolve<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
222222
schema: S | string,
223223
): Promise<$Refs<S, O>>;
224-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
224+
public static resolve<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
225225
schema: S | string,
226226
callback: $RefsCallback<S, O>,
227227
): Promise<void>;
228-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
228+
public static resolve<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
229229
schema: S | string,
230230
options: O,
231231
): Promise<$Refs<S, O>>;
232-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
232+
public static resolve<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
233233
schema: S | string,
234234
options: O,
235235
callback: $RefsCallback<S, O>,
236236
): Promise<void>;
237-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
237+
public static resolve<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
238238
baseUrl: string,
239239
schema: S | string,
240240
options: O,
241241
): Promise<$Refs<S, O>>;
242-
public static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
242+
public static resolve<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
243243
baseUrl: string,
244244
schema: S | string,
245245
options: O,
246246
callback: $RefsCallback<S, O>,
247247
): Promise<void>;
248-
static resolve<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
248+
static resolve<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
249249
| Promise<S>
250250
| Promise<void> {
251251
const instance = new $RefParser<S, O>();
@@ -263,34 +263,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
263263
* @param options (optional)
264264
* @param callback (optional) A callback that will receive the bundled schema object
265265
*/
266-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
266+
public static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
267267
schema: S | string,
268268
): Promise<S>;
269-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
269+
public static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
270270
schema: S | string,
271271
callback: SchemaCallback<S>,
272272
): Promise<void>;
273-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
273+
public static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
274274
schema: S | string,
275275
options: O,
276276
): Promise<S>;
277-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
277+
public static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
278278
schema: S | string,
279279
options: O,
280280
callback: SchemaCallback<S>,
281281
): Promise<void>;
282-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
282+
public static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
283283
baseUrl: string,
284284
schema: S | string,
285285
options: O,
286286
): Promise<S>;
287-
public static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
287+
public static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
288288
baseUrl: string,
289289
schema: S | string,
290290
options: O,
291291
callback: SchemaCallback<S>,
292292
): Promise<S>;
293-
static bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
293+
static bundle<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
294294
| Promise<S>
295295
| Promise<void> {
296296
const instance = new $RefParser<S, O>();
@@ -337,34 +337,34 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
337337
* @param options (optional)
338338
* @param callback (optional) A callback that will receive the dereferenced schema object
339339
*/
340-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
340+
public static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
341341
schema: S | string,
342342
): Promise<S>;
343-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
343+
public static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
344344
schema: S | string,
345345
callback: SchemaCallback<S>,
346346
): Promise<void>;
347-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
347+
public static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
348348
schema: S | string,
349349
options: O,
350350
): Promise<S>;
351-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
351+
public static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
352352
schema: S | string,
353353
options: O,
354354
callback: SchemaCallback<S>,
355355
): Promise<void>;
356-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
356+
public static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
357357
baseUrl: string,
358358
schema: S | string,
359359
options: O,
360360
): Promise<S>;
361-
public static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
361+
public static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
362362
baseUrl: string,
363363
schema: S | string,
364364
options: O,
365365
callback: SchemaCallback<S>,
366366
): Promise<void>;
367-
static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
367+
static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>():
368368
| Promise<S>
369369
| Promise<void> {
370370
const instance = new $RefParser<S, O>();
@@ -404,7 +404,7 @@ export class $RefParser<S extends JSONSchema = JSONSchema, O extends ParserOptio
404404
}
405405
export default $RefParser;
406406

407-
function finalize<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
407+
function finalize<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
408408
parser: $RefParser<S, O>,
409409
) {
410410
const errors = JSONParserErrorGroup.getParserErrors(parser);

lib/normalize-args.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { JSONSchema, SchemaCallback } from "./types";
44

55
// 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
66
// 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<S> = ParserOptions<S>> {
7+
export interface NormalizedArguments<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
88
path: string;
99
schema: S;
1010
options: O & Options<S>;
@@ -13,7 +13,7 @@ export interface NormalizedArguments<S extends JSONSchema = JSONSchema, O extend
1313
/**
1414
* Normalizes the given arguments, accounting for optional args.
1515
*/
16-
export function normalizeArgs<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
16+
export function normalizeArgs<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
1717
_args: Partial<IArguments>,
1818
): NormalizedArguments<S, O> {
1919
let path;

lib/options.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface DereferenceOptions {
5353
* @param [options] - Overridden options
5454
* @class
5555
*/
56-
export interface $RefParserOptions<S extends JSONSchema = JSONSchema> {
56+
export interface $RefParserOptions<S extends object = JSONSchema> {
5757
/**
5858
* The `parse` options determine how different types of files will be parsed.
5959
*
@@ -174,7 +174,7 @@ export const getJsonSchemaRefParserDefaultOptions = () => {
174174
return defaults;
175175
};
176176

177-
export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
177+
export const getNewOptions = <S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
178178
options: O | undefined,
179179
): O & $RefParserOptions<S> => {
180180
const newOptions = getJsonSchemaRefParserDefaultOptions();
@@ -184,8 +184,8 @@ export const getNewOptions = <S extends JSONSchema = JSONSchema, O extends Parse
184184
return newOptions as O & $RefParserOptions<S>;
185185
};
186186

187-
export type Options<S extends JSONSchema = JSONSchema> = $RefParserOptions<S>;
188-
export type ParserOptions<S extends JSONSchema = JSONSchema> = DeepPartial<$RefParserOptions<S>>;
187+
export type Options<S extends object = JSONSchema> = $RefParserOptions<S>;
188+
export type ParserOptions<S extends object = JSONSchema> = DeepPartial<$RefParserOptions<S>>;
189189
/**
190190
* Merges the properties of the source object into the target object.
191191
*

lib/parse.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type { FileInfo, JSONSchema } from "./types/index.js";
1515
/**
1616
* Reads and parses the specified file path or URL.
1717
*/
18-
async function parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
18+
async function parse<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
1919
path: string,
2020
$refs: $Refs<S, O>,
2121
options: O,
@@ -70,7 +70,7 @@ async function parse<S extends JSONSchema = JSONSchema, O extends ParserOptions<
7070
* @returns
7171
* The promise resolves with the raw file contents and the resolver that was used.
7272
*/
73-
async function readFile<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
73+
async function readFile<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
7474
file: FileInfo,
7575
options: O,
7676
$refs: $Refs<S, O>,
@@ -116,7 +116,7 @@ async function readFile<S extends JSONSchema = JSONSchema, O extends ParserOptio
116116
* @returns
117117
* The promise resolves with the parsed file contents and the parser that was used.
118118
*/
119-
async function parseFile<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
119+
async function parseFile<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
120120
file: FileInfo,
121121
options: O,
122122
$refs: $Refs<S, O>,

lib/pointer.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const safeDecodeURIComponent = (encodedURIComponent: string): string => {
2626
* @param [friendlyPath] - The original user-specified path (used for error messages)
2727
* @class
2828
*/
29-
class Pointer<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
29+
class Pointer<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
3030
/**
3131
* The {@link $Ref} object that contains this {@link Pointer} object.
3232
*/
@@ -86,7 +86,7 @@ class Pointer<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = Pa
8686
* the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
8787
* of the resolved value.
8888
*/
89-
resolve(obj: any, options?: O, pathFromRoot?: string) {
89+
resolve(obj: S, options?: O, pathFromRoot?: string) {
9090
const tokens = Pointer.parse(this.path, this.originalPath);
9191

9292
// Crawl the object, one token at a time
@@ -144,7 +144,7 @@ class Pointer<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = Pa
144144
* @returns
145145
* Returns the modified object, or an entirely new object if the entire object is overwritten.
146146
*/
147-
set(obj: any, value: any, options?: O) {
147+
set(obj: S, value: any, options?: O) {
148148
const tokens = Pointer.parse(this.path);
149149
let token;
150150

lib/ref.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type $RefError = JSONParserError | ResolverError | ParserError | MissingP
1313
*
1414
* @class
1515
*/
16-
class $Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
16+
class $Ref<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>> {
1717
/**
1818
* The file path or URL of the referenced file.
1919
* This path is relative to the path of the main JSON schema file.
@@ -185,7 +185,7 @@ class $Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = Parse
185185
* @param options
186186
* @returns
187187
*/
188-
static isAllowed$Ref<S extends JSONSchema = JSONSchema>(value: unknown, options?: ParserOptions<S>) {
188+
static isAllowed$Ref<S extends object = JSONSchema>(value: unknown, options?: ParserOptions<S>) {
189189
if (this.is$Ref(value)) {
190190
if (value.$ref.substring(0, 2) === "#/" || value.$ref === "#") {
191191
// It's a JSON Pointer reference, which is always allowed
@@ -266,7 +266,7 @@ class $Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = Parse
266266
* @param resolvedValue - The resolved value, which can be any type
267267
* @returns - Returns the dereferenced value
268268
*/
269-
static dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
269+
static dereference<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
270270
$ref: $Ref<S, O>,
271271
resolvedValue: S,
272272
): S {

0 commit comments

Comments
 (0)