Release: zod@3.8.0
What's new:
z.preprocess
This lets you transform input data before it is parsed by your schema. This is useful for several use cases, notably type coercion. Usage:
const coercedString = z.preprocess(
val => String(val),
z.string()
)
mySchema.parse(12); // => "12"
mySchema.parse(true); // => "true"
CUID validation
Courtesy of @alii
const cuid = z.string().cuid()
cuid.parse("ckopqwooh000001la8mbi2im9");
Improved .deepPartial()
The .deepPartial()
method on object schemas now recursively traverses through ZodArray, ZodTuple, ZodOptional, and ZodNullable elements. Previously, this method only worked in hierarchies of simple object schemas.
const mySchema = z.object({
name: z.string(),
array: z.array(z.object({ asdf: z.string() })),
tuple: z.tuple([
z.object({ value: z.string() })
]),
})
const partialed = mySchema.deepPartial();
type partialed = z.infer<typeof partialed>;
/*
{
name?: string | undefined;
array?: {
asdf?: string | undefined;
}[] | undefined;
tuple?: [{value?: string}] | undefined;
}
*/