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

instantiate custom rules with DI #381

Closed
fracz opened this issue Nov 4, 2016 · 2 comments
Closed

instantiate custom rules with DI #381

fracz opened this issue Nov 4, 2016 · 2 comments

Comments

@fracz
Copy link

fracz commented Nov 4, 2016

Apparently, there is no way to inject something from DI container when creating a custom rule. For example, I want to inject preconfigured HttpClient that exists in the container in order to call the backend for checking e-mail uniqueness.

Currently, you need to wrap the custom rule definition in an artificial class which you need to inject only in order to register the cutom rule (see an example of such).

I wish I could define a custom rule as a class, that would be instantiated with DI, resolving all of its dependencies.

Dream:

Definition:

class EmailUniquenessCustomValidationRule implements ValidationRule {
  validate (value, obj) { /* ... */ }
}

Registration (not sure if it is mandatory if we use class name directly):

ValidationRules.customRule(ValidateEmailUniqueness);

Usage:

ValidationRules
  .ensure('email').satisfiesRule(ValidateEmailUniqueness);
@fracz fracz changed the title Add ability to specify custom rule dependencies Add ability to specify custom rule dependencies (instantiate custom rule with DI) Nov 4, 2016
@jdanyow
Copy link
Contributor

jdanyow commented Mar 25, 2017

Is this a shortcut for doing something like:

ValidationRules.customRule(
  'email-unique',
  container.get(ValidateEmailUniqueness).validate
);

Another thing you could do today is:

main.js

export function configure(aurelia: Aurelia) {
  aurelia.use....
    ...
    .feature('./custom-rules')
    ...

custom-rules.js

configure(config: FrameworkConfiguration) {
  const service = config.container.get(EmailService);

  ValidationRules.customRule(
    'email-unique', 
    value => service.checkUnique(value)
  );

  ValidationRules.customRule(
    ...
    ...
  );

}

what do you think?

@jdanyow jdanyow changed the title Add ability to specify custom rule dependencies (instantiate custom rule with DI) instantiate custom rules with DI Mar 26, 2017
@dkent600
Copy link
Contributor

dkent600 commented Mar 27, 2017

I have proposed a solution for this. One would be able to use DI to instantiate SatisfiesConditionArguments (ie, any of SatisfiesConditionParameters | SatisfiesRuleParameters | SatisfiesTaggedRuleParameters) and pass it into a new method satisfiesCondition.

Note that part of this is also to support tagged rules, using SatisfiesTaggedRuleParameters.

This API has the advantage going forward of making it quite easy to extend the way satisfies works without breaking existing functionality.

It is forked from another branch of mine, hence the "FluentRulesGenerator", but could easily be made to work with the current master, if FluentRulesGenerator is not also accepted as a change.

export type SatisfiesConditionArguments =
  SatisfiesConditionParameters | SatisfiesRuleParameters | SatisfiesTaggedRuleParameters;

/**
   * Applies a named or ad hoc rule to an ensured property or object.
   * @param SatisfiesConditionArguments Configuration for the rule. Can be either a SatisfiesConditionParameters, 
   * for ad hoc rules, or SatisfiesRuleParameters, for named rules, SatisfiesTaggedRuleParameters for tagged rules.
   */
  public satisfiesCondition(params: SatisfiesConditionArguments): FluentRulesGenerator<TObject>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants