Pipes
Microservice pipes work the same way as regular pipes. The only difference is that they should throw RpcException instead of HttpException. Unless a custom exception filter handles it, an HttpException thrown during message handling reaches the client as a generic Internal server error message.
Hint TheRpcExceptionclass is exposed from the@nestjs/microservicespackage.
Binding pipes#
The following example uses a manually instantiated method-scoped pipe. The exceptionFactory option makes ValidationPipe throw an RpcException instead of its default BadRequestException. As with HTTP-based applications, you can also use controller-scoped pipes (i.e., prefix the controller class with a @UsePipes() decorator).
@UsePipes(new ValidationPipe({ exceptionFactory: (errors) => new RpcException(errors) }))
@MessagePattern({ cmd: 'sum' })
accumulate(data: number[]): number {
return (data || []).reduce((a, b) => a + b);
}
@UsePipes(new ValidationPipe({ exceptionFactory: (errors) => new RpcException(errors) }))
@MessagePattern({ cmd: 'sum' })
accumulate(data) {
return (data || []).reduce((a, b) => a + b);
}
The @Payload() decorator also accepts a schema option, which lets StandardSchemaValidationPipe (exported from @nestjs/common) validate the payload against a Standard Schema (e.g., a Zod schema). Configure its exceptionFactory option in the same way, so that validation errors are thrown as RpcException:
@UsePipes(new StandardSchemaValidationPipe({ exceptionFactory: (issues) => new RpcException(issues) }))
@MessagePattern({ cmd: 'sum' })
accumulate(@Payload({ schema: z.array(z.number()) }) data: number[]): number {
return data.reduce((a, b) => a + b, 0);
}
@UsePipes(new StandardSchemaValidationPipe({ exceptionFactory: (issues) => new RpcException(issues) }))
@Bind(Payload({ schema: z.array(z.number()) }))
@MessagePattern({ cmd: 'sum' })
accumulate(data) {
return data.reduce((a, b) => a + b, 0);
}
Hint Global pipes registered on the main HTTP application don't apply to microservices connected to a hybrid application unless you set the inheritAppConfig option. See sharing configuration.

