Skip to Content
ValidationYup (obsolete)

Yup Validation (Obsolete)

Warning

This library uses @sodaru/yup-to-json-schema  to convert Yup models to JSON Schema, and testing has shown unsatisfactory results (for example, incorrect handling of anyOf). Therefore, it’s not recommended until the issues are resolved. Because this was noticed late, it remains in the project, and existing tests help catch type issues early.

Validation with Yup is provided by the vovk-yup package, which exposes a withYup function for building validated handlers.

npm install vovk-yup@draft
src/modules/user/UserController.ts
import * as yup from 'yup'; import { prefix, post, operation } from 'vovk'; import { withYup } from 'vovk-yup'; @prefix('users') export default class UserController { @operation({ 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