简体   繁体   中英

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

I want to combine multiple types at @Query() decorator (eg 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;
}

You could use Mapped-Types to create your Dto:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM