Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for coercing nullish values #3067

Merged
merged 4 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading