@Query() does not transform to DTO
See original GitHub issueBug 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:
- Created 4 years ago
- Comments:11 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Use
@Type
on your properties.@Query
always return string type so we have to cast to desired type.Example
Whitelist must be set as true in order to cut additionals.
https://docs.nestjs.com/techniques/validation#stripping-properties