Skip to content

Commit 9f477b9

Browse files
committed
feat: add object and propName to onDereference callback
1 parent 26d824b commit 9f477b9

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

docs/options.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ interface $RefParserOptions {
8181
*
8282
* @argument {string} path The path being dereferenced (ie. the `$ref` string).
8383
* @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
84+
* @argument {JSONSchemaObject} object The JSON-Schema object that holds the dereferenced prop.
85+
* @argument {string} object The dereferenced prop name.
8486
*/
85-
onDereference?(path: string, value: JSONSchemaObject): void;
87+
onDereference?(path: string, value: JSONSchemaObject, object?: JSONSchemaObject, propName?: 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

Lines changed: 29 additions & 4 deletions
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, object, propName) {
16+
calls.push({ path, value, object, propName });
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+
object: {
27+
a: {
28+
$ref: "#/definitions/a",
29+
},
30+
b: {
31+
$ref: "#/definitions/a",
32+
},
33+
},
34+
propName: "a",
35+
},
36+
{
37+
path: "#/definitions/a",
38+
value: { $ref: "#/definitions/a" },
39+
object: {
40+
c: {
41+
type: "string",
42+
},
43+
d: {
44+
$ref: "#/definitions/a",
45+
},
46+
},
47+
propName: "d",
48+
},
2449
]);
2550
});
2651
});

0 commit comments

Comments
 (0)