Skip to content

Commit daf771c

Browse files
committed
Update readme
1 parent 93d99a8 commit daf771c

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
- [`.or`](#or)
144144
- [`.and`](#and)
145145
- [`.brand`](#brand)
146-
- [`.pipe()`](#pipe)
146+
- [`.readonly`](#readonly)
147+
- [`.pipe`](#pipe)
147148
- [You can use `.pipe()` to fix common issues with `z.coerce`.](#you-can-use-pipe-to-fix-common-issues-with-zcoerce)
148149
- [Guides and concepts](#guides-and-concepts)
149150
- [Type inference](#type-inference)
@@ -2453,7 +2454,38 @@ type Cat = z.infer<typeof Cat>;
24532454

24542455
Note that branded types do not affect the runtime result of `.parse`. It is a static-only construct.
24552456

2456-
### `.pipe()`
2457+
### `.readonly`
2458+
2459+
`.readonly() => ZodReadonly<this>`
2460+
2461+
This method returns a `ZodReadonly` schema instance that parses the input using the base schema, then calls `Object.freeze()` on the result. The inferred type is also marked as `readonly`.
2462+
2463+
```ts
2464+
const schema = z.object({ name: string }).readonly();
2465+
type schema = z.infer<typeof schema>;
2466+
// Readonly<{name: string}>
2467+
2468+
const result = schema.parse({ name: "fido" });
2469+
result.name = "simba"; // error
2470+
```
2471+
2472+
The inferred type uses TypeScript's built-in readonly types when relevant.
2473+
2474+
```ts
2475+
z.array(z.string()).readonly();
2476+
// readonly string[]
2477+
2478+
z.tuple([z.string(), z.number()]).readonly();
2479+
// readonly [string, number]
2480+
2481+
z.map(z.string(), z.date()).readonly();
2482+
// ReadonlyMap<string, Date>
2483+
2484+
z.set(z.string()).readonly();
2485+
// ReadonlySet<Promise<string>>
2486+
```
2487+
2488+
### `.pipe`
24572489

24582490
Schemas can be chained into validation "pipelines". It's useful for easily validating the result after a `.transform()`:
24592491

deno/lib/README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
- [`.or`](#or)
144144
- [`.and`](#and)
145145
- [`.brand`](#brand)
146-
- [`.pipe()`](#pipe)
146+
- [`.readonly`](#readonly)
147+
- [`.pipe`](#pipe)
147148
- [You can use `.pipe()` to fix common issues with `z.coerce`.](#you-can-use-pipe-to-fix-common-issues-with-zcoerce)
148149
- [Guides and concepts](#guides-and-concepts)
149150
- [Type inference](#type-inference)
@@ -2453,7 +2454,38 @@ type Cat = z.infer<typeof Cat>;
24532454

24542455
Note that branded types do not affect the runtime result of `.parse`. It is a static-only construct.
24552456

2456-
### `.pipe()`
2457+
### `.readonly`
2458+
2459+
`.readonly() => ZodReadonly<this>`
2460+
2461+
This method returns a `ZodReadonly` schema instance that parses the input using the base schema, then calls `Object.freeze()` on the result. The inferred type is also marked as `readonly`.
2462+
2463+
```ts
2464+
const schema = z.object({ name: string }).readonly();
2465+
type schema = z.infer<typeof schema>;
2466+
// Readonly<{name: string}>
2467+
2468+
const result = schema.parse({ name: "fido" });
2469+
result.name = "simba"; // error
2470+
```
2471+
2472+
The inferred type uses TypeScript's built-in readonly types when relevant.
2473+
2474+
```ts
2475+
z.array(z.string()).readonly();
2476+
// readonly string[]
2477+
2478+
z.tuple([z.string(), z.number()]).readonly();
2479+
// readonly [string, number]
2480+
2481+
z.map(z.string(), z.date()).readonly();
2482+
// ReadonlyMap<string, Date>
2483+
2484+
z.set(z.string()).readonly();
2485+
// ReadonlySet<Promise<string>>
2486+
```
2487+
2488+
### `.pipe`
24572489

24582490
Schemas can be chained into validation "pipelines". It's useful for easily validating the result after a `.transform()`:
24592491

0 commit comments

Comments
 (0)