Skip to content

Feature request: a .partial() that leaves absent keys absent (.exactPartial()/.partialNoDefaults()) #6719

Open

Description

@bvandrc

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.

const Settings = z.object({ theme: z.string().default("light"), fontSize: z.number().default(14) });

Settings.partial().parse({ fontSize: 16 }); // { theme: "light", fontSize: 16 }, so theme gets reset

Proposed: a variant of .partial() that leaves an absent key absent, taking the same mask:

Settings.partialNoDefaults().parse({ fontSize: 16 }); // { fontSize: 16 }

.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().

Activity

  1. mdsajjadhossain25 commented on Nov 3, 2026

    @mdsajjadhossain25

    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:

    { status: "inactive", individualUse: false, excludeSaleItems: false, freeShipping: false }

    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:

    const UpdateProduct = ProductObject.partial().extend({
      status: z.enum(["draft", "publish"]).optional(),
      isVisible: z.boolean().optional(),
      categoryIds: z.array(z.string()).optional(),
    });

    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):

    const S = z.object({ theme: z.string().default("light"), fontSize: z.number().default(14) });
    S.exactPartial().parse({ fontSize: 16 }); // { theme: "light", fontSize: 16 }

    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.

    Thanks!

  2. bvandrc commented on Nov 3, 2026

    @bvandrc
    Author

    By the way, here is my helper util I've been using to accomplish this, works great:

    import { mapValues } from 'lodash'
    
    type StripDefault<S> =
      S extends z.ZodOptional<infer Inner>
        ? StripDefault<Inner>
        : S extends z.ZodDefault<infer Inner>
          ? StripDefault<Inner>
          : S
    
    type StrippedShape<S extends z.ZodRawShape> = {
      [K in keyof S]: StripDefault<S[K]>
    }
    
    const stripDefault = (field: z.ZodType): z.ZodType =>
      field instanceof z.ZodOptional || field instanceof z.ZodDefault
        ? stripDefault(field.unwrap() as z.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.
     */
    export const partialNoDefaults = <T extends z.ZodObject>(schema: T) =>
      z.object(
          mapValues(schema.shape, stripDefault) as
            // mapValues returns a Record type rather than accurate object, must cast to correct
            StrippedShape<T['shape']>
        ).partial()

    mapValues just being Object.fromEntries(Object.entries(... natively.

  3. mdsajjadhossain25 commented on Nov 4, 2026

    @mdsajjadhossain25

    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):

    z.object({ a: z.string().default("x").nullable(), b: z.number() })               // { a: "x", b: 1 }
    z.object({ a: z.string().default("x").transform((s) => s.toUpperCase()), b: z.number() }) // { a: "X", b: 1 }
    z.object({ a: z.array(z.string()).default([]).readonly(), b: z.number() })        // { a: [], b: 1 }
    z.object({ a: z.string().prefault("x"), b: z.number() })                          // { a: "x", b: 1 }

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions