Skip to Content
DTO

DTO (class-validator) Validation Example

Form input handling and class validation  with vovk-dto . The request input is validated on the client-side before being sent to the server where it is validated again. Notice that the input data needs to be transformed with class-transformer  into a DTO class in order to be validated on the client-side.

Result

Code

src/modules/dto/UserDtoController.ts
import { prefix, post, operation } from 'vovk'; import { withDto } from 'vovk-dto'; import { UpdateUserBodyDto, UpdateUserParamsDto, UpdateUserQueryDto, UpdateUserResponseDto } from './UserDto.ts'; @prefix('users-dto') export default class UserDtoController { @operation({ summary: 'Update user (DTO)', description: 'Update user by ID with DTO validation', }) @post('{id}') static updateUser = withDto({ body: UpdateUserBodyDto, params: UpdateUserParamsDto, query: UpdateUserQueryDto, output: UpdateUserResponseDto, async handle(req, { id }) { const { name, age, email } = await req.json(); const notify = req.nextUrl.searchParams.get('notify'); // do something with the data console.log(`Updating user ${id}:`, { name, age, email, notify }); return { success: true, } satisfies UpdateUserResponseDto; }, }); }

The code above is fetched from GitHub repository. 

Last updated on