Bird
0
0

You want to apply a custom pipe that trims whitespace from all string inputs in a controller and also validate numbers globally. Which binding strategy is best to achieve this cleanly?

hard📝 Application Q15 of 15
NestJS - Pipes
You want to apply a custom pipe that trims whitespace from all string inputs in a controller and also validate numbers globally. Which binding strategy is best to achieve this cleanly?
ABind the trim pipe at controller level and the number validation pipe globally
BBind both pipes globally to affect all controllers and methods
CBind both pipes only on method parameters where needed
DBind the trim pipe globally and the number validation pipe at method level
Step-by-Step Solution
Solution:
  1. Step 1: Understand scope of pipes for separation of concerns

    Trimming strings is specific to this controller's inputs, so controller-level binding keeps it scoped and clean.
  2. Step 2: Apply number validation globally

    Number validation is common across the app, so global binding ensures consistent validation everywhere.
  3. Final Answer:

    Bind the trim pipe at controller level and the number validation pipe globally -> Option A
  4. Quick Check:

    Controller pipe for specific, global pipe for common validation [OK]
Quick Trick: Use controller pipes for specific, global pipes for common tasks [OK]
Common Mistakes:
  • Binding all pipes globally causing unnecessary processing
  • Binding all pipes only on parameters causing repetition
  • Mixing global and method-level without clear reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes