github colinhacks/zod v3.8.0

latest releases: v3.23.8, v3.23.7, v3.23.6...
3 years ago

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;
  }
*/

Don't miss a new zod release

NewReleases is sending notifications on new releases.