Skip to content

Commit 7e39a99

Browse files
committed
Improve codec docs
1 parent 648eb43 commit 7e39a99

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

packages/docs/content/codecs.mdx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ import { ThemedImage } from "@/components/themed-image";
77

88
> **New** — Introduced in `[email protected]`
99
10-
> **TLDR** — You can use `z.encode(<schema>, <input>)` to perform an _encode_ operation ("reverse parsing"). This will work with any schema but is primarily intended for use in conjunction with `z.codec()`.
11-
>
12-
> This most important exception is the `.transform()` API, which performs a *unidirectional* transformation. This isn't compatible with `z.encode()` as Zod cannot reverse it in a sound way. If you use `.transform()` anywhere in your schema, attempting a `z.encode()` operation will throw a *runtime error* (not a `ZodError`). To fix this, you'll need to refactor all usage of `.transform()` to use `z.codec()`.
10+
> **TLDR** — You can use `z.encode(<schema>, <input>)` to perform an _encode_ operation ("reverse parsing"). This will work with any schema (except those containing `.transform()`) but is primarily intended for use in conjunction with `z.codec()`.
1311
14-
All Zod schemas (with one major exception described below) are "codecs". That is, they can process inputs in both the forward and backward direction:
12+
Zod schemas are "codecs". That is, they can process inputs in both the forward and backward direction:
1513

1614
- *Decoding*: the "forward" direction: `Input -> Output`. This is what regular `.parse()` does.
1715
- *Encoding*: the "backward" direction: `Output -> Input`.
1816

19-
20-
You probably already know how to parse data with Zod:
17+
The regular `.parse()` method performs a *decode* operation (forward direction).
2118

2219
```ts
2320
import * as z from "zod";
@@ -31,7 +28,7 @@ mySchema.parse("hello"); // => "hello"
3128
z.parse(mySchema, "hello"); // => "hello"
3229
```
3330

34-
Zod also provides dedicated functions for performing "decode" and "encode" operations.
31+
For explicitness, Zod provides dedicated functions for performing "decode" and "encode" operations.
3532

3633
```ts
3734
z.decode(mySchema, "hello"); // => "hello"

play.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import * as z from "zod";
22

33
z;
44

5-
const stringToDate = z.codec(
6-
z.iso.datetime(), // input schema: ISO date string
7-
z.date(), // output schema: Date object
8-
{
9-
decode: (isoString) => new Date(isoString),
10-
encode: (date) => date.toISOString(),
11-
}
12-
);
13-
14-
const schema = stringToDate.refine((date) => date.getFullYear() > 2000, "must be after 2000");
5+
export const Field1Schema = z.strictObject({
6+
"field1.1": z.string(),
7+
"field1.2": z.number(),
8+
});
9+
10+
export const Field2Schema = z.strictObject({
11+
"field2.1": z.boolean(),
12+
"field2.2": z.date(),
13+
});
14+
15+
export const SomeSchema = z.strictObject({
16+
field1: Field1Schema as typeof Field1Schema,
17+
field2: Field2Schema as typeof Field2Schema,
18+
});

0 commit comments

Comments
 (0)