File tree 4 files changed +30
-31
lines changed
4 files changed +30
-31
lines changed Original file line number Diff line number Diff line change @@ -75,8 +75,13 @@ Example
75
75
import $RefParser from " @apidevtools/json-schema-ref-parser" ;
76
76
77
77
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 );
80
85
} catch (err) {
81
86
console .error (err);
82
87
}
Original file line number Diff line number Diff line change @@ -19,37 +19,17 @@ import {
19
19
import { ono } from "@jsdevtools/ono" ;
20
20
import maybe from "./util/maybe.js" ;
21
21
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" ;
32
23
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 } ;
51
31
52
- export type RefParserSchema = string | JSONSchema ;
32
+ type RefParserSchema = string | JSONSchema ;
53
33
54
34
/**
55
35
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,
Original file line number Diff line number Diff line change @@ -39,6 +39,11 @@ function normalizeArgs(_args: Partial<IArguments>) {
39
39
console . log ( e ) ;
40
40
}
41
41
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
+
42
47
return {
43
48
path,
44
49
schema,
Original file line number Diff line number Diff line change @@ -91,6 +91,13 @@ interface $RefParserOptions {
91
91
* Default: `relative`
92
92
*/
93
93
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 ;
94
101
} ;
95
102
}
96
103
@@ -159,6 +166,8 @@ const getDefaults = () => {
159
166
excludedPathMatcher : ( ) => false ,
160
167
referenceResolution : "relative" ,
161
168
} ,
169
+
170
+ mutateInputSchema : true ,
162
171
} as $RefParserOptions ;
163
172
return defaults ;
164
173
} ;
You can’t perform that action at this time.
0 commit comments