question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@Query() does not transform to DTO

See original GitHub issue

Bug Report

Current behavior

I have a controller that needs to recive data from the request query and I want to put that data into a DTO using @Query() but it does not properly transform the DTO to the target interface

Input Code

import { Get, Controller, Query, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common';

interface TestDto {
  field1: number;
  field2: boolean;
}

@Controller()
export class AppController {
  constructor() {}

  @Get()
  @UsePipes(new ValidationPipe({ whitelist: false, transform: true}))
  root(@Query() dto: TestDto): TestDto {
    return dto;
  }

}

Expected behavior

If I call http://localhost:3000/?field1=10&field2=true&field3=test I get

{
"field1": "10",
"field2": "true",
"field3": "test"
}

But the expected result is:

{
"field1":  10,
"field2": true
}

Environment


Nest version: 5.7.4
Node version: 10.9.0
class-transformer: 0.2.3
class-validator: 0.10.1
tsc version: 1.8.10
Platform: Ubuntu 18.10

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
Hwan-seokcommented, Feb 24, 2021

Use @Type on your properties. @Query always return string type so we have to cast to desired type.

Example

import { Type } from 'class-transformer';

export class TestDto {
  @IsNumber()
  @Type(() => Number)
  field1: number;

  @IsBoolean()
  @Type(() => Boolean)
  field2: boolean;
}
7reactions
wintertime-inccommented, Oct 11, 2019

Whitelist must be set as true in order to cut additionals.

https://docs.nestjs.com/techniques/validation#stripping-properties

Read more comments on GitHub >

github_iconTop Results From Across the Web

nestjs - @Query() does not transform to DTO - Stack Overflow
I wrote a DTO map query params to an object and I'm using a ValidationPipe to validate and transform the data to my...
Read more >
Transform and Validate Query Parameters in NestJS
In today's article, I want to show you how to transform and validate HTTP request query parameters in NestJS. As you might know,...
Read more >
Nest JS Validate Query Paramater and DTOs – @tkssharma
I want to show you how to transform and validate HTTP request query parameters in NestJS. As you might know, a query parameter...
Read more >
Query Helpers | Nestjs-query - Blog
import { applyFilter } from `@nestjs-query/core`; const dtos: TestDTO[] ... method it may not exactly match the ordering you would expect from a...
Read more >
API payloads validation and transformation in NestJS - Medium
Output procedures ensure that the data we expose as output does not contain ... serialization/deserialization and validation with DTOs is easier as well....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found