Nest pipes and DTO validation
Framework lane: NestJS 11.x candidate, TypeScript legacy decorators and emitDecoratorMetadata, compatible reflect-metadata and RxJS. Exact supported patch, dependency lock, advisory review and execution are NOT RUN. The framework code is a static candidate; do not install dependencies or expose a server automatically. The local companion uses Node.js 22.16.0 and requires no third-party packages. A decorator such as @Controller attaches framework metadata; a TypeScript annotation describes a compile-time contract. The full TypeScript chapter follows this introduction. Use a reviewed Nest TypeScript project, not node directly on the .ts file.
Framework example — staged
import { Body, Controller, Post, UsePipes, ValidationPipe } from '@nestjs/common';import { IsInt, Min, Max } from 'class-validator';class CreateOrderDto { @IsInt() @Min(1) @Max(20) quantity!: number;}@Controller('orders')export class OrdersController { @Post() @UsePipes(new ValidationPipe({whitelist:true,forbidNonWhitelisted:true})) create(@Body() dto: CreateOrderDto) { return {quantity:dto.quantity}; }}Expected behavior (not observed): Partial controller with class-validator and class-transformer installed in the reviewed project. Quantity 2 should succeed; 0, "2" and unknown extra fields should yield 400 under this configuration.
How it works
A TypeScript annotation alone does not check incoming JSON. ValidationPipe can apply class-validator rules and class-transformer integration in a configured app. Use a class (not an erased interface) for runtime metadata. Whitelisting restricts decorated fields; forbidNonWhitelisted rejects extra properties. Do not enable implicit coercion casually. Domain rules and authorization remain separate from shape checks.
Practice the underlying idea without dependencies
function check(body){ if(!body || typeof body!=='object' || Array.isArray(body)) return false; return Object.keys(body).length===1 && Number.isInteger(body.quantity) && body.quantity>=1 && body.quantity<=20;}console.log(JSON.stringify([{quantity:2},{quantity:'2'},{quantity:0},{quantity:2,extra:true},null].map(check)));Run node main.mjs in a disposable directory. Expected output:
[true,false,false,false,false]This tests the illustrated JavaScript behavior only, not the framework.
Common mistake
An interface or a type assertion does not validate JSON at runtime. Confirm decorator metadata and validator dependencies; never rely solely on validation to prevent unauthorized access.
Apply it yourself
Write valid and invalid DTO cases for a bounded quantity and document whether coercion is allowed.
Further reading
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nest pipes and DTO validation — knowledge check
Apply the lesson’s contracts and identify common boundary mistakes.
Why use a DTO class here?
What should happen to quantity "2" in the shown strict contract?
What does forbidNonWhitelisted do with extra fields in this configuration?