From ead93d3cfde54efe952dc615c21ff179a00f7574 Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Sun, 5 Mar 2023 01:31:34 -0800 Subject: [PATCH] Document .pipe() --- README.md | 35 ++++++++++++++++++++++++++--------- deno/lib/README.md | 35 ++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4befb347e..79c4b71cf 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ - [.or](#or) - [.and](#and) - [.brand](#brand) + - [.pipe](#pipe) - [Guides and concepts](#guides-and-concepts) - [Type inference](#type-inference) - [Writing generic functions](#writing-generic-functions) @@ -1762,17 +1763,21 @@ This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that conta You can create a Zod schema for any TypeScript type by using `z.custom()`. This is useful for creating schemas for types that are not supported by Zod out of the box, such as template string literals. ```ts -const px = z.custom<`${ number }px`>( x => - z.string().regex( /^\d+px$/ ).safeParse( x ).success -) -type Px = z.infer +const px = z.custom<`${number}px`>( + (x) => + z + .string() + .regex(/^\d+px$/) + .safeParse(x).success +); +type Px = z.infer; // type Px = `${number}px` -console.log( px.safeParse( '42px' ).success ) // true -console.log( px.safeParse( '42vw' ).success ) // false -console.log( px.safeParse( 42 ).success ) // false -console.log( px.safeParse( 42n ).success ) // false -console.log( px.safeParse( null ).success ) // false +console.log(px.safeParse("42px").success); // true +console.log(px.safeParse("42vw").success); // false +console.log(px.safeParse(42).success); // false +console.log(px.safeParse(42n).success); // false +console.log(px.safeParse(null).success); // false ``` If you don't provide a validation function, Zod will allow any value. This can be dangerous! @@ -2329,6 +2334,18 @@ type Cat = z.infer; Note that branded types do not affect the runtime result of `.parse`. It is a static-only construct. +### `.pipe()` + +Schemas can be chained into validation "pipelines". It's useful for easily validating the result after a `.transform()`: + +```ts +z.string() + .transform((val) => val.length) + .pipe(z.number().min(5)); +``` + +The `.pipe()` method returns a `ZodPipeline` instance. + ## Guides and concepts ### Type inference diff --git a/deno/lib/README.md b/deno/lib/README.md index 4befb347e..79c4b71cf 100644 --- a/deno/lib/README.md +++ b/deno/lib/README.md @@ -118,6 +118,7 @@ - [.or](#or) - [.and](#and) - [.brand](#brand) + - [.pipe](#pipe) - [Guides and concepts](#guides-and-concepts) - [Type inference](#type-inference) - [Writing generic functions](#writing-generic-functions) @@ -1762,17 +1763,21 @@ This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that conta You can create a Zod schema for any TypeScript type by using `z.custom()`. This is useful for creating schemas for types that are not supported by Zod out of the box, such as template string literals. ```ts -const px = z.custom<`${ number }px`>( x => - z.string().regex( /^\d+px$/ ).safeParse( x ).success -) -type Px = z.infer +const px = z.custom<`${number}px`>( + (x) => + z + .string() + .regex(/^\d+px$/) + .safeParse(x).success +); +type Px = z.infer; // type Px = `${number}px` -console.log( px.safeParse( '42px' ).success ) // true -console.log( px.safeParse( '42vw' ).success ) // false -console.log( px.safeParse( 42 ).success ) // false -console.log( px.safeParse( 42n ).success ) // false -console.log( px.safeParse( null ).success ) // false +console.log(px.safeParse("42px").success); // true +console.log(px.safeParse("42vw").success); // false +console.log(px.safeParse(42).success); // false +console.log(px.safeParse(42n).success); // false +console.log(px.safeParse(null).success); // false ``` If you don't provide a validation function, Zod will allow any value. This can be dangerous! @@ -2329,6 +2334,18 @@ type Cat = z.infer; Note that branded types do not affect the runtime result of `.parse`. It is a static-only construct. +### `.pipe()` + +Schemas can be chained into validation "pipelines". It's useful for easily validating the result after a `.transform()`: + +```ts +z.string() + .transform((val) => val.length) + .pipe(z.number().min(5)); +``` + +The `.pipe()` method returns a `ZodPipeline` instance. + ## Guides and concepts ### Type inference