🦅 NestJS Beginner

What are DTOs (Data Transfer Objects) in NestJS?

Answer

A DTO (Data Transfer Object) is a class that defines the shape of data sent in a request body. In NestJS, DTOs are TypeScript classes (not interfaces) with property decorators from the class-validator library: @IsString(), @IsEmail(), @IsNotEmpty(), @IsOptional(), @MinLength(8). Use them with the ValidationPipe: @Body() createUserDto: CreateUserDto. The pipe validates the incoming body against the DTO and throws a 400 error with details if validation fails. DTOs also serve as documentation and enable Swagger UI to auto-generate request schemas with the @nestjs/swagger package.