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

No metadata found.... Error #401

Closed
j opened this issue Aug 20, 2019 · 13 comments
Closed

No metadata found.... Error #401

j opened this issue Aug 20, 2019 · 13 comments
Labels
Question ❔ Not future request, proposal or bug issue Wontfix ❌ This will not be worked on

Comments

@j
Copy link
Contributor

j commented Aug 20, 2019

I'm using class-validator and having to bring it in as a dependency as well to add the constraints, but get the annoying console warning:

No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.

I don't want to pass validate: false and I don't want to get into a situation where my validation constraints aren't running due to how it works internally.

@MichalLytek
Copy link
Owner

Once again, what is your problem? You don't use validation but you don't want to pass validate: false?

No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.

See this for more info:
typestack/class-validator#279

@MichalLytek MichalLytek added Need More Info 🤷‍♂️ Further information is requested Question ❔ Not future request, proposal or bug issue labels Aug 21, 2019
@j
Copy link
Contributor Author

j commented Aug 21, 2019

No, I use validation, so I don't pass validate: false, but I need to add class-validator as a dependency, so seeing that error message worries me because I'm not sure if my own custom validation logic is going to be executed due to multiple class-validator since type-graphql doesn't have it as a peerDependency.

To remove the error, I'd think either type-graphql exports all of the validation decorators or uses strict peerDependency.

@j
Copy link
Contributor Author

j commented Aug 21, 2019

Them having an error makes me believe that they're using a global within class-validator to store metadata, so if type-graphql brings their own class-validator and I bring my own class-validator, leads me to feel that my validation constraints won't be executed due to validate being called within the type-graphql library. It's concerning and makes me worry, that's all.

@MichalLytek
Copy link
Owner

Yes, the problem is that TypeGraphQL and TypeORM uses global to store the metadata across multiple instances of packages while class-validator have own local storage per package.

So multiple instances of class-validator will result in that error unless you provide your own container for metadata storage. I had a project with separated repo for responses (entities) and inputs (requests) models and separate backend and frontend repos. I did this trick to make it work across multiple modules with npm link:

import * as TypeORM from "typeorm";
import * as ClassValidator from "class-validator";
import { Container } from "typedi";

TypeORM.useContainer(Container);
ClassValidator.useContainer(Container);

export * from "./entities";
export * from "./requests";

I'd think either type-graphql exports all of the validation decorators or uses strict peerDependency.

This doesn't fix the error. Without lerna and hoisting dependencies or using a private npm registry, you will have two node_modules for both project and both will have own instances of class-validator package.

@j
Copy link
Contributor Author

j commented Aug 22, 2019

Okay, hmm. I'm using inversify for my container and yarn workspaces (no lerna).

The error comes from within type-graphql package:

node_modules/type-graphql/node_modules/class-validator/validation/ValidationExecutor.js

Based off the error message, I'd think that if I brought my own class-validator and it was at the root of node_modules (which it is), then the error would go away, but I guess not?

I suppose I'll just play it safe and do manual validation.

@MichalLytek
Copy link
Owner

I'd think that if I brought my own class-validator and it was at the root of node_modules

Try also npm dedupe but I'm not sure if it works with node_modules in parent directory.
If not, even peer dependency won't work as it checks only local node_modules.

There would be a problem with duplicated graphql-js too even without peer dependency as the instaceof check doesn't work between different instances of module.

I suppose I'll just play it safe and do manual validation.

You can use method or param decorators to create own validation rules, you don't have to do this manually inside each resolver's body.

So you have a few options:

  • use dependencies hoisting (maybe yarn workspaces can do that too)
  • make a peer dependency on class-validator in your models package and use private npm registry or git to install bundled package without node_modules
  • create a PR for class-validator that uses global scoped metadata storage
  • use own decorators that imports package from root node_modules

@j
Copy link
Contributor Author

j commented Aug 22, 2019

I oddly get the error even when I dedupe, use resolutions, and have one instance of class-validator.

My mono-repo packages don't have any node_modules, they are all just in the root node_modules directory. node_modules under type-graphql package also doesn't contain class-validator. There's just one.....

I'll look into using my own decorators. I'm also not using TypeORM. So it's interesting to not know what's going on.

@j
Copy link
Contributor Author

j commented Aug 22, 2019

I'm sure it has to do with how I use inversify. I'm creating a container and bootstrapping code there, so my class imports happen before, but I get the error even after I remove useContainer from class-validator.

@j
Copy link
Contributor Author

j commented Aug 22, 2019

@19majkel94 Is it possible now to create a middleware that has the converted inputs/args? If not, it might be cool if there was an option to get the decorated params on a middleware function call then people can bring their own.

@MichalLytek
Copy link
Owner

I oddly get the error even when I dedupe, use resolutions, and have one instance of class-validator.

If you register your container after evaluating the class decorators, it will have empty metadata storage in the container, so you will see this warning.

Is it possible now to create a middleware that has the converted inputs/args?

Not yet - see #123.

to get the decorated params on a middleware function call then people can bring their own

Custom validation will be supported by vNext and plugins system 😉

@j
Copy link
Contributor Author

j commented Aug 23, 2019

@19majkel94 can't wait for it! :)

@Swatinem
Copy link

There is more than once class-validator version installed probably.

The warning message is spot-on!

You depend on an outdated version:

"class-validator": ">=0.9.1",

Whereas when someone imports class-validator in their own code, they will probably use the latest version, which is 0.10.0

@MichalLytek
Copy link
Owner

I will close this issue for now as it looks like the problem is with how class-validator store the metadata internally and how to cope with splitted packages/monorepo, not about TypeGraphQL and how it uses class-validator.

@MichalLytek MichalLytek added Wontfix ❌ This will not be worked on and removed Need More Info 🤷‍♂️ Further information is requested labels Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question ❔ Not future request, proposal or bug issue Wontfix ❌ This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants