Skip to content

Commit 4db8afe

Browse files
feat: add object and propName to onDereference callback (#324)
* feat: add object and propName to onDereference callback * refactor: review new argument names and relevant descriptions --------- Co-authored-by: JonLuca De Caro <jonluca.decaro@gmail.com>
1 parent add5f77 commit 4db8afe

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

docs/options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ The `dereference` options control how JSON Schema $Ref Parser will dereference `
8080
|:---------------------|:-------------------|:------------
8181
|`circular`|`boolean` or `"ignore"`|Determines whether [circular `$ref` pointers](README.md#circular-refs) are handled.<br><br>If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.<br><br> If set to `"ignore"`, then circular references will simply be ignored. No error will be thrown, but the [`$Refs.circular`](refs.md#circular) property will still be set to `true`.
8282
|`excludedPathMatcher`|`(string) => boolean`|A function, called for each path, which can return true to stop this path and all subpaths from being dereferenced further. This is useful in schemas where some subpaths contain literal `$ref` keys that should not be dereferenced.
83-
|`onDereference`|`(string, JSONSchemaObjectType) => void`|A function, called immediately after dereferencing, with the resolved JSON Schema value and the `$ref` being dereferenced.
83+
|`onDereference`|`(string, JSONSchemaObjectType, JSONSchemaObjectType, string) => void`|A function, called immediately after dereferencing, with: the resolved JSON Schema value, the `$ref` being dereferenced, the object holding the dereferenced prop, the dereferenced prop name.

lib/dereference.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function crawl(
107107
if (obj[key] !== dereferenced.value) {
108108
obj[key] = dereferenced.value;
109109
if (options.dereference.onDereference) {
110-
options.dereference.onDereference(value.$ref, obj[key]);
110+
options.dereference.onDereference(value.$ref, obj[key], obj, key);
111111
}
112112
}
113113
} else {

lib/options.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ interface $RefParserOptions {
7979
/**
8080
* Callback invoked during dereferencing.
8181
*
82-
* @argument {string} path The path being dereferenced (ie. the `$ref` string).
83-
* @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
82+
* @argument {string} path - The path being dereferenced (ie. the `$ref` string)
83+
* @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
84+
* @argument {JSONSchemaObject} parent - The parent of the dereferenced object
85+
* @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced
8486
*/
85-
onDereference?(path: string, value: JSONSchemaObject): void;
87+
onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
8688

8789
/**
8890
* Whether a reference should resolve relative to its directory/path, or from the cwd

test/specs/dereference-callback/dereference-callback.spec.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,40 @@ describe("Schema with a $ref", () => {
1212
const schema = pathUtils.rel("test/specs/dereference-callback/dereference-callback.yaml");
1313
const options = {
1414
dereference: {
15-
onDereference(path: any, object: any) {
16-
calls.push({ path, object });
15+
onDereference(path, value, parent, parentPropName) {
16+
calls.push({ path, value, parent, parentPropName });
1717
},
1818
},
1919
} as Options;
2020
await parser.dereference(schema, options);
21+
2122
expect(calls).to.deep.equal([
22-
{ path: "#/definitions/b", object: { $ref: "#/definitions/a" } },
23-
{ path: "#/definitions/a", object: { $ref: "#/definitions/a" } },
23+
{
24+
path: "#/definitions/b",
25+
value: { $ref: "#/definitions/a" },
26+
parent: {
27+
a: {
28+
$ref: "#/definitions/a",
29+
},
30+
b: {
31+
$ref: "#/definitions/a",
32+
},
33+
},
34+
parentPropName: "a",
35+
},
36+
{
37+
path: "#/definitions/a",
38+
value: { $ref: "#/definitions/a" },
39+
parent: {
40+
c: {
41+
type: "string",
42+
},
43+
d: {
44+
$ref: "#/definitions/a",
45+
},
46+
},
47+
parentPropName: "d",
48+
},
2449
]);
2550
});
2651
});

0 commit comments

Comments
 (0)