Skip to Content
ValidationYup (obsolete)

Yup Validation (obsolete)

⚠️
Warning

The library uses @sodaru/yup-to-json-schema  to convert Yup models to JSON Schema, and testing has shown dissatisfactory results (for example it was unable to handle anyOf properly). Therefore, I can’t recommend using this library until the issues are resolved. Yet, because I noticed that too late, I decided to keep it as part of the project, since its tests helps to detect type problems early.

Validation with Yup is performed with vovk-yup package, which provides a withYup function to create validated handlers.

npm install vovk-yup
src/lib/withYup.ts
import * as yup from 'yup'; import { prefix, post, openapi, type VovkOutput } from 'vovk'; import { withYup } from 'vovk-yup'; @prefix('users') export default class UserController { @openapi({ summary: 'Update user (Yup)', description: 'Update user by ID with Yup validation', }) @post('{id}') static updateUser = withYup({ body: yup .object() .required() .shape({ name: yup.string().required().meta({ description: 'User full name' }), age: yup.number().min(0).max(120).required().meta({ description: 'User age' }), email: yup.string().email().required().meta({ description: 'User email' }), }) .meta({ description: 'User object' }), params: yup .object() .shape({ id: yup.string().required().meta({ description: 'User ID' }), }) .required(), query: yup .object() .shape({ notify: yup.string().oneOf(['email', 'push', 'none']).required().meta({ description: 'Notification type' }), }) .required(), output: yup .object() .shape({ success: yup.boolean().required().meta({ description: 'Success status' }), }) .meta({ description: 'Response object' }) .required(), async handle(req, { id }) { const { name, age } = await req.json(); const notify = req.nextUrl.searchParams.get('notify'); // do something with the data console.log(`Updating user ${id}:`, { name, age, notify }); return { success: true, }; }, }); }
Last updated on