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

    zod-deep-partial - v1.0.0

    zod-deep-partial

    A TypeScript utility that recursively makes all properties in a Zod schema optional, providing type-safe deep partial schemas.

    npm version License: MIT

    npm install zod-deep-partial
    # or
    yarn add zod-deep-partial
    # or
    pnpm add zod-deep-partial
    • Recursively makes all properties in a Zod schema optional
    • Preserves type safety with TypeScript
    • Handles all Zod schema types:
      • Objects
      • Arrays
      • Unions
      • Intersections
      • Records
      • Tuples
      • Lazy schemas
      • Discriminated unions (preserves discriminator field as required)
    import { z } from "zod";
    import { zodDeepPartial } from "zod-deep-partial";

    // Define your schema
    const userSchema = z.object({
    name: z.string(),
    age: z.number(),
    address: z.object({
    street: z.string(),
    city: z.string(),
    country: z.string(),
    }),
    hobbies: z.array(z.string()),
    });

    // Create a deep partial version
    const partialUserSchema = zodDeepPartial(userSchema);

    // Now all fields are optional
    const partialUser = partialUserSchema.parse({
    name: "John",
    // age is optional
    address: {
    street: "123 Main St",
    // city and country are optional
    },
    // hobbies is optional
    });

    Recursively makes all properties in a Zod schema optional.

    • schema: A Zod schema to make deeply partial

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

    The package includes full TypeScript support with proper type inference. The DeepPartial type utility handles all Zod schema types:

    • ZodObject
    • ZodArray
    • ZodUnion
    • ZodIntersection
    • ZodRecord
    • ZodTuple
    • ZodLazy
    • ZodDiscriminatedUnion

    Contributions are welcome! Please feel free to submit a Pull Request.

    MIT © Amir Farzamnia