CacheControl / json-rules-engine

A rules engine expressed in JSON
ISC License
2.6k stars 461 forks source link

Is it possible make value as a dynamic data like using path? #274

Open intermx-reddysai opened 3 years ago

intermx-reddysai commented 3 years ago

Example:

conditions: {
    all: [{
      fact: 'account-information',
      operator: 'equal',
      value: 'microsoft', // **Is it possible make this value 'microsoft' as a dynamic content?**
      path: '$.company' // access the 'company' property of "account-information"
    }]
  },
  event: {
    type: 'microsoft-christmas-pto',
    params: {
      message: 'current microsoft employee taking christmas day off'
    }
  }
SivaGanesh56 commented 3 years ago

+1

raghava9010 commented 3 years ago

+1

mcacek commented 2 years ago

I believe that you can accomplish this by fact comparison and just provide the target value as dynamic fact at runtime or as a static fact on engine initialization.

https://github.com/CacheControl/json-rules-engine/blob/90272d61ed7bea8b7d4000305511e932490d2e2e/docs/rules.md#comparing-facts

Something along these lines. This doesn't address event message interpolation though as that's not currently supported.

const ptoRule = new Rule({
  name: 'christmas-pto-rule',
  conditions: {
    all: [{
      fact: 'account-information',
      operator: 'equal',
      value: {
        fact: 'company'
      },
      path: '$.company'
    }]
  },
  event: {
    type: 'christmas-pto',
    params: {
      message: 'current employee taking christmas day off'
    }
  }
});

engine.addRule(ptoRule);

engine.run({
  company: 'microsoft'
});