NestJS ValidationPipe leaves body numbers as strings
@Param('id') id arrives as the string '5' although typed numberShort answer
ValidationPipe only transforms what it is told to run on. Check that the pipe is registered with transform: true, that the handler parameter is typed with a DTO class (not an interface or inline type), and remember that path and query values arrive as strings. Convert primitives with a typed parameter or ParseIntPipe.
Fix it fast
1Find out which cause you have
Print the type of the value inside the handler. It shows at once whether you got a plain object or a DTO instance.
console.log(dto.constructor.name, typeof dto.age);2Match what you see, then jump to its fix
What does the error look like?
@Param('id') id arrives as the string '5' although typed numberThe DTO parameter is a plain Object, not an instance of the DTO class
Cause 1 of 3
The pipe is not registered, or transform is off
1Check
The docs say payloads arrive as plain objects, and transform: true makes the pipe turn them into DTO class instances. Find where ValidationPipe is created.
grep -rn "ValidationPipe" src/2Fix
Enable it globally in main.ts, or for one method with UsePipes.
app.useGlobalPipes(new ValidationPipe({ transform: true }));3Verify
Send a request and log the constructor name again; it should be the DTO class.
curl -s -X POST localhost:3000/items -H 'Content-Type: application/json' -d '{"age":5}'Confirmed by docs.nestjs.com
Cause 2 of 3
A path or query value is still a string
1Check
The docs state path and query parameters always arrive as strings. With transform on, the pipe converts them from the declared type; with it off you must convert them yourself.
grep -rn "@Param\|@Query" src/2Fix
Declare the parameter type with transform enabled, or use ParseIntPipe explicitly.
@Get(':id')
find(@Param('id', ParseIntPipe) id: number) {
return this.service.find(id);
}3Verify
Request /items/5 and check typeof id is 'number'.
curl -s localhost:3000/items/5Confirmed by docs.nestjs.com
Cause 3 of 3
The body parameter is typed with an interface, not a class
1Check
Interfaces are erased when TypeScript compiles, so the pipe sees the type as Object and skips it. Only a class gives it something to convert to.
grep -rn "@Body()" src/2Fix
Declare the DTO as a class with class-validator decorators and use it as the parameter type.
export class CreateItemDto {
@IsInt()
age: number;
}
@Post()
create(@Body() dto: CreateItemDto) {}3Verify
Post a bad value and expect a 400 response from the pipe.
curl -s -X POST localhost:3000/items -H 'Content-Type: application/json' -d '{"age":"abc"}'Confirmed by docs.nestjs.com
How do I stop it coming back?
- Register ValidationPipe once, globally, so every route behaves the same.
- Type handler parameters with DTO classes; interfaces are erased at runtime and cannot be used.
- Add one request-level test per DTO that checks types after the pipe.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Node.js in Docker Compose cannot reach MySQL on 127.0.0.1:3306
Error: connect ECONNREFUSED 127.0.0.1:3306
Inside a container, 127.0.0.1 is that container, not MySQL.
3 causes, each with a fixBack end
NestJS GraphQL cannot work out the output type of a field or query
Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.
In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.
2 causes, each with a fixBack end
IntelliJ IDEA breakpoints are ignored in a NestJS app
No 'Debugger listening' line appears when the NestJS app starts
IntelliJ can only debug a NestJS app when the Node.js process runs with the inspector on and the IDE attaches to its port.
4 causes, each with a fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fixBack end
NestJS watch mode does not restart after I save a file
Changes show on the host but not in the running container
Watch mode only sees files that exist inside the process that runs it.
3 causes, each with a fix