Skip to content

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

Open
@konradni

Description

@konradni

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions