Skip to content

Commit

Permalink
Add docs for coercing nullish values (#3067)
Browse files Browse the repository at this point in the history
* Add docs for coercing nullish values

* Clean up coerce doc

* Tweak

---------

Co-authored-by: Colin McDonnell <[email protected]>
  • Loading branch information
rbuetzer and colinhacks authored Feb 3, 2024
1 parent 7e9f3a4 commit 0bf94b2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,16 +715,19 @@ Zod now provides a more convenient way to coerce primitive values.
const schema = z.coerce.string();
schema.parse("tuna"); // => "tuna"
schema.parse(12); // => "12"
schema.parse(true); // => "true"
```

During the parsing step, the input is passed through the `String()` function, which is a JavaScript built-in for coercing data into strings. Note that the returned schema is a `ZodString` instance so you can use all string methods.
During the parsing step, the input is passed through the `String()` function, which is a JavaScript built-in for coercing data into strings.

The returned schema is a normal `ZodString` instance so you can use all string methods.

```ts
z.coerce.string().email().min(5);
```
All primitive types support coercion.
**How coercion works**
All primitive types support coercion. Zod coerces all inputs using the built-in constructors: `String(input)`, `Number(input)`, `new Date(input)`, etc.
```ts
z.coerce.string(); // String(input)
Expand All @@ -734,9 +737,19 @@ z.coerce.bigint(); // BigInt(input)
z.coerce.date(); // new Date(input)
```
Note that some behavior may not be what you expect.
```ts
schema.parse(true); // => "true"
schema.parse(undefined); // => "undefined"
schema.parse(null); // => "null"
```
For more control over coercion logic, consider using [`z.preprocess`](#preprocess) or [`z.pipe()`](#pipe).
**Boolean coercion**
Zod's boolean coercion is very simple! It passes the value into the `Boolean(value)` function, that's it. Any truthy value will resolve to `true`, any falsy value will resolve to `false`.
Zod's approach to coercion is very simple! It passes the value into the `Boolean(value)` function, that's it. Any truthy value will resolve to `true`, any falsy value will resolve to `false`.
```ts
z.coerce.boolean().parse("tuna"); // => true
Expand Down
21 changes: 17 additions & 4 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -714,16 +714,19 @@ Zod now provides a more convenient way to coerce primitive values.
const schema = z.coerce.string();
schema.parse("tuna"); // => "tuna"
schema.parse(12); // => "12"
schema.parse(true); // => "true"
```

During the parsing step, the input is passed through the `String()` function, which is a JavaScript built-in for coercing data into strings. Note that the returned schema is a `ZodString` instance so you can use all string methods.
During the parsing step, the input is passed through the `String()` function, which is a JavaScript built-in for coercing data into strings.

The returned schema is a normal `ZodString` instance so you can use all string methods.

```ts
z.coerce.string().email().min(5);
```
All primitive types support coercion.
**How coercion works**
All primitive types support coercion. Zod coerces all inputs using the built-in constructors: `String(input)`, `Number(input)`, `new Date(input)`, etc.
```ts
z.coerce.string(); // String(input)
Expand All @@ -733,9 +736,19 @@ z.coerce.bigint(); // BigInt(input)
z.coerce.date(); // new Date(input)
```
Note that some behavior may not be what you expect.
```ts
schema.parse(true); // => "true"
schema.parse(undefined); // => "undefined"
schema.parse(null); // => "null"
```
For more control over coercion logic, consider using [`z.preprocess`](#preprocess) or [`z.pipe()`](#pipe).
**Boolean coercion**
Zod's boolean coercion is very simple! It passes the value into the `Boolean(value)` function, that's it. Any truthy value will resolve to `true`, any falsy value will resolve to `false`.
Zod's approach to coercion is very simple! It passes the value into the `Boolean(value)` function, that's it. Any truthy value will resolve to `true`, any falsy value will resolve to `false`.
```ts
z.coerce.boolean().parse("tuna"); // => true
Expand Down
28 changes: 1 addition & 27 deletions playground.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
import { z } from "./src";
import * as mod from "node:module";
z;

// @ts-ignore
// const arg = await import("./index");
// arg;
// require.resolve(arg);

load: {
}
interface A<T> {
name: T;
children: any;
render(): ReadableStream | string;
}
function Hyper<T>(arg: T) {
return function () {} as any as {
prototype: A<T>;
new (): A<T>;
};
}

export default class extends Hyper({ name: 1234 }) {
render() {
// this.name.name;
return `asdf ${this.children} asdf`;
}
}
z;

0 comments on commit 0bf94b2

Please sign in to comment.