0
0
NestJSframework~20 mins

class-validator decorators in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
class-validator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a property with @IsEmail() decorator receives invalid input?

Consider a NestJS DTO class with a property decorated by @IsEmail(). What will the validation result be if the property value is "not-an-email"?

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

class UserDto {
  @IsEmail()
  email: string;
}

const user = new UserDto();
user.email = "not-an-email";
// Assume validation is run here
AValidation fails with an error indicating the email is invalid.
BValidation passes because the value is a string.
CValidation throws a runtime error due to invalid decorator usage.
DValidation ignores the property and passes.
Attempts:
2 left
💡 Hint

Think about what @IsEmail() checks for in the input.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses @Length decorator to enforce string length between 5 and 10?

Choose the correct way to use the @Length decorator from class-validator to ensure a string property has length between 5 and 10 characters.

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

class ProductDto {
  @Length(?, ?)
  name: string;
}
A@Length(10, 5)
B@Length(5, 10)
C@Length(min=5, max=10)
D@Length({ min: 5, max: 10 })
Attempts:
2 left
💡 Hint

Check the official @Length decorator signature.

🔧 Debug
advanced
2:00remaining
Why does validation not fail when @IsNotEmpty() is used on a number property set to 0?

Given this DTO:

class ScoreDto {
  @IsNotEmpty()
  score: number;
}

const dto = new ScoreDto();
dto.score = 0;
// Validation is run here

Why does validation pass even though 0 might be considered empty?

AValidation fails but the error is swallowed silently.
B@IsNotEmpty() only works on strings, so it ignores numbers.
CThe decorator is missing a required option to check for zero.
D@IsNotEmpty() considers 0 a valid non-empty value because it checks for null or undefined, not falsy values.
Attempts:
2 left
💡 Hint

Think about what @IsNotEmpty() actually checks internally.

state_output
advanced
2:00remaining
What is the validation error message for a missing required property with @IsDefined()?

Consider this DTO:

class CreateUserDto {
  @IsDefined()
  username: string;
}

const dto = new CreateUserDto();
// username is not set
// Validation is run here

What error message will the validator produce?

Ausername must be defined
Busername must be a string
Cusername should not be null or undefined
Dusername should not be empty
Attempts:
2 left
💡 Hint

Look at the default message for @IsDefined().

🧠 Conceptual
expert
3:00remaining
Which decorator combination ensures a property is a non-empty array of strings with at least 2 items?

Choose the correct set of class-validator decorators to enforce that a property is:

  • An array
  • Contains only strings
  • Has at least 2 items
  • Is not empty
NestJS
class TagsDto {
  // decorators here
  tags: string[];
}
A@IsArray() @ArrayMinSize(2) @IsString({ each: true }) @IsNotEmpty()
B@IsArray() @ArrayMinSize(2) @IsString() @IsNotEmpty()
C@IsArray() @ArrayMinSize(2) @IsString({ each: true })
D@IsArray() @ArrayMinSize(2) @IsNotEmpty({ each: true }) @IsString()
Attempts:
2 left
💡 Hint

Remember to validate each item in the array and the array itself.