10

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?

2 Answers 2

37

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 ] }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Jay! I found the issue after you explained this stuff. I was missing a validation Pipe in my controller.
5

if you don't like your search queries to look like

/evses?locations[]=1&locations[]=2

and want something like passing an entire list

/evses?locations=1,1

use inside your class

export class yourclass {


    @Transform(({ value }) => value.toString().split(',').map(Number))
    locations:Number[];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.