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

How to create nested validations #195

Closed
iagomelanias opened this issue Nov 13, 2018 · 1 comment
Closed

How to create nested validations #195

iagomelanias opened this issue Nov 13, 2018 · 1 comment
Labels
Duplicate 🔑 This issue or pull request already exists Question ❔ Not future request, proposal or bug issue

Comments

@iagomelanias
Copy link

iagomelanias commented Nov 13, 2018

Describe the issue
Hello! 👋
I'm trying to validated InputType. A specific field has it's own type. It looks like below:

@InputType()
export class RecipeInput {
  @Field()
  @Type(() => AuthorInput)
  author: AuthorInput;
}

The CustomerInput looks like this:

@InputType()
export class AuthorInput {
  @Field()
  @Length(1, 255)
  firstName: string;
}

TypeGraphQL builds the schema correctly, but class-transformer does not validate the nested input. I also tried to add the @ValidateNested decorator, but i get the following error:

"constraints": {
  "nestedValidation": "nested property customer must be either object or array"
}

Error: Argument Validation Error,
  at Object.<anonymous> (/Users/iago/Drive/Code/api/node_modules/type-graphql/resolvers/validate-arg.js:21:19),
  at Generator.throw (<anonymous>),
  at rejected (/Users/iago/Drive/Code/api/node_modules/tslib/tslib.js:105:69),
  at process.internalTickCallback (internal/process/next_tick.js:77:7)

I found a test case which contains a nested input type, but i didn't find any test case using validations.

As mentioned in this comment, it looks like the nested object is not converted to AuthorInput class, the property author is [Object: null prototype].

So, with the current limitations, is there any way to implement a nested validation?

Are you able to make a PR that fix this?
I don't think so, but i can add some test cases to ensure this is properly covered.

Additional context

  • TypeGraphQL Version: 0.15.0
  • Node Version: v11.1.0
@MichalLytek MichalLytek added Duplicate 🔑 This issue or pull request already exists Question ❔ Not future request, proposal or bug issue labels Nov 13, 2018
@MichalLytek
Copy link
Owner

The test case is only for schema generation, not for runtime instance check nor nested validation (unfortunately).

So closing this as duplicate of #133 🔒

As a temporary workaround you need to perform the validation manually in resolvers, I think that class-transformer-validator would be the best (one-liner).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🔑 This issue or pull request already exists Question ❔ Not future request, proposal or bug issue
Projects
None yet
Development

No branches or pull requests

2 participants