I have a controller that needs to receive data in the request query string (I can't use the body because I'm interacting with a legacy system).
I wrote a DTO map query params to an object and I'm using a ValidationPipe to validate and transform the data to my DTO.
So, I have this:
import { Get, Controller, Query, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common';
class TestDto {
@IsNumber()
field1: number;
@IsBoolean()
field2: boolean;
}
@Controller()
export class AppController {
constructor() {}
@Get()
@UsePipes(new ValidationPipe({ whitelist: false, transform: true}))
root(@Query() dto: TestDto): TestDto {
return dto;
}
}
All of the previous code compìles and follows the NestJS documentation, but when I call http://localhost:3000/?field1=15&field2=true I get this:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"field1": "15",
"field2": "true"
},
"value": "15",
"property": "field1",
"children": [],
"constraints": {
"isNumber": "field1 must be a number"
}
},
{
"target": {
"field1": "15",
"field2": "true"
},
"value": "true",
"property": "field2",
"children": [],
"constraints": {
"isBoolean": "field2 must be a boolean value"
}
}
]
}
Both fields are valid according to the attributes but the pipe rejects the request. If I change from @IsNumber to @IsNumberString and from @IsBoolean to @IsBooleanString it validates, but I do not received the transformed data (i.e. I get a plain object instead of my DTO)
Did anybody face something like this?