Reputation: 613
I want to transform my query param from string to number. I use dto technic.
import { IsOptional, IsInt, Min } from 'class-validator';
import { Transform } from 'class-transformer';
export class PaginationDto {
@IsOptional()
@IsInt()
@Transform(val => Number.parseInt(val))
@Min(1)
perPage: number;
Use dto in controller
@Get('/company')
public async getCompanyNews(
@Query() query: PaginationDto
) {
console.log(typeof query.page);
Result: string.
How do I change the type correctly?
Upvotes: 0
Views: 1515
Reputation: 70111
To ensure that DTOs get transformed, the transform: true
option must be set for the ValidationPipe
. Without that, the original incoming object will be passed after going through validations.
Upvotes: 2