📖 Scenario: You are building a simple API for a bookstore. Each book has a title and an author. The author itself has a name and an email. You want to make sure that when someone sends data to create a book, both the book and the author data are checked carefully.
🎯 Goal: Create nested DTO classes for Book and Author with validation rules. Then use these DTOs in a controller to validate incoming data automatically.
📋 What You'll Learn
Create a DTO class called AuthorDto with name and email properties.
Add validation decorators to AuthorDto: name must be a non-empty string, email must be a valid email.
Create a DTO class called BookDto with title and author properties.
Add validation decorators to BookDto: title must be a non-empty string, author must be a nested AuthorDto.
Use @ValidateNested() and @Type(() => AuthorDto) decorators to enable nested validation.
Create a simple controller method that accepts BookDto and validates the input automatically.
💡 Why This Matters
🌍 Real World
APIs often receive complex data with nested objects. Validating nested DTOs ensures data integrity and prevents errors early.
💼 Career
Backend developers use nested DTO validation in NestJS to build robust APIs that handle complex data safely and clearly.
Progress0 / 4 steps
1
Create the AuthorDto class with properties
Create a class called AuthorDto with two properties: name and email. Both should be strings.
NestJS
Hint
Define a class with two string properties named exactly name and email.
2
Add validation decorators to AuthorDto
Add @IsString() and @IsNotEmpty() decorators to name, and @IsEmail() decorator to email in the AuthorDto class. Import these decorators from class-validator.
NestJS
Hint
Use @IsString() and @IsNotEmpty() on name, and @IsEmail() on email.
3
Create BookDto with nested AuthorDto and validation
Create a class called BookDto with a title property as a non-empty string and an author property of type AuthorDto. Use @IsString() and @IsNotEmpty() on title. Use @ValidateNested() and @Type(() => AuthorDto) on author. Import ValidateNested from class-validator and Type from class-transformer.
NestJS
Hint
Use @ValidateNested() and @Type(() => AuthorDto) on the author property to enable nested validation.
4
Use BookDto in a controller method with validation pipe
Create a NestJS controller class called BooksController with a createBook method. This method should accept a BookDto parameter decorated with @Body() and use ValidationPipe to validate the input automatically. Import necessary decorators and pipes from @nestjs/common.
NestJS
Hint
Use @Body(new ValidationPipe()) to validate the incoming BookDto automatically.
Practice
(1/5)
1. What is the main purpose of using @ValidateNested() in a NestJS DTO?
easy
A. To define default values for nested properties
B. To convert nested objects to plain JavaScript objects
C. To skip validation of nested objects
D. To validate properties of nested objects inside the DTO automatically
Solution
Step 1: Understand the role of @ValidateNested()
This decorator tells NestJS to validate nested objects inside a DTO, not just the top-level properties.
Step 2: Compare with other options
@Type() handles transformation, not validation. Skipping validation or setting defaults is unrelated to @ValidateNested().
Final Answer:
To validate properties of nested objects inside the DTO automatically -> Option D
Hint: Missing required nested fields cause validation failure [OK]
Common Mistakes:
Assuming missing nested fields are allowed by default
Ignoring the effect of @ValidateNested()
Confusing missing nested fields with missing top-level fields
4. Consider this DTO code snippet:
class ProfileDto {
@IsString()
bio: string;
}
class UserDto {
@IsString()
username: string;
@ValidateNested()
profile: ProfileDto;
}
Why will validation NOT work correctly for the nested profile property?
medium
A. Because @ValidateNested() should be on ProfileDto class
B. Because @IsString() is missing on profile
C. Because @Type(() => ProfileDto) decorator is missing on profile
D. Because bio property is missing @ValidateNested()
Solution
Step 1: Identify missing decorator for nested validation
For nested validation to work, @Type(() => ProfileDto) must be added to the nested property to transform plain objects into class instances.
Step 2: Understand why other options are incorrect
@IsString() is for string properties, not objects. @ValidateNested() belongs on the nested property, not the nested class. bio is a string, no nested validation needed.
Final Answer:
Because @Type(() => ProfileDto) decorator is missing on profile -> Option C
Quick Check:
Missing @Type() breaks nested validation [OK]
Hint: Always add @Type() with @ValidateNested() for nested DTOs [OK]
Common Mistakes:
Forgetting @Type() on nested properties
Misplacing @ValidateNested() on nested class
Confusing property decorators with class decorators
5. You have a CompanyDto with a nested array of EmployeeDto objects. Which is the correct way to validate this nested array properly?