You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
question: No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies. #261
Same issue for me, i don't exactly get why it prints
Reacted by Viktor Sikk, Joon Ho Cho, David Babel, jean-noelp, Philipp Litzenberger, Kirill Kulakov, expatyler, Kalugin Sergei, vellengs, Eileen Kuo and 15 more
What enviroment do you use class-validator? What other libraries do you use with it?
The reason to this error as the message states there are multiple class-validator versions in your node_modules folder and they do not share the same metadata storage. Let me explain in details below:
Reason
Why do I see this
First, historyically class-validator only checked the type of known properties when validating a class and this led to silent errors when no metadata was found for a class and class-validator validated nothing and stayed silent. So an option was added to throw an error on unknown classes and the above message is logged when that option is not enabled to warn you.
Technical reason
To understand the problem, first, we need to understand how class-validator knows what to check on the properties. It knows ther required types because of the attached decorators, which will register the expected type in an internal storage inside class-validator.
But that storage is not shared accross the different versions of class-validator, so if you declarad a semver range in your modules the way that they doesn't share the same allowed latest version then npm will install multiple versions for you and will break it.
So this is what propably is in your node_modules right now:
node_modules
/class-validator@0.9.x
/your-another-lib-or-something-else-using-class-validator (requires 0.8.x)
/node_modules
/class-validator@0.8.x
/ ... lot of other folders
package.json (requires 0.9.x)
In the example above the module called your-another-lib-or-something-else-using-class-validator requires a different version of the class-validator than the main project, so npm will install that specific version for him, but that is our problem, because now if you want to validate any model defined in that module from the outside you will get the error because your outer proram will use class-validator@0.9.x while the inner program used the inner class-validator@0.9.y instance to register it's type metadata.
So the folder sturcutur we want is the following:
node_modules
/class-validator@0.9.x
/your-another-lib-or-something-else-using-class-validator (requires version 0.9.x)
/ ... lot of other folders
package.json (requires 0.9.x)
Solution
In short, you need to make sure to install the same class-validator accross all the projects you use together. This is most easily done by adding the same semver range, eg: 0.9.x. (You cannot use ^0.9.0 because we have not reached 1.0, so a minor change considered a major change by semver.)
Required extra steps
You need to update npm to the latest version, npm flatten the node_modules for you sice the 6. version only.
I hope I could explain the probem (this one is hard to explain!), if anything is not clear, feel free to ask more, and I will try my best to explain it.
@NoNameProvided I am also experiencing these errors and unable to validate. I have ensured that there is only one version of class-validator installed throughout my package tree. I have confirmed by searching the node_modules folder that only one version has been installed, and the node_modules has been flattened.
I still unfortunately get the error in my console and am unable to validate.
Thanks in advance.
Reacted by Jed Thompson, Jody, Sina Nejati, Niek van Bennekom, Diego, Joseph Buchma, Vinicius Vieira, Asim Alharbi, gliluaume, Jon Lauridsen and 2 more
I'm having the same issue. In my project I have routing-controllers package which currently has outdated dependency of class-validator package. There's an open ticket for this as well.
I would argue that routing-controllers shouldn't have a hard dependency on class-validator package, because of the nature of this issue.
Ok, I got it working! For some reason it does not work when used with typedi container.
To be more specific, it worked after I removed following piece:
Thanks, I had the same issue.
As we are using Lerna, the flattening is not an effective solution.
Let's say if have my own 2 packages, A and B, both having class-validator as a dependency.
Then, I add an external dependency, called C, to my B package. C also requires class-validator, but with another version.
Then, using lerna boostrap all the dependecies are installed in A/node_modules, B/node_modues, etc.
The last step lerna is taking, if you require local package in the same monorepo, is to create a symlink like A/node_modules/B -> ../../B. That's when you lose any flattened structure, and you end up with a different version of class-validator (as far as I can tell).
Setting the same version of class-validator used in C in my packages in both A and B solved the issue
I hate this misleading warn message but for now it is our only indication about missing metadata in prod (whatever it is because of no validation rules or more than one class-validator libs installed).
Lets to nothing now. We may change/remove this warn in the future
Hi guys, sharing my experience on this...stuck for 2-3 days, now I got a solution that words for my scenario.
I got the ~ error message when trying to validate models via Jest (unit test) in a NestJS project. The NestJS project references models from a Lerna monorepo (relative path - and NestJS is not part of the monorepo).
Jest references packages in the monorepo via moduleNameMapper like:
I think, the reason I am getting the error message is because the model files resolve 'class-validator' by traversing up its directory path. So, even when if i hoist dependencies to the root Lerna directory, it does not work.
Solution: Specifically, tell Jest know how to resolve 'class-validator' by adding an entry to moduleNameMapper (a Jest configuration option):
Hi @glikaj, the first thing is please verify you use 0.12.0-rc.0 and there's not any lib that uses another version. If the node_modules have only 0.12.0-rc.0 then it means there's no any validation rule and the case is similar to @rfgamaral had - just add a validation rule somewhere and the error should gone. Other cases I would say aren't possible, but I believe in magic and would like to ask you to share the source code for an investigation :)
changed the title [-]No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.[/-][+]question: No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.[/+]on Aug 8, 2020
Activity
DavidBabel commentedon Sep 22, 2018
Same issue for me, i don't exactly get why it prints
NoNameProvided commentedon Nov 4, 2018
Hi @viktor-ku and @DavidBabel!
What enviroment do you use class-validator? What other libraries do you use with it?
The reason to this error as the message states there are multiple
class-validatorversions in yournode_modulesfolder and they do not share the same metadata storage. Let me explain in details below:Reason
Why do I see this
First, historyically
class-validatoronly checked the type of known properties when validating a class and this led to silent errors when no metadata was found for a class andclass-validatorvalidated nothing and stayed silent. So an option was added to throw an error on unknown classes and the above message is logged when that option is not enabled to warn you.Technical reason
To understand the problem, first, we need to understand how
class-validatorknows what to check on the properties. It knows ther required types because of the attached decorators, which will register the expected type in an internal storage insideclass-validator.But that storage is not shared accross the different versions of class-validator, so if you declarad a semver range in your modules the way that they doesn't share the same allowed latest version then npm will install multiple versions for you and will break it.
So this is what propably is in your node_modules right now:
In the example above the module called
your-another-lib-or-something-else-using-class-validatorrequires a different version of the class-validator than the main project, so npm will install that specific version for him, but that is our problem, because now if you want to validate any model defined in that module from the outside you will get the error because your outer proram will useclass-validator@0.9.xwhile the inner program used the innerclass-validator@0.9.yinstance to register it's type metadata.So the folder sturcutur we want is the following:
Solution
In short, you need to make sure to install the same class-validator accross all the projects you use together. This is most easily done by adding the same semver range, eg:
0.9.x. (You cannot use^0.9.0because we have not reached 1.0, so a minor change considered a major change by semver.)Required extra steps
You need to update npm to the latest version, npm flatten the
node_modulesfor you sice the 6. version only.I hope I could explain the probem (this one is hard to explain!), if anything is not clear, feel free to ask more, and I will try my best to explain it.
NoNameProvided commentedon Nov 4, 2018
This is related to #132.
NoNameProvided commentedon Nov 4, 2018
An another explanation: #181 (comment)
FSM1 commentedon Nov 8, 2018
@NoNameProvided I am also experiencing these errors and unable to validate. I have ensured that there is only one version of class-validator installed throughout my package tree. I have confirmed by searching the
node_modulesfolder that only one version has been installed, and the node_modules has been flattened.I still unfortunately get the error in my console and am unable to validate.
Thanks in advance.
niekvb commentedon Nov 21, 2018
Faced the same problem, this helped for me:
Instead of declaring a variable with the type of the class
Initialize the class first before assigning data
It might not be the most beautiful way to solve it, but it seems to work.
Let me know if this solves your issue.
grbatinic-bogdan commentedon Nov 25, 2018
I'm having the same issue. In my project I have routing-controllers package which currently has outdated dependency of
class-validatorpackage. There's an open ticket for this as well.I would argue that
routing-controllersshouldn't have a hard dependency onclass-validatorpackage, because of the nature of this issue.diegolaciar commentedon Nov 29, 2018
@NoNameProvided,
I have the same issue, I get the warning and the validate don' t return any ValidationError.
I also I have confirmed by searching the node_modules folder that only one version has been installed, and the node_modules has been flattened.
npm v6.4.1
tried the command
npm dedupe
but no luck solving this issue.
thanks
josephbuchma commentedon Dec 2, 2018
Having same issue, node_modules flattened 😞
@niekvb check out this
josephbuchma commentedon Dec 2, 2018
Ok, I got it working! For some reason it does not work when used with typedi container.
To be more specific, it worked after I removed following piece:
RDeluxe commentedon Jan 15, 2019
Thanks, I had the same issue.
As we are using Lerna, the flattening is not an effective solution.
Let's say if have my own 2 packages, A and B, both having class-validator as a dependency.
Then, I add an external dependency, called C, to my B package. C also requires class-validator, but with another version.
Then, using
lerna boostrapall the dependecies are installed inA/node_modules,B/node_modues, etc.The last step lerna is taking, if you require local package in the same monorepo, is to create a symlink like
A/node_modules/B -> ../../B. That's when you lose any flattened structure, and you end up with a different version of class-validator (as far as I can tell).Setting the same version of class-validator used in C in my packages in both A and B solved the issue
RDeluxe commentedon Feb 6, 2019
Hello again.
Seems that even with one single version of class-validator, the issue still exists. Any indications about why this could be the case ?
Edit:
Ok, after 2 hours trying to fix this, we realized that Typedi was not going to help (we did not manage to make it work).
Our use case is really simple: we use lerna. And as such, we don't have a flattened 'node_modules' during development.
class-validator should really support such a use case out of the box
44 remaining items
satanTime commentedon Mar 31, 2020
@rfgamaral, I've not seen it in your repo. let's go back to the "chat" :)
rfgamaral commentedon Mar 31, 2020
Sorry everyone, and sorry @satanTime for wasting your time. There was no problem, I made a bit mistake (kinda tired) and it was my problem all along.
Everything is great, please keep up the good work :)
vlapo commentedon Mar 31, 2020
I hate this misleading warn message but for now it is our only indication about missing metadata in prod (whatever it is because of no validation rules or more than one class-validator libs installed).
Lets to nothing now. We may change/remove this warn in the future
vlapo commentedon Mar 31, 2020
Thanks @satanTime for your quick reactions :)
kctang commentedon Apr 1, 2020
Hi guys, sharing my experience on this...stuck for 2-3 days, now I got a solution that words for my scenario.
I got the ~ error message when trying to validate models via Jest (unit test) in a NestJS project. The NestJS project references models from a Lerna monorepo (relative path - and NestJS is not part of the monorepo).
Jest references packages in the monorepo via
moduleNameMapperlike:I think, the reason I am getting the error message is because the model files resolve 'class-validator' by traversing up its directory path. So, even when if i hoist dependencies to the root Lerna directory, it does not work.
Solution: Specifically, tell Jest know how to resolve 'class-validator' by adding an entry to
moduleNameMapper(a Jest configuration option):Hope this helps someone with similar scenario.
satanTime commentedon Apr 5, 2020
Hello to your house too, @Syy0n.
Steps to reproduction please.
glikaj commentedon Apr 8, 2020
For me it happens in authorizationChecker @satanTime.
This happens me when i call the User model or any other class that imports the 'class-validator'.
satanTime commentedon Apr 8, 2020
Hi @glikaj, the first thing is please verify you use
0.12.0-rc.0and there's not any lib that uses another version. If the node_modules have only0.12.0-rc.0then it means there's no any validation rule and the case is similar to @rfgamaral had - just add a validation rule somewhere and the error should gone. Other cases I would say aren't possible, but I believe in magic and would like to ask you to share the source code for an investigation :)glikaj commentedon Apr 9, 2020
Thank you @satanTime, update to
0.12.0-rc.0fixed the problem.lock commentedon Apr 17, 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.
[-]No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.[/-][+]question: No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.[/+]