docs: add minimal example for usage with inversify
Description
Hello everyone.
Using the last version 0.12.2 I receive
Error: No matching bindings found for serviceIdentifier: CustomConstraint
at _validateActiveBindingCount (/home/nodebrick/application/node_modules/inversify/lib/planning/planner.js:62:23)
at _getActiveBindings (/home/nodebrick/application/node_modules/inversify/lib/planning/planner.js:48:5)
at _createSubRequests (/home/nodebrick/application/node_modules/inversify/lib/planning/planner.js:85:26)
at Object.plan (/home/nodebrick/application/node_modules/inversify/lib/planning/planner.js:136:9)
at /home/nodebrick/application/node_modules/inversify/lib/container/container.js:317:37
at Container._get (/home/nodebrick/application/node_modules/inversify/lib/container/container.js:310:44)
at Container.get (/home/nodebrick/application/node_modules/inversify/lib/container/container.js:230:21)
at InversifyContainerWrapper.get (/home/nodebrick/application/packages/nodebrick-core/src/di/InversifyContainerWrapper.ts:23:45)
at getFromContainer (/home/nodebrick/application/node_modules/src/container.ts:53:44)
at ConstraintMetadata.get [as instance] (/home/nodebrick/application/node_modules/src/metadata/ConstraintMetadata.ts:46:16)
Just wondering If some more changes need to be done
https://github.com/typestack/class-validator/search?q=getFromContainer&unscoped_q=getFromContainer
Just wondering If some more changes need to be done https://github.com/typestack/class-validator/search?q=getFromContainer&unscoped_q=getFromContainer
Any ideas?
Thanks for all the hard work btw 👍
Environment
- [ ] nodejs: 12
- [ ] framework/library: nodebrick
class-validator version: 0.12.2
So I had to actually remove the use of the DI container all together it seems to have it working Is that correct?
Before
useContainer(
new InversifyContainerWrapper(NodebrickCore._ContainerDI),
{
fallbackOnErrors: false,
fallback: false,
});
I removed the ☝️ code and it works. Is that the correct approach?
Cheers :)
Without useContainer I think it doesn't solve dependencies and they are undefined, please check them in constructors of your validators.
And might you share code of CustomConstraint?
@satanTime I'm facing this problem too using class-validator with inversify. I do not have a CustomConstraint class defined anywhere in my code. What could be the reason? Are there any instructions for using class-validator with inversify or a code sample?
Hi @harindaka,
Is there a chance to create an example with min code to reproduce the issue?
@satanTime ok after some googling, I found that it is necessary to use an adaptor for third party DI containers such as inversify to work. Obviously this must be the reason for me since I was not aware of that. It's not super obvious to someone who's only using class validator and not routing controllers. Maybe update the documentation for class-validator to link to that?
@harindaka, yep, makes sense, might you create a PR? I think you know better how to describe it.
@satanTime done
Same issue. Any progress for supporting inversify?
any official documentation piece for this?
The following could be a solution!
// InversifyContainerAdapter.ts
import { Container } from "inversify";
export class InversifyContainerAdapter extends Container {
public get<T>(serviceIdentifier: any): T {
const id = serviceIdentifier;
try{
return super.get(id);
}catch(e){
this.bind(serviceIdentifier).to(serviceIdentifier).inSingletonScope();
}
return super.get(id);
}
}
// App.ts
import { Container } from "inversify";
import { useContainer } from "class-validator";
import {InversifyContainerAdapter} from "./InversifyContainerAdapter";
const container = new InversifyContainerAdapter();
useContainer(container);
@ttruongatl I tried your solution but now I get:
I am using routing-controllers, inversify and class-validator.
Error: Missing required @injectable annotation in: Validator. node-app | at getTargets (/usr/src/app/node_modules/inversify/src/planning/reflection_utils.ts:32:11) node-app | at getDependencies (/usr/src/app/node_modules/inversify/src/planning/reflection_utils.ts:14:10) node-app | at /usr/src/app/node_modules/inversify/src/planning/planner.ts:179:43 node-app | at Array.forEach (<anonymous>) node-app | at _createSubRequests (/usr/src/app/node_modules/inversify/src/planning/planner.ts:164:18) node-app | at plan (/usr/src/app/node_modules/inversify/src/planning/planner.ts:240:5) node-app | at /usr/src/app/node_modules/inversify/src/container/container.ts:623:25 node-app | at InversifyContainerAdapter.Container._get (/usr/src/app/node_modules/inversify/src/container/container.ts:574:37) node-app | at InversifyContainerAdapter.Container._getButThrowIfAsync (/usr/src/app/node_modules/inversify/src/container/container.ts:580:25) node-app | at InversifyContainerAdapter.Container.get (/usr/src/app/node_modules/inversify/src/container/container.ts:325:17)