Class transformer not converting to array of numbers

Viewed 8155

I have a DTO in which I have a field which is an array of numbers. These ids are coming via API query parameters. I am using Class Transformer to transform these ids into an array of numbers. But I am getting an array of strings only. My DTO class is as below.

export class EvseGetQueryDto {
  ...
  ...

  @IsOptional()
  @IsArray()
  @IsNumber({}, {each: true})
  @ApiProperty({ type: [Number] })
  @Type(() => Number)
  locations?: number[];

  ...
  ...
}

My Controller code looks like this.

async GetAll(@Query() query: EvseGetQueryDto): Promise<EvseDto[]> {
    return await this.evseService.GetAll(query);
}

If I call my controller like this below, I am still getting ['1', '2'] in my locations field.

http://localhost:3000/evses?locations[]=1&locations[]=2

Can anyone please guide me?

1 Answers

Query parameters always come in as strings, as do URL parameters if you use those. The @Type(() => Number) is enough to modify this though, but you need to make sure you have transform: true set in your ValidationPipe.

import {  Type } from 'class-transformer';
import { IsArray, IsNumber } from 'class-validator';

export class NumbersQuery {
  @Type(() => Number)
  @IsArray()
  @IsNumber({}, {each: true})
  numbers: number[];
}
import { Controller, Get, Query, UsePipes, ValidationPipe } from '@nestjs/common';
import { AppService } from './app.service';
import { NumbersQuery } from './numbers';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(@Query() query: Record<string, unknown>): string {
    return this.appService.getHello(query);
  }

  @UsePipes(new ValidationPipe({ transform: true }))
  @Get('numbers')
  getNumbers(@Query() numbers: NumbersQuery) {
    console.log(numbers);
    return numbers.numbers;
  }
}
▶ curl http://localhost:3000/numbers/\?numbers\[\]\=1\&numbers\[\]\=2
[1,2]%  
[Nest] 76497   - 10/23/2020, 10:40:47 AM   [NestFactory] Starting Nest application...
[Nest] 76497   - 10/23/2020, 10:40:47 AM   [InstanceLoader] AppModule dependencies initialized +11ms
[Nest] 76497   - 10/23/2020, 10:40:47 AM   [RoutesResolver] AppController {}: +5ms
[Nest] 76497   - 10/23/2020, 10:40:47 AM   [RouterExplorer] Mapped {, GET} route +2ms
[Nest] 76497   - 10/23/2020, 10:40:47 AM   [RouterExplorer] Mapped {/numbers, GET} route +1ms
[Nest] 76497   - 10/23/2020, 10:40:47 AM   [NestApplication] Nest application successfully started +1ms
NumbersQuery { numbers: [ 1, 2 ] }
Related