Skip to content

Commit 5929a46

Browse files
committed
feat(cloning): add option to clone input schema, add notice to readme
1 parent 48445fb commit 5929a46

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ Example
7575
import $RefParser from "@apidevtools/json-schema-ref-parser";
7676

7777
try {
78-
let schema = await $RefParser.dereference(mySchema);
79-
console.log(schema.definitions.person.properties.firstName);
78+
await $RefParser.dereference(mySchema);
79+
// note - by default, mySchema is modified in place, and the returned value is a reference to the same object
80+
console.log(mySchema.definitions.person.properties.firstName);
81+
82+
// if you want to avoid modifying the original schema, you can disable the `mutateInputSchema` option
83+
let clonedSchema = await $RefParser.dereference(mySchema, { mutateInputSchema: false });
84+
console.log(clonedSchema.definitions.person.properties.firstName);
8085
} catch (err) {
8186
console.error(err);
8287
}

lib/index.ts

+9-29
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,17 @@ import {
1919
import { ono } from "@jsdevtools/ono";
2020
import maybe from "./util/maybe.js";
2121
import type { ParserOptions } from "./options.js";
22-
import type {
23-
Plugin,
24-
$RefsCallback,
25-
JSONSchema,
26-
SchemaCallback,
27-
HTTPResolverOptions,
28-
FileInfo,
29-
ResolverOptions,
30-
JSONSchemaObject,
31-
} from "./types/index.js";
22+
import type { $RefsCallback, JSONSchema, SchemaCallback } from "./types/index.js";
3223

33-
export {
34-
JSONSchemaObject,
35-
ResolverOptions,
36-
ParserError,
37-
UnmatchedResolverError,
38-
ResolverError,
39-
HTTPResolverOptions,
40-
FileInfo,
41-
UnmatchedParserError,
42-
ParserOptions,
43-
MissingPointerError,
44-
InvalidPointerError,
45-
JSONParserError,
46-
Plugin,
47-
JSONSchema,
48-
$RefsCallback,
49-
SchemaCallback,
50-
};
24+
export { JSONParserError };
25+
export { InvalidPointerError };
26+
export { MissingPointerError };
27+
export { ResolverError };
28+
export { ParserError };
29+
export { UnmatchedParserError };
30+
export { UnmatchedResolverError };
5131

52-
export type RefParserSchema = string | JSONSchema;
32+
type RefParserSchema = string | JSONSchema;
5333

5434
/**
5535
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,

lib/normalize-args.ts

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ function normalizeArgs(_args: Partial<IArguments>) {
3939
console.log(e);
4040
}
4141

42+
if (!options.mutateInputSchema) {
43+
// Make a deep clone of the schema, so that we don't alter the original object
44+
schema = JSON.parse(JSON.stringify(schema));
45+
}
46+
4247
return {
4348
path,
4449
schema,

lib/options.ts

+9
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ interface $RefParserOptions {
9191
* Default: `relative`
9292
*/
9393
externalReferenceResolution?: "relative" | "root";
94+
95+
/**
96+
* Whether to clone the schema before dereferencing it.
97+
* This is useful when you want to dereference the same schema multiple times, but you don't want to modify the original schema.
98+
* Default: `true` due to mutating the input being the default behavior historically
99+
*/
100+
mutateInputSchema?: boolean;
94101
};
95102
}
96103

@@ -159,6 +166,8 @@ const getDefaults = () => {
159166
excludedPathMatcher: () => false,
160167
referenceResolution: "relative",
161168
},
169+
170+
mutateInputSchema: true,
162171
} as $RefParserOptions;
163172
return defaults;
164173
};

0 commit comments

Comments
 (0)