0

I am currently developing application using nestjs with fastify adapter

But something weird on object construction.

Following all the related classes, and methods:

  1. Controller endpoint handler
    @Get()
    @ApiOperation({
        description: "Get all user admin",
    })
    async findAll(
        @Query() filter: GetListAdminReqFilter,
        @Query() pagination: PaginatedReqDto
    ): Promise<RestRespDto<GetListAdminRespDto[]>> {
        return new RestRespDto({
            data: await this.adminService.findAll(
                new GetListAdminReqDto(filter, pagination)
            ),
        });
    }
  1. The request dto
export class GetListAdminReqDto extends PaginatedReqDto {
    constructor(filter: GetListAdminReqFilter, pagination: PaginatedReqDto) {
        super();
        this.filter = filter;
        this.pagination = pagination.pagination;
        this.page = pagination.page;
    }
    filter?: GetListAdminReqFilter;
}
  1. The pagination req dto
export class PaginatedReqDto {
    @ApiPropertyOptional({
        default: 10,
        description: "Number of items to retrieve",
    })
    pagination?: number;
    @ApiPropertyOptional({
        description: "Page number, e.g:1 ",
        default: 1,
    })
    page?: number;
}
  1. The filter
export class GetListAdminReqFilter {
    @ApiPropertyOptional()
    @IsOptional()
    name?: string;

    @ApiPropertyOptional()
    @IsOptional()
    email?: string;

    @ApiPropertyOptional()
    @IsOptional()
    divisi?: string;

    @ApiPropertyOptional({ enum: AdminStatusEnum})
    @IsOptional()
    status?: AdminStatusEnum;
}

The result of GetListAdminReqDto object is following:

{
  filter: [Object: null prototype] {
    pagination: '10',
    page: '1',
    name: 'asdfasdf',
    email: 'asdfasdf',
    divisi: 'asdfasdf'
  },
  pagination: '10',
  page: '1'
}

Why pagination and page is property also included in filter?, i dont understand what happened, any help will be appreciated

rizesky
  • 424
  • 6
  • 13

1 Answers1

0

My guess is that the @Query() filter: GetListAdminReqFilter is receiving the page and pagination properties because your ValidationPipe is allowing properties outside of the DTO. You could alter the global ValidationPipe.whitelist property:

  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true, // this prevents properties outside the DTO to be ignored
    }),
  );
TPoschel
  • 3,775
  • 2
  • 30
  • 29