fix(errors): report union input issues at the union's path

Zod reports union branch issues relative to the union, so a missing
union-typed argument such as trace_application_feature's seed was
reported at the request root, once per branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Iwakurakaoru0822
2026-10-06 22:02:25 -07:00
co-authored by Claude Opus 5.5
parent b6d9acca63
commit bddeb634b5
2 changed files with 39 additions and 2 deletions
+16 -2
View File
@@ -15,9 +15,17 @@ const projectIssue = (
const path = issue.path.flatMap((part) =>
typeof part === "string" || typeof part === "number" ? [part] : [],
);
// Branch issue paths are relative to the union; branches often agree.
if (issue.code === "invalid_union")
return issue.errors.flatMap((branch) =>
branch.flatMap((branchIssue) => projectIssue(branchIssue, input)),
return uniqueIssues(
issue.errors.flatMap((branch) =>
branch.flatMap((branchIssue) =>
projectIssue(
{ ...branchIssue, path: [...issue.path, ...branchIssue.path] },
input,
),
),
),
);
if (issue.code === "unrecognized_keys")
return issue.keys.map((key) => ({
@@ -58,6 +66,12 @@ const projectIssue = (
];
};
const uniqueIssues = (
issues: readonly AnalysisInputIssue[],
): AnalysisInputIssue[] => [
...new Map(issues.map((issue) => [JSON.stringify(issue), issue])).values(),
];
const valueAtPath = (
input: unknown,
path: readonly (string | number)[],