DTOs & validation pipes
Fill-gap30 min
DTOs & validation pipes
A DTO declares exactly what a request body may contain:
export class CreateFavoriteDto {
@IsString() lessonId!: string;
}
@Post('favorites')
create(@Body() dto: CreateFavoriteDto) { … }The ValidationPipe checks every request against the DTO before your code runs, and rejects unknown or malformed fields with 400. Bad data never reaches the service — the boundary does the work.
This app uses Zod schemas for the same job (see src/roadmaps/dto). Same principle, different validator: the network is hostile, so the door checks papers.
Exercise
Write a DTO for creating a diagram (name required, lesson_id optional) and the controller method that consumes it.