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.

question: ValidateNested with IsOptional throws a BadRequest

See original GitHub issue

Currently using -

  • class-transformer - 0.2.3
  • class-validator - 0.11.0
  • express - 4.15.4
  • node version - 13.9.0

I have 2 classes -

export class A {
    @IsNotEmpty()
    @Length(2, 32)
    public someField: string;

    @IsOptional()
    @IsEnum(EnumClass)
    public someEnum: EnumClass;
}
export class B {
    @ValidateNested({ each: true })
    @Type(() => A)
    public arrayOfAs: A[];
}

For some reason - I get a BadRequest when I don’t send the enum type although it’s decorated with “IsOptional”, am I doing something wrong or is that a bug? Couldn’t find any sample of code to validate a nested object array with isOptional decoraror.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Jhonnyccommented, Apr 8, 2020

Hi @vlapo, Not really, but after a clean build + set and unset of defaultErrorHandler on routing-controller options I got data on the “errors” property of the error object, not sure why (as per the documentation of routing controllers defaultErrorHandler is set by default on dev environment, but explicitly setting it to true building and then back to false then a rebuild solved the problem) might have been a cache problem? 0_o

0reactions
lock[bot]commented, Apr 15, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IsDefined + IsOptional combination return error 500 instead of ...
Should return error 400 bad request, since its undefined. Actual behavior. It throws internal server error (isnt returning the error 400).
Read more >
Validating nested objects with class-validator in NestJS
Today I have for you a quick and short article. Maybe it will help someone. I'm using class-validator for request validation in NestJS ......
Read more >
mongodb - Validate nested object with dot syntax using class ...
I'm having trouble on validating nested object when I try to update a specific prop of a nested object e.g.:
Read more >
Validation | NestJS - A progressive Node.js framework
By default it throws BadRequestException . exceptionFactory, Function, Takes an array of the validation errors and returns an exception object to be thrown....
Read more >
class-validator - Bountysource
I understand that this problem is concerning class-transformer more than it is ... import { IsString, ValidateNested, validateSync } from 'class-validator'; ...
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