You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/docs/api-reference/workflow/define-hook.mdx
+37-11Lines changed: 37 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,10 +8,10 @@ import { generateDefinition } from "@/lib/tsdoc"
8
8
9
9
Creates a type-safe hook helper that ensures the payload type is consistent between hook creation and resumption.
10
10
11
-
This is a lightweight wrapper around [`createHook()`](/docs/api-reference/workflow/create-hook) and [`resumeHook()`](/docs/api-reference/workflow-api/resume-hook) to avoid type mismatches.
11
+
This is a lightweight wrapper around [`createHook()`](/docs/api-reference/workflow/create-hook) and [`resumeHook()`](/docs/api-reference/workflow-api/resume-hook) to avoid type mismatches. It also supports optional runtime validation and transformation of payloads using any [Standard Schema v1](https://standardschema.dev) compliant validator like Zod or Valibot.
12
12
13
13
<Callout>
14
-
We recommend using `defineHook()` over `createHook()` in production codebases for better type safety.
14
+
We recommend using `defineHook()` over `createHook()` in production codebases for better type safety and optional runtime validation.
15
15
</Callout>
16
16
17
17
```ts lineNumbers
@@ -67,7 +67,7 @@ export default DefineHook;`
67
67
68
68
## Examples
69
69
70
-
### Type-Safe Hook Definition
70
+
### Basic Type-Safe Hook Definition
71
71
72
72
By defining the hook once with a specific payload type, you can reuse it in multiple workflows and API routes with automatic type safety.
73
73
@@ -117,16 +117,21 @@ export async function POST(request: Request) {
117
117
118
118
### Validate and Transform with Schema
119
119
120
-
The optional `schema` accepts any validator that conforms to [Standard Schema v1](https://standardschema.dev).
120
+
You can provide runtime validation and transformation of hook payloads using the `schema`option. This option accepts any validator that conforms to the [Standard Schema v1](https://standardschema.dev) specification.
121
121
122
-
Zod is shown below as one example, but libraries like Valibot, ArkType, Effect Schema, or your own custom validator work as well.
122
+
<Callouttype="info">
123
+
Standard Schema is a standardized specification for schema validation libraries. Most popular validation libraries support it, including Zod, Valibot, ArkType, and Effect Schema. You can also write custom validators.
124
+
</Callout>
125
+
126
+
#### Using Zod with defineHook
127
+
128
+
Here's an example using [Zod](https://zod.dev) to validate and transform hook payloads:
123
129
124
130
```typescript lineNumbers
125
131
import { defineHook } from"workflow";
126
132
import { z } from"zod";
127
133
128
134
exportconst approvalHook =defineHook({
129
-
// Provide a schema to validate/transform payloads.
The [`defineHook()`](/docs/api-reference/workflow/define-hook) helper provides type safety between creating and resuming hooks:
365
+
The [`defineHook()`](/docs/api-reference/workflow/define-hook) helper provides type safety and runtime validation between creating and resuming hooks using [Standard Schema v1](https://standardschema.dev). Use any compliant validator like Zod or Valibot:
This pattern is especially valuable in larger applications where the workflow and API code are in separate files.
407
+
This pattern is especially valuable in larger applications where the workflow and API code are in separate files, providing both compile-time type safety and runtime validation.
0 commit comments