Search code examples
javascriptvalidationnestjs

Access multipe parameters with a custom pipe in Nest.js


I'm trying to write a custom validation pipe to validate a request with multiple parameters. And I want to validate one param with a custom pipe, but another param is also needed to be used in the validation, and I didn't find the way described in the document.

For example, this is my API, it requires a chainId and an address as input parameters. I need to validate that the address is valid, but I can't do this without chainId.

Here's my code, I wrote the pipe followed by the document about custom pipe examples:

Controller

@Put('/:chainId/tokens/:address')
@ApiOperation({
  summary: 'Create a token',
})
async create(
  @Param('chainId') chainId: number,
  @Param('address', new AddressValidationPipe()) address: string,
): Promise<Token> {
  return await this.tokensService.save(chainId, address);
}

Validation pipe

@Injectable()
export class AddressValidationPipe implements PipeTransform {
  async transform(address: string) {
    // chainId should be another param, but I don't know how to get it in this pipe
    const chainId = 4;

    if (!validator(chainId, address)) {
      throw new BadRequestException(
        'Address is not valid, or it is not on this chain',
      );
    }
    return address;
  }
}

Solution

  • After some research, I found that I can have these two params into this pipe in one time, and only validate the address.

    Validation Pipe

    @Injectable()
    export class AddressValidationPipe implements PipeTransform {
      constructor(private readonly configService: ConfigService) {}
      async transform(value: { chainId: string; address: string }) {
        const { chainId, address } = value;
    
        if (!validator(Number(chainId), address)) {
          throw new BadRequestException(
            'Address is not valid, or it is not on this chain',
          );
        }
    
        return { chainId: Number(chainId), address };
      }
    }
    

    Although in this way, I must convert the chainId to number by myself, and I need to tell Swagger what params I'm using because I'm not writing them as each argument in my controller.

    Controller

    @ApiParam({
      name: 'address',
      description: 'Address of the token',
      type: String,
    })
    @ApiParam({
      name: 'chainId',
      description: 'Chain ID of the token',
      type: Number,
    })
    @Put('/:chainId/tokens/:address')
    @ApiOperation({
      summary: 'Create a token',
    })
    async create(
      @Param(AddressValidationPipe) param: { chainId: number; address: string },
    ): Promise<Token> {
      const { chainId, address } = param;
      return await this.tokensService.save(chainId, address);
    }