Validation fails with latest 0.14.0 version of class-validator
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Current behavior
Note This is an issue opened by a
class-validatiormaintainer to raise awareness of the latest breaking change in theclass-validator@0.14.0version and provide guidance on how to work around it.
The latest 0.14.0 release of class-validator changed the default option for forbidUnknownValues from false to true. The old behavior can be restored by specifying forbidUnknownValues: false when providing ValidatorOptions 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 metadata is not registered correctly
- when using group validation and the specified validation group results in zero validation applied
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:
class MyPayload {
@IsString({ groups: ['A']})
property: string;
constructor(property: string) {
this.property = property;
}
}
validate(new MyPayload('value'), { groups: ['B'] }).then(console.log);
Calling the above will result in an error from now on:
[
ValidationError {
target: MyPayload { property: 'value' },
value: undefined,
property: undefined,
children: [],
constraints: {
unknownValue: 'an unknown value was passed to the validate function'
}
}
]
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
- update
class-validatorto the latest 0.14.0 version - use group validation or incorrectly configure your decorator metadata
- observe the failing validations which were passing in the previous versions
Expected behavior
The affected code is updated by the affected developers.
Package
- I don’t know. Or some 3rd-party 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 (see below)
Other package
class-validator
NestJS version
9.2.1
Packages versions
Node.js version
v18.12.1
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
Issue Analytics
- State:
- Created 9 months ago
- Reactions:3
- Comments:6 (1 by maintainers)
Top Related StackOverflow Question
Example:
Suggestions for developers running into this error:
class-validator < 0.14{forbidUnknownValues: false}, e.g.app.useGlobalPipes(new ValidationPipe({forbidUnknownValues: false}));The following documentation may need to be updated:
@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.