Validation fails with latest 0.14.0 version of class-validator

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Note This is an issue opened by a class-validatior maintainer to raise awareness of the latest breaking change in the class-validator@0.14.0 version 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

  1. update class-validator to the latest 0.14.0 version
  2. use group validation or incorrectly configure your decorator metadata
  3. observe the failing validations which were passing in the previous versions

Expected behavior

The affected code is updated by the affected developers.

Package

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:open
  • Created 9 months ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
mschneecommented, Dec 13, 2022

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:

  • Pin to class-validator < 0.14
  • Use the option {forbidUnknownValues: false}, e.g. app.useGlobalPipes(new ValidationPipe({forbidUnknownValues: false}));
  • Update models to include class-validator decorators.

The following documentation may need to be updated:

Since TypeScript does not store metadata about generics or interfaces, when you use them in your DTOs, ValidationPipe may not be able to properly validate incoming data. For this reason, consider using concrete classes in your DTOs.

2reactions
NoNameProvidedcommented, Dec 13, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

class-validator - npm
Decorator-based property validation for classes.. Latest version: 0.14.0, last published: 16 days ago. Start using class-validator in your ...
Read more >
class-validator/README.md - UNPKG
Allows use of decorator and non-decorator based validation. 11, Internally uses [validator.js][1] to perform validation. 12 ...
Read more >
How to make class-validator to stop on error? - Stack Overflow
I want class validator to stop validation as soon as the first error is found. I know there is a stopAtFirstError option, but...
Read more >
Arbitrary Code Execution Vulnerability in the class-validator ...
This issue was fixed in version 0.14.0. That version is currently considered safe, we suggest that you upgrade to the fixed version. Validate....
Read more >
What's new in 1.4.0 (January 22, 2022) - Pandas
Custom CSS classes can now be directly specified without string ... 0.14.0 ... Bug in constructing a IntegerArray from pyarrow data failing to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found