NestJS Logo

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 The RpcException class is exposed from the @nestjs/microservices package.

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).

JS TS

@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:

JS TS

@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.
Edit on GitHub

Support us

Nest is an MIT-licensed open source project. It can grow thanks to the support of these awesome people. If you'd like to join them, please read more here.

Principal Sponsors

SerpApi LogoTrilon LogoMojam Logo

Sponsors / Partners

Become a sponsor