zod-deep-partial - v1.2.0
    Preparing search index...

    Function zodDeepPartial

    • Recursively makes all properties in a Zod schema optional at all levels.

      This is the main entry point for creating deeply partial Zod schemas. It transforms a Zod schema so that every property, at every level of nesting, becomes optional. This is useful for:

      • Creating partial update schemas
      • Handling incomplete data structures
      • Building flexible API request/response validators
      • Implementing patch operations

      The function preserves type safety and works with all Zod schema types including:

      • Objects (with nested objects)
      • Arrays
      • Unions
      • Intersections
      • Records
      • Tuples
      • Lazy/recursive schemas
      • Discriminated unions (preserves discriminator as required)

      Type Parameters

      • T extends SomeType

        The Zod schema type

      Parameters

      • schema: T

        The Zod schema to make deeply partial

      Returns DeepPartial<T>

      A new Zod schema where all properties are optional at all levels

      import { z } from "zod";
      import { zodDeepPartial } from "zod-deep-partial";

      const userSchema = z.object({
      name: z.string(),
      email: z.string().email(),
      profile: z.object({
      bio: z.string(),
      avatar: z.string().url(),
      }),
      });

      const partialUserSchema = zodDeepPartial(userSchema);

      // All of these are now valid:
      partialUserSchema.parse({});
      partialUserSchema.parse({ name: "John" });
      partialUserSchema.parse({ profile: { bio: "Developer" } });