0
0
NestJSframework~20 mins

DTO class creation in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS DTO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a NestJS DTO class is used for validation?
Consider this DTO class in NestJS using class-validator decorators. What will happen if the input object misses the email property?

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

export class UserDto {
  @IsString()
  name: string;

  @IsEmail()
  email: string;
}
AValidation passes because only the name property is required.
BValidation fails because the email property is missing.
CValidation fails because name is missing, not email.
DValidation passes and email is set to null automatically.
Attempts:
2 left
💡 Hint
Think about what happens when a required decorated property is missing in the input.
📝 Syntax
intermediate
2:00remaining
Which DTO class syntax is correct for optional properties in NestJS?
You want to create a DTO class where the age property is optional. Which code snippet correctly defines this?
A
export class PersonDto {
  @IsOptional()
  @IsNumber()
  age?: number;
}
B
export class PersonDto {
  @IsNumber()
  age?: number;
}
C
export class PersonDto {
  @IsOptional()
  age: number;
}
D
export class PersonDto {
  age: number | undefined;
}
Attempts:
2 left
💡 Hint
Optional properties need a special decorator to skip validation if missing.
🔧 Debug
advanced
2:00remaining
Why does this DTO class cause a runtime error in NestJS?
Look at this DTO class code. Why does it cause a runtime error when used in a controller?

export class ProductDto {
  name: string;
  price: number;

  constructor() {
    this.name = '';
  }
}
ANestJS DTO classes should not have constructors; this causes runtime errors.
BThe constructor does not initialize the price property, causing an error.
CMissing decorators on properties cause runtime errors.
DThe constructor overwrites the name property, causing validation to fail.
Attempts:
2 left
💡 Hint
Think about how NestJS creates instances of DTO classes and how constructors affect this.
state_output
advanced
2:00remaining
What is the value of the isActive property after validation?
Given this DTO class and input object, what will be the value of isActive after validation?

import { IsBoolean, IsOptional } from 'class-validator';

export class StatusDto {
  @IsBoolean()
  @IsOptional()
  isActive?: boolean = true;
}

const input = {};
Atrue, because the default value is used when property is missing.
Bfalse, because missing boolean defaults to false.
CValidation fails because isActive is missing.
Dundefined, because the input does not provide isActive.
Attempts:
2 left
💡 Hint
Default values in DTO classes do not apply automatically during validation.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of DTO classes in NestJS?
Choose the most accurate description of what DTO classes do in NestJS applications.
ADTO classes are used to store data permanently in the database.
BDTO classes automatically generate API documentation without extra tools.
CDTO classes define the shape of data and enable validation and transformation before it reaches business logic.
DDTO classes replace entity classes and contain all business logic.
Attempts:
2 left
💡 Hint
Think about how data flows from outside requests into your app.