Skip to content

Commit d5c9432

Browse files
committed
docs: mention about valibot
1 parent b27aba1 commit d5c9432

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/kit/rpc.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,44 @@ const getLiveMetrics = defineRpcFunction({
198198
> [!TIP]
199199
> If your data genuinely needs live server state, use `type: 'query'` without dumps. The function will work in dev mode but gracefully fail in build mode.
200200
201+
## Schema Validation (Optional)
202+
203+
The RPC system has built-in support for runtime schema validation using [Valibot](https://valibot.dev). When you provide schemas, TypeScript types are automatically inferred and validation happens at runtime.
204+
205+
```ts
206+
import { defineRpcFunction } from '@vitejs/devtools-kit'
207+
import * as v from 'valibot'
208+
209+
const getModule = defineRpcFunction({
210+
name: 'my-plugin:get-module',
211+
type: 'query',
212+
args: [
213+
v.string(),
214+
v.optional(v.object({
215+
includeSource: v.boolean(),
216+
})),
217+
],
218+
returns: v.object({
219+
id: v.string(),
220+
source: v.optional(v.string()),
221+
}),
222+
setup: () => ({
223+
handler: (id, options) => {
224+
// Types are automatically inferred from schemas
225+
// id: string
226+
// options: { includeSource: boolean } | undefined
227+
return {
228+
id,
229+
source: options?.includeSource ? '...' : undefined,
230+
}
231+
},
232+
}),
233+
})
234+
```
235+
236+
> [!NOTE]
237+
> Schema validation is optional. If you don't provide `args` or `returns` schemas, the RPC system will work without validation and you can use regular TypeScript types instead.
238+
201239
## Client-Side Calls
202240

203241
### In Iframe Pages

0 commit comments

Comments
 (0)