#Can I use ValidationPipe outside of the controller?

3 messages · Page 1 of 1 (latest)

simple monolith
#

example:

import { IsNumber, IsString } from 'class-validator';
import { UsePipes, ValidationPipe } from '@nestjs/common';

export class Params {
  @IsNumber()
  min!: string;
  @IsNumber()
  max!: number;
  @IsString()
  stat!: string;
}

export class A {
  @UsePipes(new ValidationPipe())
  validate(args: Params): Promise<boolean> {
    console.log('A args', args);
    return Promise.resolve(true);
  }
}
coral ore
#

No. Since Nest has no control over which functions you call directly from your code, it can only intercept calls before it hands over control to your code - that is, between receiving the request and calling your controller method.