Skip to Content
Zod

Zod validation example

Form input handling and Zod validation with vovk-zod. The request input is validated on the client-side before being sent to the server where it is validated again.

Result

Code

UserZodController.ts
ZodFormExample.tsx
1import { z } from 'zod/v4';
2import { prefix, post, openapi, type VovkOutput } from 'vovk';
3import { withZod } from 'vovk-zod';
4
5@prefix('users-zod')
6export default class UserZodController {
7 @openapi({
8 summary: 'Update user (Zod)',
9 description: 'Update user by ID with Zod validation',
10 })
11 @post('{id}')
12 static updateUser = withZod({
13 body: z
14 .object({
15 name: z.string().describe('User full name'),
16 age: z.number().min(0).max(120).describe('User age'),
17 email: z.email().describe('User email'),
18 })
19 .describe('User object'),
20 params: z.object({
21 id: z.uuid().describe('User ID'),
22 }),
23 query: z.object({
24 notify: z.enum(['email', 'push', 'none']).describe('Notification type'),
25 }),
26 output: z
27 .object({
28 success: z.boolean().describe('Success status'),
29 })
30 .describe('Response object'),
31 async handle(req, { id }) {
32 const { name, age } = await req.json();
33 const notify = req.nextUrl.searchParams.get('notify');
34
35 // do something with the data
36 console.log(`Updating user ${id}:`, { name, age, notify });
37 return {
38 success: true,
39 } satisfies VovkOutput<typeof UserZodController.updateUser>;
40 },
41 });
42}
1import { z } from 'zod/v4';
2import { prefix, post, openapi, type VovkOutput } from 'vovk';
3import { withZod } from 'vovk-zod';
4
5@prefix('users-zod')
6export default class UserZodController {
7 @openapi({
8 summary: 'Update user (Zod)',
9 description: 'Update user by ID with Zod validation',
10 })
11 @post('{id}')
12 static updateUser = withZod({
13 body: z
14 .object({
15 name: z.string().describe('User full name'),
16 age: z.number().min(0).max(120).describe('User age'),
17 email: z.email().describe('User email'),
18 })
19 .describe('User object'),
20 params: z.object({
21 id: z.uuid().describe('User ID'),
22 }),
23 query: z.object({
24 notify: z.enum(['email', 'push', 'none']).describe('Notification type'),
25 }),
26 output: z
27 .object({
28 success: z.boolean().describe('Success status'),
29 })
30 .describe('Response object'),
31 async handle(req, { id }) {
32 const { name, age } = await req.json();
33 const notify = req.nextUrl.searchParams.get('notify');
34
35 // do something with the data
36 console.log(`Updating user ${id}:`, { name, age, notify });
37 return {
38 success: true,
39 } satisfies VovkOutput<typeof UserZodController.updateUser>;
40 },
41 });
42}
Last updated on