Skip to content

Commit

Permalink
feat: add object and propName to onDereference callback
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed Sep 25, 2023
1 parent 26d824b commit 9f477b9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ The `dereference` options control how JSON Schema $Ref Parser will dereference `
|:---------------------|:-------------------|:------------
|`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`.
|`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.
|`onDereference`|`(string, JSONSchemaObjectType) => void`|A function, called immediately after dereferencing, with the resolved JSON Schema value and the `$ref` being dereferenced.
|`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.
2 changes: 1 addition & 1 deletion lib/dereference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function crawl(
if (obj[key] !== dereferenced.value) {
obj[key] = dereferenced.value;
if (options.dereference.onDereference) {
options.dereference.onDereference(value.$ref, obj[key]);
options.dereference.onDereference(value.$ref, obj[key], obj, key);
}
}
} else {
Expand Down
4 changes: 3 additions & 1 deletion lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ interface $RefParserOptions {
*
* @argument {string} path The path being dereferenced (ie. the `$ref` string).
* @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
* @argument {JSONSchemaObject} object The JSON-Schema object that holds the dereferenced prop.
* @argument {string} object The dereferenced prop name.
*/
onDereference?(path: string, value: JSONSchemaObject): void;
onDereference?(path: string, value: JSONSchemaObject, object?: JSONSchemaObject, propName?: string): void;

/**
* Whether a reference should resolve relative to its directory/path, or from the cwd
Expand Down
33 changes: 29 additions & 4 deletions test/specs/dereference-callback/dereference-callback.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,40 @@ describe("Schema with a $ref", () => {
const schema = pathUtils.rel("test/specs/dereference-callback/dereference-callback.yaml");
const options = {
dereference: {
onDereference(path: any, object: any) {
calls.push({ path, object });
onDereference(path, value, object, propName) {
calls.push({ path, value, object, propName });
},
},
} as Options;
await parser.dereference(schema, options);

expect(calls).to.deep.equal([
{ path: "#/definitions/b", object: { $ref: "#/definitions/a" } },
{ path: "#/definitions/a", object: { $ref: "#/definitions/a" } },
{
path: "#/definitions/b",
value: { $ref: "#/definitions/a" },
object: {
a: {
$ref: "#/definitions/a",
},
b: {
$ref: "#/definitions/a",
},
},
propName: "a",
},
{
path: "#/definitions/a",
value: { $ref: "#/definitions/a" },
object: {
c: {
type: "string",
},
d: {
$ref: "#/definitions/a",
},
},
propName: "d",
},
]);
});
});

0 comments on commit 9f477b9

Please sign in to comment.