Skip to content

3.8.0#602

Merged
colinhacks merged 9 commits into
masterfrom
3.8
Aug 23, 2021
Merged

3.8.0#602
colinhacks merged 9 commits into
masterfrom
3.8

Conversation

@colinhacks

@colinhacks colinhacks commented Aug 23, 2021

Copy link
Copy Markdown
Owner

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

@colinhacks colinhacks changed the title 3.8 3.8.0 Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants