ValidateNested throw 500, when it receives string or number instead of object
See original GitHub issueI have problem with ValidationNested and I hope that someone will know how handle with it.
Have you tried to send a request with a string or number in the place where the object should be (which is validated by a @ValidateNested annotation)? It will throw error with status code 500.
Example:
export class ClassA {
@ValidateNested()
@Type(() => ClassB) //annotation from class-transformer
public company: ClassB;
@IsOptional()
@IsInt()
public id?: number;
}
export class ClassB {
@IsInt()
public id: number;
@IsString()
@Length(1, 255) //class-validator will try count length of this string, no matter what :(
public name: string;
}
POST request with body:
{
company: 5,
id: 3
}
response:
{
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "Internal Server Error",
"status": 500,
"detail": "TypeError: Cannot read property 'length' of undefined\n ... } //class-validator has tried count ClassB.name length
I’m using class-validator 0.8.5, class-transformer 0.1.9 and routing-controllers 0.7.6
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Validating nested objects with class-validator in NestJS
But if an object in the array doesn't pass validation, I receive only { "statusCode": 500, "message": "Internal server error" } How to...
Read more >NestJs - Class-validator not returning full validation error object
This was a breaking change in Nestv7 . From the migration guide when using the ValidationPipe you can pass an exceptionFactory property like ......
Read more >Built-In, Nested, Custom Validators with FluentValidation
FluentValidation has a number of built-in validators, which saves us writing code to perform the most common validation.
Read more >Pipes | NestJS - A progressive Node.js framework
Nest comes with a number of built-in pipes that you can use out-of-the-box. ... as an interface instead of a class, the metatype...
Read more >SimpleSchema (simpl-schema NPM package)
Validate an Array of Objects and Throw an Error ... const userSchema = new SimpleSchema( { name: String, number: "SimpleSchema.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
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

Hi there.
Having a similar issue. It appears the validator is not picking up when the type is completely wrong, like where a string is in the place of an object. It will however correctly validate an object with incorrect fields. I have the following setup:
if my message input contains this:
No validation error is thrown. However if my input looks like this:
A validation error is correctly thrown. Versions: class-transformer 0.1.9, class-validator 0.8.1
@chrisjpalmer
I came across the same problem on Stackoverflow and proposed a custom validator as a workaround.