Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ValidateNested always fails with optional/nullable values even when IsOptional({each:true}) is set #2587

Open
Woodz opened this issue Feb 28, 2025 · 0 comments
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@Woodz
Copy link

Woodz commented Feb 28, 2025

Description

Minimal code-snippet showcasing the problem

class Bar {
  @IsAlphanumeric()
  baz: string;
}

class Foo {
  @IsOptional({ each: true })
  @ValidateNested({ each: true })
  bars: Map<string, Bar | null> | null;

  @IsOptional({ each: true })
  @ValidateNested({ each: true })
  abc: (Bar | null)[];
}

const foo = new Foo();
const bar1 = new Bar();
bar1.baz = "Hello";
const bar2 = new Bar();
bar2.baz = "World";
foo.bars = new Map([
  ["a", bar1],
  ["b", bar2],
  ["c", null],
]);
foo.abc = [bar1, bar2, null];

(async function () {
  console.dir(JSON.stringify(foo));
  const validationErrors = await validate(foo);
  console.dir(JSON.stringify(validationErrors));
})();

https://codesandbox.io/p/sandbox/class-validator-forked-kfxxkq?file=%2Fsrc%2Findex.ts%3A38%2C46

Expected behavior

Since we have @IsOptional({ each: true }), I expect these fields to not have any validation errors when containing nulls

Actual behavior

Returns validation errors:

  1. each value in nested property bars must be either object or array
  2. each value in nested property abc must be either object or array
@Woodz Woodz added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

1 participant