Deep nested validation broken
See original GitHub issueDescription
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:
- Created 3 years ago
- Reactions:7
- Comments:25 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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β.Hi! I had the same issue but I managed to resolve it by using:
Please let me know if it helped also in your cases.