Skip to Content
Valibot

Valibot validation example

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

Result

Code

UserValibotController.ts
ValibotFormExample.tsx
1import { prefix, post, openapi, type VovkOutput, createStandardValidation, KnownAny } from 'vovk';
2import { toJsonSchema } from '@valibot/to-json-schema';
3import * as v from 'valibot';
4
5const withValibot = createStandardValidation({
6 toJSONSchema: (model: v.BaseSchema<KnownAny, KnownAny, KnownAny>) => toJsonSchema(model),
7});
8
9@prefix('users-valibot')
10export default class UserValibotController {
11 @openapi({
12 summary: 'Update user (Valibot)',
13 description: 'Update user by ID with Valibot validation',
14 })
15 @post('{id}')
16 static updateUser = withValibot({
17 body: v.pipe(
18 v.object({
19 name: v.pipe(v.string(), v.description('User full name')),
20 age: v.pipe(v.number(), v.minValue(0), v.maxValue(120), v.description('User age')),
21 email: v.pipe(v.string(), v.email(), v.description('User email')),
22 }),
23 v.description('User object')
24 ),
25 params: v.object({
26 id: v.pipe(v.string(), v.uuid(), v.description('User ID')),
27 }),
28 query: v.object({
29 notify: v.pipe(v.picklist(['email', 'push', 'none']), v.description('Notification type')),
30 }),
31 output: v.pipe(
32 v.object({
33 success: v.pipe(v.boolean(), v.description('Success status')),
34 }),
35 v.description('Response object')
36 ),
37 async handle(req, { id }) {
38 const { name, age } = await req.json();
39 const notify = req.nextUrl.searchParams.get('notify');
40
41 // do something with the data
42 console.log(`Updating user ${id}:`, { name, age, notify });
43 return {
44 success: true,
45 } satisfies VovkOutput<typeof UserValibotController.updateUser>;
46 },
47 });
48}
1import { prefix, post, openapi, type VovkOutput, createStandardValidation, KnownAny } from 'vovk';
2import { toJsonSchema } from '@valibot/to-json-schema';
3import * as v from 'valibot';
4
5const withValibot = createStandardValidation({
6 toJSONSchema: (model: v.BaseSchema<KnownAny, KnownAny, KnownAny>) => toJsonSchema(model),
7});
8
9@prefix('users-valibot')
10export default class UserValibotController {
11 @openapi({
12 summary: 'Update user (Valibot)',
13 description: 'Update user by ID with Valibot validation',
14 })
15 @post('{id}')
16 static updateUser = withValibot({
17 body: v.pipe(
18 v.object({
19 name: v.pipe(v.string(), v.description('User full name')),
20 age: v.pipe(v.number(), v.minValue(0), v.maxValue(120), v.description('User age')),
21 email: v.pipe(v.string(), v.email(), v.description('User email')),
22 }),
23 v.description('User object')
24 ),
25 params: v.object({
26 id: v.pipe(v.string(), v.uuid(), v.description('User ID')),
27 }),
28 query: v.object({
29 notify: v.pipe(v.picklist(['email', 'push', 'none']), v.description('Notification type')),
30 }),
31 output: v.pipe(
32 v.object({
33 success: v.pipe(v.boolean(), v.description('Success status')),
34 }),
35 v.description('Response object')
36 ),
37 async handle(req, { id }) {
38 const { name, age } = await req.json();
39 const notify = req.nextUrl.searchParams.get('notify');
40
41 // do something with the data
42 console.log(`Updating user ${id}:`, { name, age, notify });
43 return {
44 success: true,
45 } satisfies VovkOutput<typeof UserValibotController.updateUser>;
46 },
47 });
48}
Last updated on