0
0
NestJSframework~20 mins

Why DTOs enforce data contracts in NestJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DTO Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do DTOs help maintain consistent data in NestJS?

In NestJS, Data Transfer Objects (DTOs) are used to define the shape of data sent between client and server. Why does using DTOs help enforce data contracts?

ADTOs automatically encrypt data to secure it during transfer.
BDTOs define a fixed structure that incoming data must match, preventing unexpected or missing fields.
CDTOs allow any data shape, so developers can send flexible data without restrictions.
DDTOs replace the need for database models by storing data directly.
Attempts:
2 left
💡 Hint

Think about how defining a shape or schema helps catch errors early.

component_behavior
intermediate
2:00remaining
What happens when invalid data is sent to a NestJS controller expecting a DTO?

Consider a NestJS controller method that expects a DTO with a required name string property. What will happen if the client sends a request missing the name field?

NestJS
import { IsString } from 'class-validator';

export class CreateUserDto {
  @IsString()
  name: string;
}

@Post()
createUser(@Body() createUserDto: CreateUserDto) {
  return `User ${createUserDto.name} created`;
}
ANestJS will crash the server with an unhandled exception.
BNestJS will accept the request and create a user with an undefined name.
CNestJS will ignore the missing field and fill it with an empty string.
DNestJS will throw a validation error and respond with a 400 Bad Request.
Attempts:
2 left
💡 Hint

Think about how validation pipes work with DTOs in NestJS.

📝 Syntax
advanced
2:00remaining
Which DTO definition correctly enforces a required string and optional number in NestJS?

Choose the correct DTO class that enforces title as a required string and count as an optional number.

A
import { IsString, IsNumber, IsOptional } from 'class-validator';

export class ItemDto {
  @IsString()
  title: string;

  @IsNumber()
  @IsOptional()
  count?: number;
}
B
export class ItemDto {
  title?: string;
  count: number;
}
C
export class ItemDto {
  @IsNumber()
  title: number;

  @IsString()
  count?: string;
}
D
export class ItemDto {
  @IsOptional()
  title: string;

  count: number;
}
Attempts:
2 left
💡 Hint

Look for correct use of decorators and optional property syntax.

🔧 Debug
advanced
2:00remaining
Why does this DTO validation not reject missing fields?

Given this DTO and controller, why does NestJS accept requests missing the email field?

NestJS
import { IsEmail, IsString } from 'class-validator';

export class RegisterDto {
  @IsEmail()
  email: string;

  @IsString()
  password: string;
}

@Post('register')
register(@Body() registerDto: RegisterDto) {
  return 'Registered';
}
AThe ValidationPipe is not enabled globally or on the route, so validation is skipped.
BThe <code>@IsEmail()</code> decorator is incorrect and does not validate the field.
CThe <code>email</code> property is optional by default, so missing is allowed.
DNestJS does not support validation on DTOs without explicit type annotations.
Attempts:
2 left
💡 Hint

Think about how validation is triggered in NestJS.

state_output
expert
2:00remaining
What is the output when sending extra fields not in the DTO with ValidationPipe whitelist enabled?

Consider this DTO and controller with ValidationPipe configured with whitelist: true. What will the response contain if the client sends extra fields?

NestJS
import { IsString } from 'class-validator';

export class UpdateProfileDto {
  @IsString()
  username: string;
}

@Post('update')
updateProfile(@Body() updateProfileDto: UpdateProfileDto) {
  return updateProfileDto;
}

// ValidationPipe setup:
// app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
AServer crashes due to unexpected fields in the DTO.
B{"username":"newuser", "age":30} - extra fields are kept in the object.
C{"username":"newuser"} - extra fields are removed from the object.
DValidation error response because extra fields are not allowed.
Attempts:
2 left
💡 Hint

What does the whitelist option do in ValidationPipe?