Files
rea/src/domain/inputIssueProjection.ts
T
Iwakurakaoru0822andClaude Opus 5.5 bddeb634b5 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>
2026-10-06 22:02:25 -07:00

108 lines
3.1 KiB
TypeScript

import type { z } from "zod";
import type { AnalysisInputIssue } from "./analysisErrorCore.js";
/** Project Zod failures to secret-safe caller correction metadata. */
export const projectInputIssues = (
issues: readonly z.core.$ZodIssue[],
input: unknown,
): readonly AnalysisInputIssue[] =>
issues.flatMap((issue) => projectIssue(issue, input));
const projectIssue = (
issue: z.core.$ZodIssue,
input: unknown,
): readonly AnalysisInputIssue[] => {
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 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) => ({
path: [...path, key],
reason: "unknown_argument" as const,
}));
if (issue.code === "invalid_type")
return [
{
path,
reason:
valueAtPath(input, path) === undefined
? "missing_argument"
: "invalid_type",
expected: safeExpected(issue.expected),
},
];
if (issue.code === "too_small")
return [boundedIssue(path, "minimum", numericBound(issue.minimum))];
if (issue.code === "too_big")
return [boundedIssue(path, "maximum", numericBound(issue.maximum))];
if (issue.code === "invalid_format")
return [{ path, reason: "invalid_format", expected: issue.format }];
if (issue.code === "invalid_value")
return [
{
path,
reason: "invalid_value",
expected: issue.values.filter(isSafeExpected),
},
];
return [
{
path,
reason: "invalid_value",
...(issue.message.length > 0 ? { message: issue.message } : {}),
},
];
};
const uniqueIssues = (
issues: readonly AnalysisInputIssue[],
): AnalysisInputIssue[] => [
...new Map(issues.map((issue) => [JSON.stringify(issue), issue])).values(),
];
const valueAtPath = (
input: unknown,
path: readonly (string | number)[],
): unknown => {
let current = input;
for (const part of path) {
if (typeof current !== "object" || current === null) return undefined;
const next: unknown = Reflect.get(current, part);
current = next;
}
return current;
};
const numericBound = (value: number | bigint): number | undefined =>
typeof value === "number" && Number.isFinite(value) ? value : undefined;
const isSafeExpected = (
value: unknown,
): value is string | number | boolean | null =>
value === null || ["string", "number", "boolean"].includes(typeof value);
const safeExpected = (value: unknown) =>
isSafeExpected(value) ? value : "schema-defined value";
const boundedIssue = (
path: readonly (string | number)[],
key: "minimum" | "maximum",
value: number | undefined,
): AnalysisInputIssue => ({
path,
reason: "out_of_range",
...(value === undefined ? {} : { [key]: value }),
});