Pipe scoping in NestJS defines where a pipe is applied within the request processing hierarchy. Pipes can be scoped at four levels:
- Parameter-scoped: Bound directly to a parameter decorator —
@Body(new ValidationPipe()) - Method-scoped: Applied to a single route handler —
@UsePipes(new ZodValidationPipe(schema)) - Controller-scoped: Applied to all handlers in a controller —
@UsePipes(new ValidationPipe())on the controller class - Global-scoped: Applied to all routes —
app.useGlobalPipes(new ValidationPipe())or viaAPP_PIPEtoken for DI support
The APP_PIPE token approach (registered in module providers) is preferred for global pipes because it enables dependency injection, unlike useGlobalPipes() which binds outside module context.