0
0
NestjsDebug / FixBeginner · 4 min read

How to Fix Validation Error in NestJS Quickly

To fix validation errors in NestJS, ensure you use ValidationPipe globally or on controllers and decorate your DTO properties with class-validator decorators like @IsString(). Also, enable whitelist and forbidNonWhitelisted options in ValidationPipe to control input strictly.
🔍

Why This Happens

Validation errors in NestJS usually happen because the incoming data does not match the expected format defined in your DTO (Data Transfer Object). This can be due to missing decorators, not enabling the validation pipe, or sending unexpected fields.

typescript
import { Controller, Post, Body } from '@nestjs/common';

class CreateUserDto {
  name: string;
  age: number;
}

@Controller('users')
export class UsersController {
  @Post()
  createUser(@Body() createUserDto: CreateUserDto) {
    return createUserDto;
  }
}
Output
ValidationError: Validation failed for property 'name' or 'age' because no validation decorators were used.
🔧

The Fix

Fix the error by adding validation decorators from class-validator to your DTO properties and enable ValidationPipe globally or on the route. This ensures NestJS checks the data before processing.

typescript
import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common';
import { IsString, IsInt, Min } from 'class-validator';

class CreateUserDto {
  @IsString()
  name: string;

  @IsInt()
  @Min(0)
  age: number;
}

@Controller('users')
export class UsersController {
  @Post()
  @UsePipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }))
  createUser(@Body() createUserDto: CreateUserDto) {
    return createUserDto;
  }
}
Output
When sending valid data, the server responds with the user data; invalid data triggers a clear validation error.
🛡️

Prevention

Always use ValidationPipe globally in your main app file with options like whitelist and forbidNonWhitelisted to automatically validate all incoming requests. Use class-validator decorators on DTOs to define clear rules. This prevents invalid data from reaching your business logic.

typescript
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true }));
  await app.listen(3000);
}
bootstrap();
Output
All incoming requests are validated automatically, reducing errors and improving security.
⚠️

Related Errors

  • Missing ValidationPipe: Requests are not validated, causing unexpected data to pass through.
  • Incorrect DTO types: Using wrong types or missing decorators causes validation to fail silently.
  • Extra fields in request: Without whitelist, extra fields are accepted, which might cause issues.

Key Takeaways

Always use ValidationPipe with whitelist and forbidNonWhitelisted options to enforce strict validation.
Decorate DTO properties with class-validator decorators like @IsString and @IsInt for clear validation rules.
Enable global validation in main.ts to avoid missing validation on any route.
Validation errors help catch bad data early and keep your app safe and stable.
Check error messages carefully to identify which property failed validation.