Under The Hood
backend nestjs NestJS 10+ (ValidationPipe)

Validation Pipes: class-validator, class-transformer Transformation Pipeline

Last updated
Prerequisites:
TypeScript decorators
DTO (Data Transfer Object) pattern
  • nestjs
  • pipes
  • validation-pipe
  • class-validator
  • class-transformer
  • dto

Read at your depth

The practical view

Pipes in NestJS serve two purposes: **Transformation** (converting input data to the desired type, like `ParseIntPipe`) and **Validation** (verifying incoming request body against a DTO class using `ValidationPipe`). By decorating DTO fields with `class-validator` annotations (`@IsString()`, `@IsEmail()`, `@Min(18)`), `ValidationPipe` automatically validates and converts JSON payloads, throwing a `400 Bad Request` with structured error details on failure.

The same idea in other frameworks

nestjs equivalent
`@UsePipes(new ValidationPipe({ whitelist: true }))` with `class-validator` DTOs.

Legacy vs modern

Manual Request Body Validation vs Automatic ValidationPipe

Manual validation in every controller is error-prone and insecure; `ValidationPipe` strips unknown properties and enforces DTO contracts.

before → after
Manual Controller Checking
if (!body.email || !body.email.includes('@')) throw new BadRequestException(); // ❌ Fragile
ValidationPipe with DTO
export class CreateUserDto { @IsEmail() email: string; @Min(18) age: number; } // ✅ NEW

Interview gotchas

Context

Interviewers test your knowledge of mass-assignment vulnerabilities in REST APIs.

The mechanical answer

By default, `plainToInstance` leaves un-declared properties attached to the request object. If an attacker sends `{ "email": "user@test.com", "role": "admin", "balance": 99999 }`, those extra fields could be passed directly to the database. Setting `whitelist: true` **silently strips all un-declared properties** not decorated with validation annotations. Setting `forbidNonWhitelisted: true` **rejects the request immediately with a 400 error** if any unknown property is present.

Trap

Any DTO property lacking a `class-validator` decorator will be stripped if `whitelist: true` is enabled.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.