-
Notifications
You must be signed in to change notification settings - Fork 502
PartialType
can lead to 500 Internal Server Error
#2623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
PartialType
can cause 500 Internal Server ErrorPartialType
can lead to 500 Internal Server Error
I think that's something we should rather report in the class-validator repository |
Hi @kamilmysliwiec If they would fix it, they would probably do it in a way that would require people to pass an optional parameter, or use another decorator to avoid a significant breaking change. Because of that, it would probably require NestJS to adjust the code slightly anyways. Under the hood,
We could fix the problem on the NestJS side by using
|
Would you like to create a PR for this issue? |
Hi @kamilmysliwiec. I'd be happy to put up a PR for this, since my team also would like this functionality. It looks like It might be safest to make the new behaviour opt-in, so nothing breaks for people who are relying on the current behaviour. So perhaps If that sounds OK, I'll get started. Or let me know if you have other ideas about this. |
Sounds great @tf3 Let's track this here nestjs/mapped-types#1274 |
For anyone still encountering issues with You can pass a second options argument to Example: export class UpdateUserDto extends PartialType(CreateUserDto, {
skipNullProperties: false,
}) {} With this, your validation decorators will also trigger on |
Is there an existing issue for this?
Current behavior
Let's say we have the following DTO:
and use it with POST requests. The
content
is a nullable column in my PostgreSQL database, buttitle
is required.I would like to create a separate DTO for PATCH requests where every property is optional. I could use
PartialType
for that.Under the hood,
PartialType
succesfully applies theIsOptional
decorator to bothcontent
andtitle
. Unfortunately, there is a significant issue with this approach. The most important thing about theIsOptional
decorator is that it allows both undefined and null values.typestack/class-validator#491
Because of the above, using
PartialType
withCreateArticleDto
does not cause 400 Bad Request when the user providesnull
fortitle
. The validation does not work as expected, and the application will attempt to create a new row in the database usingnull
as the value for thetitle
column, which is likely to cause 500 Internal Server Error.In practical terms, with my
UpdateArticleDto
, the users can't provide a number for thetitle
thanks to@IsString()
, but they can providenull
. This is because@IsOptional()
turns off the validation if the provided value equalsnull
.Minimum reproduction code
https://github.com/mwanago/nestjs-partial-type-issue
Steps to reproduce
article
instance in the databasenull
fortitle
The above results in 500 Internal Server Error
Expected behavior
Instead of 500 Internal Server Error, the
class-validator
should prevent the user from providing thenull
value fortitle
. It should still allow the user to skip thetitle
property, effectively setting it toundefined
.To achieve that, we can't use the
IsOptional
decorator.Package version
7.1.11
NestJS version
10.1.3
Node.js version
18.6.0
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: