Bird
Raised Fist0
NestJSframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of @IsEmail() decorator in class-validator when used in a NestJS DTO class?
easy
A. To convert the property value to lowercase
B. To check if the property value is a valid email address format
C. To make the property optional
D. To trim whitespace from the property value

Solution

  1. Step 1: Understand the decorator's role

    The @IsEmail() decorator validates that the decorated property contains a valid email format string.
  2. Step 2: Compare options with decorator behavior

    Only To check if the property value is a valid email address format matches the validation purpose of @IsEmail(). Other options describe unrelated behaviors.
  3. Final Answer:

    To check if the property value is a valid email address format -> Option B
  4. Quick Check:

    @IsEmail() validates email format [OK]
Hint: Remember: @IsEmail() checks email format only [OK]
Common Mistakes:
  • Thinking @IsEmail() modifies the value instead of validating
  • Confusing @IsEmail() with @IsOptional() or @Transform decorators
2. Which of the following is the correct way to use @Length() decorator to require a string property to be between 5 and 10 characters in NestJS?
easy
A. @Length(5, 10)
B. @Length(10, 5)
C. @Length(min=5, max=10)
D. @Length({min:5, max:10})

Solution

  1. Step 1: Recall the syntax of @Length decorator

    The @Length() decorator takes two numeric arguments: minimum length first, then maximum length.
  2. Step 2: Match options with correct syntax

    @Length(5, 10) correctly uses @Length(5, 10). Options A, B, and D use incorrect argument order or syntax.
  3. Final Answer:

    @Length(5, 10) -> Option A
  4. Quick Check:

    @Length(min, max) = @Length(5, 10) [OK]
Hint: @Length(min, max) uses two numbers, min first [OK]
Common Mistakes:
  • Swapping min and max values
  • Using object syntax instead of positional arguments
  • Using named parameters which are invalid
3. Given this DTO class, what will be the validation result for username = 'abc' and age = 17?
class UserDto {
  @Length(5, 15)
  username: string;

  @Min(18)
  age: number;
}
medium
A. Validation passes with no errors
B. Validation fails because username is too short
C. Validation fails because age is less than 18
D. Validation fails because both username and age are invalid

Solution

  1. Step 1: Check username length

    The username 'abc' has length 3, which is less than the minimum 5 required by @Length(5, 15). So username is invalid.
  2. Step 2: Check age minimum

    The age is 17, which is less than the minimum 18 required by @Min(18). So age is invalid.
  3. Final Answer:

    Validation fails because both username and age are invalid -> Option D
  4. Quick Check:

    username too short + age too low = both invalid [OK]
Hint: Check each decorator rule separately for all properties [OK]
Common Mistakes:
  • Ignoring one invalid field and assuming validation passes
  • Confusing @Min with @Max for age
  • Assuming length includes spaces or trimming automatically
4. Identify the error in this DTO class using class-validator decorators:
class ProductDto {
  @IsInt()
  @Min(1)
  price: string;
}
medium
A. price should be a number type, not string
B. Missing @IsString() decorator for price
C. @Min(1) cannot be used with @IsInt()
D. No error, code is correct

Solution

  1. Step 1: Analyze property type and decorators

    The property price is declared as string, but decorators @IsInt() and @Min(1) expect a number type.
  2. Step 2: Identify mismatch causing validation error

    Since price is string, validation will fail because @IsInt() requires a number. The type should be number.
  3. Final Answer:

    price should be a number type, not string -> Option A
  4. Quick Check:

    Type mismatch between property and decorators = error [OK]
Hint: Match property type with decorator expectations [OK]
Common Mistakes:
  • Using string type with number validators
  • Assuming decorators convert types automatically
  • Ignoring TypeScript type declarations
5. You want to validate a DTO property tags which is an array of strings, each string must be at least 3 characters. Which combination of class-validator decorators correctly enforces this?
hard
A. @IsString()\n@Length(3, 50)\n@IsArray()
B. @IsArray()\n@IsString()\n@Length(3, 50)
C. @IsArray()\n@ArrayMinSize(1)\n@IsString({ each: true })\n@Length(3, 50, { each: true })
D. @IsArray()\n@ArrayMaxSize(1)\n@IsString({ each: false })

Solution

  1. Step 1: Validate the property is an array

    Use @IsArray() to ensure tags is an array type.
  2. Step 2: Validate each element is a string with length constraints

    Use @IsString({ each: true }) and @Length(3, 50, { each: true }) to apply string and length validation to every item in the array.
  3. Step 3: Ensure array has at least one element

    @ArrayMinSize(1) ensures the array is not empty.
  4. Final Answer:

    @IsArray()\n@ArrayMinSize(1)\n@IsString({ each: true })\n@Length(3, 50, { each: true }) -> Option C
  5. Quick Check:

    Array + each:true for element validation = @IsArray()\n@ArrayMinSize(1)\n@IsString({ each: true })\n@Length(3, 50, { each: true }) [OK]
Hint: Use { each: true } to validate every array element [OK]
Common Mistakes:
  • Forgetting { each: true } for array element validation
  • Using @IsString() without array context
  • Setting wrong array size constraints
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard