Validation by class-validator or class-transformer dont work when apply multiple Types on @Query docrator

Viewed 329

I want to combine multiple types at @Query() decorator (e.g. ParamsWithRegex and PaginationParams) , but when I do that no validation applied , how to fix that ?


 **// MY CONTROLLER : here I combine two types but validate didnt work**
 @Get()
  async findAll(
    @Query() query: PaginationParams & ParamsWithRegex,
  ) {
    return 'OK'
  }



// ParamsWithRegex.dto.ts
export class ParamsWithRegex {
  @IsOptional()
  @Transform(({ obj }) => {
    return new RegExp(escapeRegExp(obj.name), 'i');
  })
  name?: string;
}





// paginationParams.dto.ts 
export class PaginationParams {
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  limit?: number = 8;
}

1 Answers

You could use Mapped-Types to create your Dto:

@Get()
async findAll(@Query() query: CombinedDto) {
  ...
}
export class CombinedDto extends IntersectionType(
  ParamsWithRegex,
  PaginationParams
) {}
Related