05 / 07

How do you use a custom param decorator together with a pipe for automatic validation in NestJS?

Custom param decorators accept pipes as arguments. NestJS passes the decorator's resolved value through the pipe before injecting it into the handler. This lets you extract a value from the request and validate or transform it in a single decorator call.

@TenantId() decorator combined with a validation pipe
Decorator + pipe composition rules:
  1. 1

    Pass a pipe instance as an argument to the custom decorator: @TenantId(new MyPipe()).

  2. 2

    NestJS resolves the decorator value first, then passes it through the pipe.

  3. 3

    The pipe can be async — NestJS awaits it before injecting the final value.

  4. 4

    Multiple pipes can be chained: @TenantId(new ParsePipe(), new ValidatePipe()).

  5. 5

    The pipe receives the raw decorator output — design both to work together.