Default with partial in zod 4
Author: jonassoderholmCreated Sep 23, 2026Updated Sep 23, 2026
We have an API where Zod is used for type checking. Here is an example (shorter) of the schema for the POST endpoint:
z.object({
a: z.string().default('a'),
b: z.string().default('b'),
});We also have a PATCH endpoint. We derive the schema from the Post schema:
z.object({
a: z.string().default('a'),
b: z.string().default('b'),
}).partial();Passing in the following to the PATCH endpoint:
{
"b": "ddd",
}In Zod 3, the parsed value would be:
{
"b": "ddd"
}In Zod 4:
{
"a": "a",
"b": "ddd"
}This would mean we overwrite the value in "a" which is not what we want in this casee. Is there a way to get the Zod 3 behaviour in Zod 4 in this case?
Source: colinhacks/zod