mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
fix(errors): select the alternative that admits the omitted arguments
When every alternative of a plain union failed and the present keys did
not separate them, issue projection merged the issues of all
alternatives, including another alternative's requirement for an
argument the caller omitted. inspect_api_contract reported format for
{path, detail: "bogus"}, and inspect_electron_page reported
include_script_sources for a bad cdp_endpoint, although the selected
alternative defaults each.
After the existing narrowing, drop an alternative when the omitted
arguments it reports are a strict superset of another alternative's.
Alternatives that each miss different arguments are kept, and a field
only another alternative declares still selects it first.
Refs #15319
Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
1 parent
866dac2e9b
commit
687e90ef37
2 files changed
+90
No files matched your search
@@ -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<string, unknown>) => {
|
||||
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({
|
||||
|
||||
@@ -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<string>();
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user