Skip to content

Commit ead6d84

Browse files
committed
fix: reduce dependency missing keyword required
1 parent 8d9afb9 commit ead6d84

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/compileSchema.reduceSchema.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ describe("compileSchema : reduceNode", () => {
9292
assert.deepEqual(reduced.dynamicId, "#/$defs/schema(dependencies/one,dependencies/two)");
9393
});
9494

95+
it("should add required-property from dependency", () => {
96+
const node: any = compileSchema({
97+
type: "object",
98+
properties: { one: { title: "Property One", type: "string" } },
99+
dependencies: { one: { required: ["two"], properties: { two: { type: "string" } } } }
100+
});
101+
const { node: reduced } = node.reduceNode({ one: "" });
102+
assert.deepEqual(reduced.schema.required, ["two"]);
103+
});
104+
95105
it("should NOT merge dependency when it is not defined", () => {
96106
const node: any = compileSchema({
97107
type: "object",

src/keywords/dependencies.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ export function reduceDependencies({ node, data, key, pointer, path }: JsonSchem
7979
return true;
8080
}
8181

82+
if (Array.isArray(dependency.schema.required)) {
83+
required.push(...dependency.schema.required);
84+
}
85+
8286
// @note pass on updated required-list to resolve nested dependencies. This is currently supported,
8387
// but probably not how json-schema spec defines this behaviour (resolve only within sub-schema)
8488
const reducedDependency = { ...dependency, schema: { ...dependency.schema, required } }.reduceNode(data, {
@@ -88,9 +92,6 @@ export function reduceDependencies({ node, data, key, pointer, path }: JsonSchem
8892
}).node;
8993

9094
workingNode = mergeNode(workingNode, reducedDependency);
91-
if (workingNode.schema.required) {
92-
required.push(...workingNode.schema.required);
93-
}
9495

9596
// @dynamicId
9697
const nestedDynamicId = reducedDependency.dynamicId?.replace(node.dynamicId, "") ?? "";
@@ -103,10 +104,6 @@ export function reduceDependencies({ node, data, key, pointer, path }: JsonSchem
103104
return node;
104105
}
105106

106-
// mergedSchema = mergeSchema(node.schema, mergedSchema, "dependencies");
107-
// const { node: childNode, error } = node.compileSchema(mergedSchema, node.evaluationPath).reduceNode(data, { path });
108-
// return childNode ?? error;
109-
110107
if (required.length === 0) {
111108
return workingNode;
112109
}

0 commit comments

Comments
 (0)