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.

ValidateNested throw 500, when it receives string or number instead of object

See original GitHub issue

I 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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
chrisjpalmercommented, May 8, 2018

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:

export class PostInput {
  @ValidateNested()
  @Type(() => PostInputMessage)
  @IsArray()
  entries: PostInputMessage[];
}

export class PostInputMessage {
  @IsString()
  message: string;
}

if my message input contains this:

{
       "entries":["Hello", "How are you"]
}

No validation error is thrown. However if my input looks like this:

{
      "entries":[{"pinapples":"Hello"}, {"pinapples":"How are you"}]
}

A validation error is correctly thrown. Versions: class-transformer 0.1.9, class-validator 0.8.1

4reactions
kiwikerncommented, Dec 14, 2018

@chrisjpalmer

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 came across the same problem on Stackoverflow and proposed a custom validator as a workaround.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

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