Skip to content

Commit d7a936a

Browse files
IdanGonenclaude
andcommitted
docs: add dynamic error message and combined refinement examples for .refine()
Addresses issue #5998 by adding to the `.refine()` `error` subsection: - Dynamic error message example using `error` as a function with `iss.input` - Combined `.refine()` + `.superRefine()` example with guidance on when to use each - Best-practices note pointing users to `.superRefine()` for multi-issue scenarios Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b6071fc commit d7a936a

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

packages/docs/content/api.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,31 @@ const myString = z.string().check(
21272127
</Tab>
21282128
</Tabs>
21292129

2130+
The `error` option also accepts a **function** for dynamic error messages. The function receives the raw issue object, including `iss.input` (the value that failed validation):
2131+
2132+
<Tabs groupId="lib" items={["Zod", "Zod Mini"]}>
2133+
<Tab value="Zod">
2134+
```ts
2135+
const myString = z.string().refine(
2136+
(val) => val.startsWith("hello"),
2137+
{
2138+
error: (iss) => `Expected greeting, got "${iss.input}"`,
2139+
}
2140+
);
2141+
```
2142+
</Tab>
2143+
<Tab value="Zod Mini">
2144+
```ts
2145+
const myString = z.string().check(
2146+
z.refine(
2147+
(val) => val.startsWith("hello"),
2148+
{ error: (iss) => `Expected greeting, got "${iss.input}"` }
2149+
)
2150+
);
2151+
```
2152+
</Tab>
2153+
</Tabs>
2154+
21302155
#### `abort`
21312156

21322157
By default, validation issues from checks are considered *continuable*; that is, Zod will execute *all* checks in sequence, even if one of them causes a validation error. This is usually desirable, as it means Zod can surface as many errors as possible in one go.

0 commit comments

Comments
 (0)