export function deepPartialify(schema: z.ZodTypeAny): z.ZodTypeAny {
if (schema instanceof ZodObject) {
const newShape: z.ZodRawShape = {};
for (const key in schema.shape) {
const fieldSchema = schema.shape[key];
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema)) as typeof newShape[keyof typeof newShape];
}
return new ZodObject({ ...schema._def, shape: () => newShape });
} else if (schema instanceof ZodArray) {
return new ZodArray({ ...schema._def, type: deepPartialify(schema.element) });
} else if (schema instanceof ZodOptional) {
return ZodOptional.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodNullable) {
return ZodNullable.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodTuple) {
return ZodTuple.create(schema.items.map((item: z.ZodTypeAny) => deepPartialify(item)));
} else if (schema instanceof z.ZodUnion) {
return z.union(schema.options.map((option: z.ZodTypeAny) => deepPartialify(option)));
} else if (schema instanceof ZodLazy) {
return ZodLazy.create(() => deepPartialify(schema._def.getter()));
} else {
return schema;
}
}
export function deepPartialify(schema: z.ZodTypeAny): z.ZodTypeAny {
if (schema instanceof ZodObject) {
const newShape: z.ZodRawShape = {};
for (const key in schema.shape) {
const fieldSchema = schema.shape[key];
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema)) as typeof newShape[keyof typeof newShape];
}
return new ZodObject({ ...schema._def, shape: () => newShape });
} else if (schema instanceof ZodArray) {
return new ZodArray({ ...schema._def, type: deepPartialify(schema.element) });
} else if (schema instanceof ZodOptional) {
return ZodOptional.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodNullable) {
return ZodNullable.create(deepPartialify(schema.unwrap()));
} else if (schema instanceof ZodTuple) {
return ZodTuple.create(schema.items.map((item: z.ZodTypeAny) => deepPartialify(item)));
} else if (schema instanceof z.ZodUnion) {
return z.union(schema.options.map((option: z.ZodTypeAny) => deepPartialify(option)));
} else if (schema instanceof ZodLazy) {
return ZodLazy.create(() => deepPartialify(schema._def.getter()));
} else {
return schema;
}
}