NestJS

Nest ValidationPipe

A pipe transforms or validates an argument before a Nest controller method receives it. DTO classes describe input fields and can carry validation metadata. 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. Static example only; runtime NOT RUN. Source: https://docs.nestjs.com/techniques/validation.

Formal Syntax & Structure

nodejs syntaxnodejs
new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true })

Working Example

Example Snippetnodejs
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}; }}
Node.js Reference