Skip to content

Commit

Permalink
Document .pipe()
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Mar 5, 2023
1 parent 20df80e commit ead93d3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
35 changes: 26 additions & 9 deletions README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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<typeof px>
const px = z.custom<`${number}px`>(
(x) =>
z
.string()
.regex(/^\d+px$/)
.safeParse(x).success
);
type Px = z.infer<typeof px>;
// 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!
Expand Down Expand Up @@ -2329,6 +2334,18 @@ type Cat = z.infer<typeof Cat>;

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
Expand Down
35 changes: 26 additions & 9 deletions deno/lib/README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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<typeof px>
const px = z.custom<`${number}px`>(
(x) =>
z
.string()
.regex(/^\d+px$/)
.safeParse(x).success
);
type Px = z.infer<typeof px>;
// 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!
Expand Down Expand Up @@ -2329,6 +2334,18 @@ type Cat = z.infer<typeof Cat>;

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
Expand Down

0 comments on commit ead93d3

Please sign in to comment.