51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { z } from "zod";
|
|
import type { DeepPartial } from "./types";
|
|
/**
|
|
* 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)
|
|
*
|
|
* @template T - The Zod schema type
|
|
* @param schema - The Zod schema to make deeply partial
|
|
* @returns A new Zod schema where all properties are optional at all levels
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* import { z } from "zod";
|
|
* import { zodDeepPartial } from "zod-deep-partial";
|
|
*
|
|
* const userSchema = z.object({
|
|
* name: z.string(),
|
|
* email: z.email(),
|
|
* profile: z.object({
|
|
* bio: z.string(),
|
|
* avatar: z.url(),
|
|
* }),
|
|
* });
|
|
*
|
|
* const partialUserSchema = zodDeepPartial(userSchema);
|
|
*
|
|
* // All of these are now valid:
|
|
* partialUserSchema.parse({});
|
|
* partialUserSchema.parse({ name: "John" });
|
|
* partialUserSchema.parse({ profile: { bio: "Developer" } });
|
|
* ```
|
|
*/
|
|
export declare function zodDeepPartial<T extends z.core.SomeType>(schema: T): DeepPartial<T>;
|