You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request: a .partial() that leaves absent keys absent (.exactPartial()/.partialNoDefaults()) #6719
Since v4, a field's .default() applies even when its key is optional (changelog). That's the right default for parsing, but it makes .partial() unusable for patch-style input: every absent key gets filled in, so a partial update turns into a full overwrite.
.prefault() doesn't help, since it changes when the default is parsed rather than whether it applies. The workaround is to unwrap every field's ZodOptional/ZodDefault layers before calling .partial(), which needs a mapped type and a cast, and misses anything else that wraps a default.
The name is open. The mask argument rules out an option on .partial() itself, so it would probably have to be a sibling method, like .exactPartial() or .partialNoDefaults().
Hi, we hit the same problem in our project, so +1 for this.
We make our PATCH schemas from the create schemas with .partial(). The create schemas have defaults. So when someone sends only one field, zod fills all the other fields with their default value, and our update saves them too.
Example: we send { "status": "inactive" } to deactivate a coupon. After parse we get:
So the coupon loses its other settings. Same with products: a small PATCH made the product draft again and removed its categories. There was no error, it just saved wrong data. Lucky for us, our e2e tests found it.
Right now we fix it like this. We write every field that has a default again, without the default:
So I think a new name like .partialNoDefaults() is safer. Changing .exactPartial() could break other people's code.
One more thing: the .partial() docs don't say that defaults still apply. That's why we missed it. If it's ok with you, I can open a small PR to add a note there, with the removeDefault() way from #6627.
By the way, here is my helper util I've been using to accomplish this, works great:
import{mapValues}from'lodash'typeStripDefault<S>=Sextendsz.ZodOptional<infer Inner>
? StripDefault<Inner>
: Sextendsz.ZodDefault<infer Inner>
? StripDefault<Inner>
: StypeStrippedShape<Sextendsz.ZodRawShape>={[KinkeyofS]: StripDefault<S[K]>}conststripDefault=(field: z.ZodType): z.ZodType=>fieldinstanceofz.ZodOptional||fieldinstanceofz.ZodDefault
? stripDefault(field.unwrap()asz.ZodType)
: field/** * `.partial()` for PATCH bodies, where an absent key must stay absent -- * unlike a plain `.partial()`, which fills an absent key with the field's * default and so turns a partial update into a full-row overwrite. */exportconstpartialNoDefaults=<Textendsz.ZodObject>(schema: T)=>z.object(mapValues(schema.shape,stripDefault)as// mapValues returns a Record type rather than accurate object, must cast to correctStrippedShape<T['shape']>).partial()
mapValues just being Object.fromEntries(Object.entries(... natively.
Thanks, nice helper! I tested it on zod 4.6.7. It works for plain .default() and .default().optional(), but the default still gets filled when it's inside another wrapper. Parsing { b: 1 } with partialNoDefaults(schema):
Activity
mdsajjadhossain25 commentedon Nov 3, 2026
Hi, we hit the same problem in our project, so +1 for this.
We make our PATCH schemas from the create schemas with
.partial(). The create schemas have defaults. So when someone sends only one field, zod fills all the other fields with their default value, and our update saves them too.Example: we send
{ "status": "inactive" }to deactivate a coupon. After parse we get:So the coupon loses its other settings. Same with products: a small PATCH made the product draft again and removed its categories. There was no error, it just saved wrong data. Lucky for us, our e2e tests found it.
Right now we fix it like this. We write every field that has a default again, without the default:
It works, but we have to do it by hand in every PATCH schema. If someone forgets one field, only a test can catch it.
About the name:
.exactPartial()already exists, and it also fills the defaults (I checked on zod 4.6.7):So I think a new name like
.partialNoDefaults()is safer. Changing.exactPartial()could break other people's code.One more thing: the
.partial()docs don't say that defaults still apply. That's why we missed it. If it's ok with you, I can open a small PR to add a note there, with theremoveDefault()way from #6627.Thanks!
bvandrc commentedon Nov 3, 2026
By the way, here is my helper util I've been using to accomplish this, works great:
mapValuesjust beingObject.fromEntries(Object.entries(...natively.mdsajjadhossain25 commentedon Nov 4, 2026
Thanks, nice helper! I tested it on zod 4.6.7. It works for plain
.default()and.default().optional(), but the default still gets filled when it's inside another wrapper. Parsing{ b: 1 }withpartialNoDefaults(schema):So a helper outside zod has to know every wrapper type, and it's easy to miss one. I think that's a good reason to have this built into zod.