Date
s objects are automatically transformed with the ValdiationPipe
and at least one validator option
#12647
Closed
3 of 15 tasks
Labels
needs triage
This issue has not been looked into
Is there an existing issue for this?
Current behavior
This was something interesting brought up in a Discord message.
When using the
ValidaitonPipe
, withtransform: false
and definingwhitelist: false
(or any other variant of) along with@Query('date') date: Date
, when accessingdate
in the route handler, it would come back as aDate
object, rather than the expectedstring
. When removing thewhitelist
option altogether, thedate
variable was correctly astring
.Minimum reproduction code
https://github.com/jmcdo29/nest-whitelist-date-validation
Steps to reproduce
pnpm i
(or whatever package manager you wantpnpm start:dev
curl http://localhost:3000?date=2023-10-24T:10:00:00
a. alternatively, run
pnpm test:e2e
a. or test failures
Expected behavior
I believe that we don't want to run validations on
Date
class properties if they're single values, like when accessed via@Query('date')
,@Param('date')
,@Body('date')
, etc. The fix for this is to simply addDate
to thetoValidate
array of class types that we don't immediately run validations for.Package
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
No response
NestJS version
10.2.7
Packages versions
Node.js version
20.8.0
In which operating systems have you tested?
Other
Happy to open the PR for this, just wanted to verify it before I did
The text was updated successfully, but these errors were encountered: