Bird
0
0

Given this pipe usage in a controller method:

medium📝 component behavior Q4 of 15
NestJS - Pipes
Given this pipe usage in a controller method:
@UsePipes(new ValidationPipe())
async create(@Body() data: CreateDto) { return data; }

What happens if the input data is missing a required field defined in CreateDto?
AThe method receives the data with missing fields as undefined
BThe request is rejected with a validation error before reaching the method
CThe method throws an error when accessing missing fields
DThe missing fields are automatically filled with default values
Step-by-Step Solution
Solution:
  1. Step 1: Understand ValidationPipe behavior

    ValidationPipe checks input against DTO rules and rejects invalid data before method execution.
  2. Step 2: Analyze what happens with missing required fields

    Missing required fields cause validation failure, so the request is rejected early.
  3. Final Answer:

    The request is rejected with a validation error before reaching the method -> Option B
  4. Quick Check:

    ValidationPipe rejects invalid input early [OK]
Quick Trick: ValidationPipe stops invalid data before controller runs [OK]
Common Mistakes:
  • Assuming method runs with incomplete data
  • Thinking missing fields get default values automatically
  • Confusing validation errors with runtime exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes