Nestjs - Validating and Converting Query params

Viewed 704

I have defined a DTO to validate and convert incoming query params.

export class ScrollingDto {
    @ApiPropertyOptional({
        description: 'Number of items to skip',
        default: 0,
        nullable: true
    })
    @IsOptional()
    @Type(() => Number)
    @IsInt()
    skip?: number;

    @ApiPropertyOptional({
        description: 'Number of items to take',
        example: 20,
        nullable: true
    })
    @IsOptional()
    @Type(() => Number)
    @IsInt()
    take?: number;
}

And I've added the global validation pipe in main.ts.

app.useGlobalPipes(new ValidationPipe({ transform: true }));

But it returns Bad Request error - skip must be an integer number with this query:

/api/blog/search?skip=0

Controller

@Controller('blog')
export class BlogController {
    constructor() {}

    @Get('search')
    searchBlogs(@Query() query: ScrollingDto) {
        ...
    }
}

I am not sure what I did wrong here. Hope to get any help on this.

Update

The ScrollingDTO is imported from a third-party library which is installed as a node module. If I just bring the class inside my app, then it works.

0 Answers
Related