Skip to Content
Yup

Yup validation example

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

Result

Code

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