diff --git a/src/domain/inputIssueProjection.test.ts b/src/domain/inputIssueProjection.test.ts index 10bbbab96d5..69397f4ffe1 100644 --- a/src/domain/inputIssueProjection.test.ts +++ b/src/domain/inputIssueProjection.test.ts @@ -469,6 +469,55 @@ describe("discriminated union input issue projection", () => { ]); }); + it("selects the alternative that admits more omitted arguments", () => { + const schema = z.union([ + z.strictObject({ + format: z.literal("json").default("json"), + detail: z.enum(["summary", "complete"]), + }), + z.strictObject({ + format: z.literal("sdl"), + detail: z.enum(["summary", "complete"]), + operations: z.string().optional(), + }), + ]); + const project = (input: Record) => { + const parsed = schema.safeParse(input); + if (parsed.success) throw new Error("expected invalid input"); + return projectInputIssues(parsed.error.issues, input); + }; + expect(project({ detail: "bogus" })).toEqual([ + { + path: ["detail"], + reason: "invalid_value", + expected: ["summary", "complete"], + }, + ]); + const flag = z.union([ + z.strictObject({ + id: z.string(), + sources: z.literal(false).default(false), + }), + z.strictObject({ id: z.string(), sources: z.literal(true) }), + ]); + const flagInput = { endpoint: "x" }; + const flagParsed = flag.safeParse(flagInput); + if (flagParsed.success) throw new Error("expected invalid input"); + expect(projectInputIssues(flagParsed.error.issues, flagInput)).toEqual([ + { path: ["id"], reason: "missing_argument", expected: "string" }, + { path: ["endpoint"], reason: "unknown_argument" }, + ]); + // A field only the other alternative declares still selects it. + expect(project({ detail: "bogus", operations: "q" })).toEqual([ + { path: ["format"], reason: "invalid_value", expected: ["sdl"] }, + { + path: ["detail"], + reason: "invalid_value", + expected: ["summary", "complete"], + }, + ]); + }); + it("narrows branches when union includes a strict object variant without the discriminator key", () => { const schema = z.union([ z.strictObject({ diff --git a/src/domain/inputIssueProjection.ts b/src/domain/inputIssueProjection.ts index 4acdab5bb6b..5ce0d2b10a7 100644 --- a/src/domain/inputIssueProjection.ts +++ b/src/domain/inputIssueProjection.ts @@ -500,6 +500,45 @@ const narrowUnionBranches = ( return currentErrors; }; +/** + * Drop alternatives that require every omitted argument another alternative + * requires, and more. The input selected the alternative that admits more + * omissions, such as one that defaults an omitted discriminator. + */ +const narrowByOmittedArguments = ( + branches: readonly (readonly z.core.$ZodIssue[])[], + targetValue: object, + budget?: { remaining: number }, +): readonly (readonly z.core.$ZodIssue[])[] => { + const omitted = branches.map((branch) => { + const keys = new Set(); + for (const issue of branch) { + if (budget !== undefined) { + budget.remaining -= 16; + if (budget.remaining < 0) throw projectionBudgetExceeded; + } + const [key] = issue.path; + if ( + issue.path.length === 1 && + typeof key === "string" && + !Object.hasOwn(targetValue, key) + ) + keys.add(key); + } + return keys; + }); + const kept = branches.filter( + (_, index) => + !omitted.some( + (other, otherIndex) => + otherIndex !== index && + other.size < omitted[index]!.size && + [...other].every((key) => omitted[index]!.has(key)), + ), + ); + return kept.length > 0 ? kept : branches; +}; + interface UnionProjectionContext { readonly input: unknown; readonly projectChildIssue: ( @@ -579,6 +618,8 @@ const projectUnionIssue = ( narrowed = payloadNarrowed; } } + if (narrowed.length > 1) + narrowed = narrowByOmittedArguments(narrowed, targetValue, budget); activeErrors = narrowed; }