import type * as core from "../core/index.js"; import { visit } from "../core/visit.js"; import * as schemas from "./schemas.js"; /** Distributes `.partial()` through every object in a schema type, preserving the wrappers around them. */ // biome-ignore format: preserve vertical layout for readability. export type DeepPartial = T extends schemas.ZodObject ? schemas.ZodObject< { -readonly [K in keyof Shape]: schemas.ZodOptional> }, Config > // Must precede ZodUnion; degrades to it, matching the runtime. : T extends schemas.ZodDiscriminatedUnion ? schemas.ZodUnion< { -readonly [I in keyof Options]: Options[I] extends core.SomeType ? DeepPartial : Options[I] } > : T extends schemas.ZodUnion ? schemas.ZodUnion< { -readonly [I in keyof Options]: Options[I] extends core.SomeType ? DeepPartial : Options[I] } > : T extends schemas.ZodArray ? schemas.ZodArray> : T extends schemas.ZodTuple ? schemas.ZodTuple< { -readonly [K in keyof Items]: Items[K] extends core.SomeType ? DeepPartial : Items[K] }, Rest extends core.SomeType ? DeepPartial : Rest > : T extends schemas.ZodIntersection ? schemas.ZodIntersection, DeepPartial> : T extends schemas.ZodRecord ? schemas.ZodRecord> : T extends schemas.ZodMap ? schemas.ZodMap> : T extends schemas.ZodSet ? schemas.ZodSet> : T extends schemas.ZodOptional ? schemas.ZodOptional> : T extends schemas.ZodNullable ? schemas.ZodNullable> : T extends schemas.ZodDefault ? schemas.ZodDefault> : T extends schemas.ZodPrefault ? schemas.ZodPrefault> : T extends schemas.ZodNonOptional ? schemas.ZodNonOptional> : T extends schemas.ZodCatch ? schemas.ZodCatch> : T extends schemas.ZodReadonly ? schemas.ZodReadonly> : T extends schemas.ZodSuccess ? schemas.ZodSuccess> : T extends schemas.ZodPromise ? schemas.ZodPromise> // Must precede ZodPipe, which both extend structurally, or the subtype is lost. : T extends schemas.ZodCodec ? schemas.ZodCodec, DeepPartial> : T extends schemas.ZodPreprocess ? schemas.ZodPreprocess> : T extends schemas.ZodPipe ? schemas.ZodPipe, DeepPartial> : T extends schemas.ZodLazy ? schemas.ZodLazy> : T; /** Returns a copy of the schema with every nested object's properties made optional. */ export function deepPartial(schema: T): DeepPartial { return visit(schema, { object: (s) => (s as schemas.ZodObject).partial(), // Every partialed option now admits `undefined`, which the constructor rejects as a duplicate. union: (s) => { const def = s._zod.def as core.$ZodDiscriminatedUnionDef; return def.discriminator === undefined ? s : schemas.union(def.options as schemas.ZodType[]); }, }) as unknown as DeepPartial; }