zod icon indicating copy to clipboard operation
zod copied to clipboard

Unexpected parse failure

Open epatey opened this issue 2 years ago • 0 comments

First off, thank you for Zod. It's indispensable.

I've created the below schema that should yield type NameAndVitals = { name: string } & ({ age: number; } | {})

const vitalsSchema = z.object({ age: z.number() }).or(z.object({}).strict());
const nameAndVitalsSchema = z.object({ name: z.string() }).and(vitalsSchema);

Unfortunately, { name: "trey" } fails to parse with {"_errors":["Unrecognized key(s) in object: 'name'"]}.

I debugged it and determined that it works as expected if I omit the .strict() from vitalsSchema. I don't understand why that would fix the issue. Is this a bug, or is this expected behavior?

Here's a CodeSandbox that exhibits the issue.

Also, my real-world scenario is much more complex. This is the minimal/contrived repro schema.

epatey avatar Sep 20 '22 23:09 epatey