A TypeScript utility that recursively makes all properties in a Zod schema optional, providing type-safe deep partial schemas.
npm install zod-deep-partial
# or
yarn add zod-deep-partial
# or
pnpm add zod-deep-partial
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
});
zodDeepPartial<T extends z.ZodTypeAny>(schema: T): DeepPartial<T>
Recursively makes all properties in a Zod schema optional.
schema
: A Zod schema to make deeply partialA 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