Bird
0
0

Why does the following code cause a validation error?

medium📝 Debug Q7 of 15
NestJS - Pipes
Why does the following code cause a validation error?
class ProductDto {  @IsString()  name: string;  @IsInt()  quantity: number;}

Input: { "name": "Pen", "quantity": "five" }
ValidationPipe options: { transform: true }
AThe name property is not a string
BThe string "five" cannot be converted to an integer
Ctransform: true disables validation
DValidationPipe does not support nested objects
Step-by-Step Solution
Solution:
  1. Step 1: Analyze input and DTO types

    The quantity property expects an integer, but input provides the string "five".
  2. Step 2: Understand transform behavior

    transform: true converts types when possible, but "five" cannot convert to a number, causing validation failure.
  3. Final Answer:

    The string "five" cannot be converted to an integer -> Option B
  4. Quick Check:

    Non-numeric strings fail integer validation even with transform [OK]
Quick Trick: transform converts only valid type strings, not words [OK]
Common Mistakes:
  • Assuming all strings convert to numbers
  • Thinking transform disables validation
  • Confusing property types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes