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

docs: add minimal example for usage with inversify #607

Open
2 tasks
nolazybits opened this issue May 12, 2020 · 11 comments
Open
2 tasks

docs: add minimal example for usage with inversify #607

nolazybits opened this issue May 12, 2020 · 11 comments
Labels
type: feature Issues related to new features.

Comments

@nolazybits
Copy link
Contributor

nolazybits commented May 12, 2020

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

@nolazybits
Copy link
Contributor Author

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 :)

@satanTime
Copy link
Contributor

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?

@harindaka
Copy link

@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?

@satanTime
Copy link
Contributor

Hi @harindaka,

Is there a chance to create an example with min code to reproduce the issue?

@harindaka
Copy link

harindaka commented Nov 28, 2020

@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?

@satanTime
Copy link
Contributor

@harindaka, yep, makes sense, might you create a PR? I think you know better how to describe it.

@harindaka
Copy link

@satanTime done

@NoNameProvided NoNameProvided changed the title missing binding CustomConstraint docs: add minimal example for usage with inversify Jan 11, 2021
@NoNameProvided NoNameProvided added flag: needs docs type: feature Issues related to new features. labels Jan 11, 2021
@dreamerblue
Copy link

Same issue. Any progress for supporting inversify?

@afraz-khan
Copy link

afraz-khan commented Nov 23, 2021

any official documentation piece for this?

@ttruongatl
Copy link

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);

@LimbersMay
Copy link

LimbersMay commented Aug 10, 2023

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Issues related to new features.
Development

No branches or pull requests

8 participants