Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid union-of-partials data does not parse even though it conforms to one (or more) of the union members #3584

Open
brenfwd opened this issue Jun 18, 2024 · 1 comment

Comments

@brenfwd
Copy link

brenfwd commented Jun 18, 2024

Consider this example:

import { z } from "zod";

const FooASchema = z.object({
  type: z.literal("A"),
  a: z.number(),
});

const FooBSchema = z.object({
  type: z.literal("B"),
  b: z.number(),
});

// const FooSchema = z.union([FooASchema, FooBSchema]);
const PartialFooSchema = z.union([FooASchema.partial(), FooBSchema.partial()]);

const input: unknown = { b: 12 };

const typed = PartialFooSchema.safeParse(input);

console.log("Input:", input);
console.log("Error:", typed.error);
console.log("Data:", typed.data);

Here I would expect that typed.data would be { b: 12 } since input should validate against FooBSchema.partial(). However, the actual behavior is (1) no error; and (2) typed.data is {}.

@brenfwd
Copy link
Author

brenfwd commented Jun 18, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant