Skip to content

Commit 9379c0c

Browse files
committed
Improve stringbool docs
1 parent 345864f commit 9379c0c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/docs/content/api.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,11 @@ strbool.parse(/* anything else */); // ZodError<[{ code: "invalid_value" }]>
733733
To customize the truthy and falsy values:
734734

735735
```ts
736+
// these are the defaults
736737
z.stringbool({
737-
truthy: ["yes", "true"],
738-
falsy: ["no", "false"]
739-
})
738+
truthy: ["true", "1", "yes", "on", "y", "enabled"],
739+
falsy: ["false", "0", "no", "off", "n", "disabled"],
740+
});
740741
```
741742

742743
Be default the schema is *case-insensitive*; all inputs are converted to lowercase before comparison to the `truthy`/`falsy` values. To make it case-sensitive:

play.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
import { z } from "zod/v4";
22

33
z;
4+
5+
function inferSchema<T extends z.core.$ZodType>(data: unknown, schema: T) {
6+
return parseData(data, schema);
7+
}
8+
9+
function parseData<T extends z.core.$ZodType>(data: unknown, schema: T): z.output<T> {
10+
return z.parse(schema, data);
11+
}
12+
13+
const A = z.union([z.stringbool(), z.literal([null, undefined]).transform((v) => !!v)]);
14+
15+
console.dir(A.parse(undefined), { depth: null });
16+
console.dir(A.parse(null), { depth: null });
17+
console.dir(A.parse("true"), { depth: null });
18+
console.dir(A.parse("false"), { depth: null });
19+
20+
z.stringbool({
21+
truthy: ["true", "1", "yes", "on", "y", "enabled"],
22+
falsy: ["false", "0", "no", "off", "n", "disabled"],
23+
});

0 commit comments

Comments
 (0)