0
0
NestJSframework~10 mins

Why DTOs enforce data contracts in NestJS - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a DTO class property with a type.

NestJS
export class CreateUserDto {
  username: [1];
}
Drag options to blanks, or click blank then click option'
Aany
Bnumber
Cboolean
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using number or boolean for username type.
Using any defeats the purpose of enforcing a data contract.
2fill in blank
medium

Complete the code to add validation decorator to a DTO property.

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

export class CreateUserDto {
  @[1]()
  username: string;
}
Drag options to blanks, or click blank then click option'
AIsString
BIsDate
CIsBoolean
DIsNumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using @IsNumber() or other decorators that don't match the property type.
Forgetting to add any validation decorator.
3fill in blank
hard

Fix the error in the DTO by completing the missing decorator to enforce required property.

NestJS
import { IsString, [1] } from 'class-validator';

export class CreateUserDto {
  @IsString()
  @[1]()
  username: string;
}
Drag options to blanks, or click blank then click option'
AIsOptional
BIsEmail
CIsNotEmpty
DIsNumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using @IsOptional() which allows the property to be missing.
Using unrelated decorators like @IsEmail() or @IsNumber().
4fill in blank
hard

Fill both blanks to create a DTO property with type and validation decorator.

NestJS
import { [1] } from 'class-validator';

export class UpdateUserDto {
  @[2]()
  email: string;
}
Drag options to blanks, or click blank then click option'
AIsEmail
BIsString
CIsNotEmpty
DIsBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Importing or using @IsString() instead of @IsEmail().
Using unrelated decorators like @IsBoolean().
5fill in blank
hard

Fill both blanks to define a DTO with multiple validated properties.

NestJS
import { [1], [2] } from 'class-validator';

export class RegisterUserDto {
  @[1]()
  username: string;

  @[2]()
  age: number;
}
Drag options to blanks, or click blank then click option'
AIsString
BIsInt
CIsNotEmpty
DIsBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up decorators and imports.
Using IsNotEmpty or IsBoolean incorrectly.