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
Copy file name to clipboardExpand all lines: CHANGELOG.md
+12-1Lines changed: 12 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,19 @@
1
1
#### 6.0. / 2020-12-XX
2
2
* BREAKING CHANGES
3
-
* Private `rule.event` property renamed. Use `rule.getEvent()` to avoid breaking changes in the future.
3
+
* To continue using [selectn](https://github.com/wilmoore/selectn.js) syntax for condition `path`s, use the new `pathResolver` feature. Read more [here](./docs/rules.md#condition-helpers-custom-path-resolver). Add the following to the engine constructor:
4
+
```js
5
+
constpathResolver= (object, path) => {
6
+
returnselectn(path)(object)
7
+
}
8
+
constengine=newEngine(rules, { pathResolver })
9
+
```
10
+
(fixes #205)
4
11
* Engine and Rule events `on('success')`, `on('failure')`, and Rule callbacks `onSuccess` and `onFailure` now honor returned promises; any event handler that returns a promise will be waited upon to resolve before engine execution continues. (fixes #235)
12
+
* Private `rule.event` property renamed. Use`rule.getEvent()` to avoid breaking changes in the future.
5
13
* The 'success-events' fact used to store successful events has been converted to an internal data structure and will no longer appear in the almanac's facts. (fixes #187)
14
+
* NEW FEATURES
15
+
* Engine constructor now accepts a `pathResolver` option for resolving condition `path` properties. Read more [here](./docs/rules.md#condition-helpers-custom-path-resolver). (fixes #210)
*[engine.addOperator(String operatorName, Function evaluateFunc(factValue, jsonValue))](#engineaddoperatorstring-operatorname-function-evaluatefuncfactvalue-jsonvalue)
@@ -27,6 +42,8 @@ let engine = new Engine([Array rules], options)
27
42
an exception is thrown. Turning this option on will cause the engine to treat
28
43
undefined facts as `undefined`. (default: false)
29
44
45
+
`pathResolver` - Allows a custom object path resolution library to be used. (default: `json-path` syntax). See [custom path resolver](./rules.md#condition-helpers-custom-path-resolver) docs.
46
+
30
47
### engine.addFact(String id, Function [definitionFunc], Object [options])
Copy file name to clipboardExpand all lines: docs/facts.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,9 @@
3
3
Facts are methods or constants registered with the engine prior to runtime and referenced within rule conditions. Each fact method should be a pure function that may return a either computed value, or promise that resolves to a computed value.
4
4
As rule conditions are evaluated during runtime, they retrieve fact values dynamically and use the condition _operator_ to compare the fact result with the condition _value_.
Copy file name to clipboardExpand all lines: docs/rules.md
+54-9Lines changed: 54 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,30 @@
3
3
4
4
Rules contain a set of _conditions_ and a single _event_. When the engine is run, each rule condition is evaluated. If the results are truthy, the rule's _event_ is triggered.
*[String and Numeric operators:](#string-and-numeric-operators)
27
+
*[Numeric operators:](#numeric-operators)
28
+
*[Array operators:](#array-operators)
29
+
*[Rule Results](#rule-results)
15
30
16
31
## Methods
17
32
@@ -217,6 +232,36 @@ json-path support is provided by [jsonpath-plus](https://github.com/s3u/JSONPath
217
232
218
233
For an example, see [fact-dependency](../examples/04-fact-dependency.js)
219
234
235
+
### Condition helpers: custom `path` resolver
236
+
237
+
To use a custom path resolver instead of the `json-path` default, a `pathResolver` callback option may be passed to the engine. The callback will be invoked during execution when a `path` property is encountered.
238
+
239
+
```js
240
+
const { get } =require('lodash') // to use the lodash path resolver, for example
241
+
242
+
functionpathResolver (object, path) {
243
+
// when the rule below is evaluated:
244
+
// "object" will be the 'fact1' value
245
+
// "path" will be '.price[0]'
246
+
returnget(object, path)
247
+
}
248
+
constengine=newEngine(rules, { pathResolver })
249
+
engine.addRule(newRule({
250
+
conditions: {
251
+
all: [
252
+
{
253
+
fact:'fact1',
254
+
path:'.price[0]', // uses lodash path syntax
255
+
operator:'equal',
256
+
value:1
257
+
}
258
+
]
259
+
})
260
+
)
261
+
```
262
+
263
+
This feature may be useful in cases where the higher performance offered by simpler object traversal DSLs are preferable to the advanced expressions provided by `json-path`. It can also be useful for leveraging more complex DSLs ([jsonata](https://jsonata.org/), for example) that offer more advanced capabilities than `json-path`.
264
+
220
265
### Comparing facts
221
266
222
267
Sometimes it is necessary to compare facts against other facts. This can be accomplished by nesting the second fact within the `value` property. This second fact has access to the same `params` and `path` helpers as the primary fact.
debug(`condition::evaluate could not compute object path(${path}) of non-object: ${factValue} <${typeoffactValue}>; continuing with ${factValue}`)
137
-
returnfactValue
138
-
}
139
-
})
140
-
}else{
141
-
letselectn
142
-
try{
143
-
selectn=require('selectn')
144
-
}catch(err){
145
-
console.error('Oops! Looks like you\'re trying to use the deprecated syntax for the ".path" property.')
146
-
console.error('Please convert your "path" properties to JsonPath syntax (ensure your path starts with "$")')
147
-
console.error('Alternatively, if you wish to continue using old syntax (provided by selectn), you may "npm install selectn" as a direct dependency.')
148
-
console.error('See https://github.com/CacheControl/json-rules-engine/blob/master/CHANGELOG.md#500--2019-10-27 for more information.')
149
-
thrownewError('json-rules-engine: Unmet peer dependency "selectn" required for use of deprecated ".path" syntax. please "npm install selectn" or convert to json-path syntax')
0 commit comments