-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Validation fails with latest 0.14.0 version of class-validator #10683
Comments
This issue may be pinned for the next few days. |
@NoNameProvided It would be helpful if you could provide a few examples of this case where you say metadata is not registered correctly. |
Please provide a minimum reproduction repository. why reproductions are required |
If in the previous version you saw validation errors, then 99% chance that your setup works correctly. A possible scenario is when you use a different package to define the payload classes used for validation and you use inconsistent
In this setup, the decorators on your classes register the metadata in version This may lead to situations when validation is silently passed without any actual validation taking place. |
@micalevisk I am not sure what reproduction case do you asking for. This is an issue opened to raise awareness of a breaking change. I myself did not run into any problems with NestJs so I have no reproduction case to provide. |
Example: /**
* This post body is completely undecorated.
*/
class MyInputModel {
foo: string;
}
@Controller('/test')
export class MyTestController {
@Post()
async postTest(@Body() body: MyInputModel) {
return; // expect HTTP 201, receive HTTP 400
}
} Suggestions for developers running into this error:
The following documentation may need to be updated:
|
See GHSA-fj58-h2fr-3pp2. This should be fixed ASAP |
@NoNameProvided any tips on the Any tips? |
@scopsy my team ran into a similar issue in our monorepo - we ended up tracking it down to what we think is an issue in class-transformer that I just opened typestack/class-transformer#1440 to address. FYI @NoNameProvided typestack/class-transformer#1043 gives a really good description of the problem with sharing validators within a monorepo. We ended up using a very similar workaround as the people from that thread:
|
Npm is showing errors when Could not resolve dependency: |
See this comment from jmcdo nestjs/mapped-types#949 (comment) |
Since this caused quite an important regression (described here #10683 (comment)) I'm going to create a PR shortly that will set |
That sounds like a good solution. |
According to info here: nestjs/nest#10683
I've having the same issue with TIA |
Is there an existing issue for this?
Current behavior
The latest
0.14.0
release ofclass-validator
changed the default option forforbidUnknownValues
fromfalse
totrue
. The old behavior can be restored by specifyingforbidUnknownValues: false
when providingValidatorOptions
to NestJS.This means validation of class instances where no actual validation takes place will fail instead of silently passing. This is the expected behavior for the majority of use cases.
There are two scenarios when this can happen:
The first case will be a misconfiguration in your project 99% of the time, and you probably never want to allow payloads to bypass validation due to missing metadata. In this case, you need to find out why no metadata is present to do validations.
The second case may be a legit use case when you want to support validation groups that are exclusive to each other. If the called validation excludes all metadata due to the specified group then the validation will fail from now on instead of passing. Example:
Calling the above will result in an error from now on:
If this is your use case you need to restore
forbidUnknownValues: false
or re-think your approach to how you are using groups.For more details see PR #1798 and #1422 (comment).
Minimum reproduction code
https://github.com/typestack/class-validator/blob/develop/CHANGELOG.md#0140-2022-12-09
Steps to reproduce
class-validator
to the latest 0.14.0 versionExpected behavior
The affected code is updated by the affected developers.
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
class-validator
NestJS version
9.2.1
Packages versions
Node.js version
v18.12.1
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: