fix(ai/schema): fail validation when primitive ref recursion exceeds cap

Primitive $ref recursion now emits a validation issue when the depth cap is exceeded instead of returning true. This preserves termination without accepting invalid values at the end of a long ref chain.
This commit is contained in:
can1357
2026-05-15 18:13:15 +02:00
parent 0af6a08190
commit 734091f6f2
2 changed files with 15 additions and 1 deletions
@@ -153,7 +153,10 @@ function validateSchemaNode(
if (ctx.seenPairs.has(pairKey)) return true;
ctx.seenPairs.add(pairKey);
} else {
if (ctx.refDepth >= MAX_REF_DEPTH) return true;
if (ctx.refDepth >= MAX_REF_DEPTH) {
pushIssue(issues, path, "reference depth exceeded", { keyword: "$ref" });
return false;
}
ctx.refDepth += 1;
}
const ok = validateSchemaNode(resolved, value, path, ctx, issues);
@@ -583,6 +583,17 @@ describe("json-schema validator unsupported-keyword regressions", () => {
expect(isJsonSchemaValueValid(schema, { name: "root", child: { name: 123 } })).toBe(false);
expect(isJsonSchemaValueValid(schema, { name: "root", child: { child: { name: "x" } } })).toBe(false);
});
it("fails primitive $ref chains that exceed the recursion cap instead of accepting invalid values", () => {
const definitions: Record<string, unknown> = {};
for (let i = 0; i < 66; i += 1) {
definitions[`A${i}`] = { $ref: `#/definitions/A${i + 1}` };
}
definitions.A66 = { type: "number" };
const schema = { $ref: "#/definitions/A0", definitions };
expect(isJsonSchemaValueValid(schema, "not-a-number")).toBe(false);
});
});
describe("meta-validator conditional keywords", () => {