CodePark/ questions / Pipes, DTOs & validation
START PRACTICINGPRACTICE
Backend question · NestJS › Pipes, DTOs & validation

You register ValidationPipe with app.useGlobalPipes(new ValidationPipe()) in main.ts. Which statement is true?

It is set up outside any module, so it can't use DI; registering it with the APP_PIPE token in a module allows injection

A pipe registered with useGlobalPipes() is created outside any module, so it can't use DI. Nest can't inject dependencies into it. To fix this, provide { provide: APP_PIPE, useClass: ValidationPipe } in a module. The pipe is then global and can use DI.

Why the other options are wrong:

• "The pipe can inject any provider through its constructor" is false, for the reason above.

• "It applies only to controllers in the root
AppModule" is false. Global pipes apply to every route.
• "It runs before guards for every request" is false. Pipes always run after guards, and after the before-logic of interceptors.