question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deep nested validation broken

See original GitHub issue

Description

When trying to validate nested objects using @ValidateNested(), whitelist: true and forbidNonWhitelisted: true, error "property XY should not exist" gets thrown for no apparent reason.

Reproduction

Here a vastly simplified version of my use case:

class ProductPricing {
    @Type(() => ProductPricingOptionsDTO)
    options: ProductPricingOptionsDTO;
}
class ProductPricingOptionsDTO{
    @ValidateNested()
    @Type(() => OnlineUserPricingOptionsDTO)
    onlineUserPrices: OnlineUserPricingOptionsDTO;
}
class OnlineUserPricingOptionsDTO{
    @IsNumber()
    @Min(2)
    price: number;
}

and validating the following payload:

{
    "options": {
        "onlineUserPrices": {
            "price": 2
        }
    }
}

with whitelist: true, forbidNonWhitelisted: true throws an error containing the following message:

"constraints": {
    "whitelistValidation": "property price should not exist"            }
}

Environment

  • nodejs:
  • browser:
  • typeorm:

class-validator version: ^0.12.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:25 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
murbanowiczcommented, Sep 24, 2020

Cheers! We have found out that adding @Type(() => NestedType) was the thing. It works without adding that parameter which you suggested. Adding comment for β€œfuture generations”.

4reactions
gabrieloczkowskicommented, Sep 24, 2020

Hi! I had the same issue but I managed to resolve it by using:

transformOptions: {
    enableImplicitConversion: true,
},

Please let me know if it helped also in your cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validate nested objects using class validator and nestjs
I've just put your code in an empty sample project and it seems to work for me. What specific value is "not working"?...
Read more >
Validating nested objects with class-validator in NestJS
I'm using class-validator for request validation in NestJS really often. A few days ago I needed to validate a nested object.
Read more >
Validation messages for deeply nested properties are masked ...
In situations where a schema has multiple level nested allOf, oneOf or anyOf properties, validation errors in the deeper nested propertiesΒ ...
Read more >
Built-In, Nested, Custom Validators with FluentValidation
Deep Dive Into Different Validators with FluentValidation. Posted by Ryan Miranda | Updated Date Sep 6, 2022 | 2 Β· Code Maze Book...
Read more >
Refactoring-safe nested validation with express-validator and ...
Refactoring-safe nested validation with express-validator and ts-simple- ... if you don't have tests, your validation will be broken now.
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