Bird
0
0

How can you enforce that a query parameter active is both required and a boolean using NestJS pipes?

hard📝 Application Q9 of 15
NestJS - Pipes
How can you enforce that a query parameter active is both required and a boolean using NestJS pipes?
AUse <code>@Query('active') active: string</code> and manually check in method
BUse <code>@Query('active', ParseIntPipe) active: boolean</code>
CUse <code>@Query('active', ParseBoolPipe) active?: boolean</code> without validation
DUse <code>@Query('active', new ParseBoolPipe({ exceptionFactory: () => new BadRequestException('active is required') })) active: boolean</code>
Step-by-Step Solution
Solution:
  1. Step 1: Use ParseBoolPipe to convert to boolean

    This pipe converts string 'true'/'false' to boolean.
  2. Step 2: Make parameter required with custom exception

    By providing exceptionFactory, you can throw a custom error if the parameter is missing or invalid.
  3. Final Answer:

    Use new ParseBoolPipe({ exceptionFactory: () => new BadRequestException('active is required') }) to enforce required boolean -> Option D
  4. Quick Check:

    Custom exception enforces required boolean query param [OK]
Quick Trick: Use ParseBoolPipe with exceptionFactory for required booleans [OK]
Common Mistakes:
  • Using ParseIntPipe for boolean parameters
  • Not handling missing parameters explicitly
  • Relying on optional parameters without validation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes