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

@ValidateNested does not seem to work? #431

Closed
richarddd opened this issue Oct 4, 2019 · 3 comments

Comments

@richarddd
Copy link

@richarddd richarddd commented Oct 4, 2019

Using the latest version: 0.10.1

Please check: https://codesandbox.io/s/gallant-wildflower-1pwls

import { Validator } from "class-validator";
import { Post } from "./Post";
import { Tag } from "./Tag";

let validator = new Validator();

let tag1 = new Tag();
tag1.name = "ja";

let tag2 = new Tag();
tag2.name = "node.js";

let post1 = new Post();
post1.title = "Hello world";
post1.tags = [tag1, tag2];

validator.validate(post1).then(result => {
    console.log("1. should not pass: ", result);
});

The first example works fine.

But when you add some other object to tags validation does not fail:

let post2 = new Post();
post2.title = "Hello world";
post2.tags = [1, 2] as any;

validator.validate(post2).then(result => {
    console.log("2. should not pass: ", result);
});

Furthermore, omitting tags altogether passes validation:

let post2 = new Post();
post2.title = "Hello world";

validator.validate(post2).then(result => {
    console.log("2. should not pass: ", result);
});

Plese see this codesandbox to replicate the issue:
https://codesandbox.io/s/gallant-wildflower-1pwls

@vlapo

This comment has been minimized.

Copy link
Contributor

@vlapo vlapo commented Oct 4, 2019

Hi @richarddd,

Thank you for so detailed description and example.
But this is how @ValidateNested() works. It will not check instance type or array size. You have to define more validators. E.g.

export class Post {
  @Length(10, 20, {
    message: "Incorrect length!"
  })
  title!: string;

  @IsArray()
  @ArrayNotEmpty()
  @IsInstance(Tag, { each: true })
  @ValidateNested()
  tags!: Tag[];
}

This will check If:

  • tags is not empty array
  • all items in array are instance of Tag class

Hope this helps :)

@vlapo vlapo added the type: question label Oct 4, 2019
@richarddd richarddd closed this Oct 4, 2019
@richarddd

This comment has been minimized.

Copy link
Author

@richarddd richarddd commented Oct 4, 2019

Thanks! Sorry for the missunderstanding!

@lock

This comment has been minimized.

Copy link

@lock lock bot commented Oct 11, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants
You can’t perform that action at this time.