0
0
NestJSframework~5 mins

Nested DTO validation in NestJS

Choose your learning style9 modes available
Introduction

Nested DTO validation helps check data inside objects within objects. It makes sure all parts of your data are correct before using them.

When you receive complex data with objects inside other objects, like a user with an address.
When you want to keep your validation rules organized by splitting them into smaller parts.
When you want to reuse validation rules for nested parts in different places.
When you want to avoid manual checks and let the framework handle deep validation automatically.
Syntax
NestJS
import { Type } from 'class-transformer';
import { ValidateNested, IsString } from 'class-validator';

class ChildDTO {
  @IsString()
  name: string;
}

class ParentDTO {
  @ValidateNested()
  @Type(() => ChildDTO)
  child: ChildDTO;
}

Use @ValidateNested() to tell NestJS to validate the nested object.

Use @Type(() => ChildDTO) from class-transformer to help NestJS know the nested object type.

Examples
This example shows a user with a nested address object. Both user and address fields are validated.
NestJS
import { IsString, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

class AddressDTO {
  @IsString()
  street: string;

  @IsString()
  city: string;
}

class UserDTO {
  @IsString()
  name: string;

  @ValidateNested()
  @Type(() => AddressDTO)
  address: AddressDTO;
}
This example shows validation of an array of nested objects. Each item in the array is validated.
NestJS
import { IsString, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

class ItemDTO {
  @IsString()
  title: string;
}

class OrderDTO {
  @ValidateNested({ each: true })
  @Type(() => ItemDTO)
  items: ItemDTO[];
}
Sample Program

This NestJS controller accepts a POST request to create a user. The user data includes a nested profile object. Both username and profile.bio are validated as strings.

NestJS
import { Controller, Post, Body } from '@nestjs/common';
import { IsString, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

class ProfileDTO {
  @IsString()
  bio: string;
}

class CreateUserDTO {
  @IsString()
  username: string;

  @ValidateNested()
  @Type(() => ProfileDTO)
  profile: ProfileDTO;
}

@Controller('users')
export class UsersController {
  @Post()
  createUser(@Body() createUserDto: CreateUserDTO) {
    return `User ${createUserDto.username} with bio '${createUserDto.profile.bio}' created.`;
  }
}
OutputSuccess
Important Notes

Always use @Type(() => NestedDTO) with @ValidateNested() for nested validation to work.

If you forget @Type(), nested validation will not run correctly.

For arrays of nested objects, add { each: true } option to @ValidateNested().

Summary

Nested DTO validation checks data inside objects within objects automatically.

Use @ValidateNested() and @Type() together for nested validation.

This keeps your data safe and your code clean by reusing validation rules.