Skip to content

Commit

Permalink
Feature/update json rules engine (#101)
Browse files Browse the repository at this point in the history
* Simplified rules unit tests

* Updated json-rules-engine

* Updated code to use native json-rules-engine jsonpath syntax

* Made linter happy. Removed jsonpath dependency.
  • Loading branch information
partiallyordered authored Dec 5, 2019
1 parent c2b9016 commit 50564a1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 249 deletions.
120 changes: 29 additions & 91 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@
"good-console": "8.0.0",
"good-squeeze": "5.1.0",
"hapi-openapi": "1.2.6",
"json-rules-engine": "4.1.0",
"jsonpath": "1.0.2",
"json-rules-engine": "5.0.0",
"knex": "0.20.3",
"memory-cache": "0.2.0",
"mysql": "2.17.1",
Expand Down
92 changes: 0 additions & 92 deletions src/model/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
*/

const jre = require('json-rules-engine')
const jsonpath = require('jsonpath')
const assert = require('assert').strict

module.exports.events = {
Expand Down Expand Up @@ -67,97 +66,6 @@ const createEngine = () => {
return deepEqual(factValue, ruleValue)
})

/**
* The json-rules-engine path only supports selectn paths. This is problematic, as selectn cannot
* traverse an array with filters, it can only use a static array index. For example, selectn
* cannot find the age of user with name 'Tutaia' in the following array, without knowing in
* advance that Tutaia will be the first element of the array:
*
* [ { name: 'Tutaia', age: 25 }, { name: 'Kim', age: 66 } ]
*
* In many (most) cases we cannot know the order and content of our data in advance. We therefore
* provide a more flexible, but still declarative jsonpath dynamic fact. Note that the examples
* provided below in this comment are reproduced in the tests.
*
* Note that the jsonPathFact requires the deepEqual operator to function correctly, as jsonpath
* returns an array of results.
*
* See https://www.npmjs.com/package/jsonpath for more information on jsonpath.
*
* The json-path fact exploits the fact params as its API by allowing the user to specify the fact
* they'd like to retrieve and a jsonpath within that fact.
*
* The following example looks at the payload fact, and checks whether the payer fspId, at jsonpath
* `$.payload.payer.partyIdInfo.fspId` is not `payerfsp`:
*
* {
* fact: 'json-path',
* params: {
* fact: 'payload',
* path: '$.payload.payer.partyIdInfo.fspId'
* },
* operator: 'notDeepEqual',
* value: [ 'payerfsp' ]
* }
*
* Note that the value of .params.fact will be a top-level key in the jsonpath query, and therefore
* must correspond to the top-level key in .params.path. A general example:
*
* {
* fact: 'json-path',
* params: {
* fact: 'top-level-key',
* path: '$.top-level-key'
* },
* ...
* }
*
* Supported top-level keys (facts) are:
* - payload
* - headers
* - payer
* - payee
*
* Another slightly more complex example, comparing the value of the fspiop-source header with the
* payer fspId in the payload:
*
* {
* fact: 'json-path',
* params: {
* fact: 'payload',
* path: '$.payload.payer.partyIdInfo.fspId'
* },
* operator: 'deepEqual',
* value: {
* fact: 'json-path',
* params: {
* path: '$.headers[\'fspiop-source\']',
* fact: 'headers'
* }
* }
* }
*
* So far, no example rules have _required_ the use of jsonpath. The following rule filters the
* KYCPayerTier key in the extension list of the quote payload and verifies that it is '1':
*
* {
* fact: 'json-path',
* params: {
* fact: 'payload',
* path: '$.payload.extensionList[?(@.key === \'KYCPayerTier\')].value'
* },
* operator: 'deepEqual',
* value: [ '1' ]
* }
*/
const jsonPathFact = function (params, almanac) {
return almanac.factValue(params.fact)
.then((fact) => {
return jsonpath.query({ [params.fact]: fact }, params.path)
})
}
engine.addFact('json-path', jsonPathFact)

return engine
}

Expand Down
Loading

0 comments on commit 50564a1

Please sign in to comment.