-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
Operator decorators
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict' | ||
/* | ||
* This example demonstrates using operator decorators. | ||
* | ||
* In this example, a fact contains a list of strings and we want to check if any of these are valid. | ||
* | ||
* Usage: | ||
* node ./examples/12-using-operator-decorators.js | ||
* | ||
* For detailed output: | ||
* DEBUG=json-rules-engine node ./examples/12-using-operator-decorators.js | ||
*/ | ||
|
||
require('colors') | ||
const { Engine } = require('json-rules-engine') | ||
|
||
async function start () { | ||
/** | ||
* Setup a new engine | ||
*/ | ||
const engine = new Engine() | ||
|
||
/** | ||
* Add a rule for validating a tag (fact) | ||
* against a set of tags that are valid (also a fact) | ||
*/ | ||
const validTags = { | ||
conditions: { | ||
all: [{ | ||
fact: 'tags', | ||
operator: 'everyFact:in', | ||
value: { fact: 'validTags' } | ||
}] | ||
}, | ||
event: { | ||
type: 'valid tags' | ||
} | ||
} | ||
|
||
engine.addRule(validTags) | ||
|
||
engine.addFact('validTags', ['dev', 'staging', 'load', 'prod']) | ||
|
||
let facts | ||
|
||
engine | ||
.on('success', event => { | ||
console.log(facts.tags.join(', ') + ' WERE'.green + ' all ' + event.type) | ||
}) | ||
.on('failure', event => { | ||
console.log(facts.tags.join(', ') + ' WERE NOT'.red + ' all ' + event.type) | ||
}) | ||
|
||
// first run with valid tags | ||
facts = { tags: ['dev', 'prod'] } | ||
await engine.run(facts) | ||
|
||
// second run with an invalid tag | ||
facts = { tags: ['dev', 'deleted'] } | ||
await engine.run(facts) | ||
|
||
// add a new decorator to allow for a case-insensitive match | ||
engine.addOperatorDecorator('caseInsensitive', (factValue, jsonValue, next) => { | ||
return next(factValue.toLowerCase(), jsonValue.toLowerCase()) | ||
}) | ||
|
||
// new rule for case-insensitive validation | ||
const caseInsensitiveValidTags = { | ||
conditions: { | ||
all: [{ | ||
fact: 'tags', | ||
// everyFact has someValue that caseInsensitive is equal | ||
operator: 'everyFact:someValue:caseInsensitive:equal', | ||
value: { fact: 'validTags' } | ||
}] | ||
}, | ||
event: { | ||
type: 'valid tags (case insensitive)' | ||
} | ||
} | ||
|
||
engine.addRule(caseInsensitiveValidTags); | ||
Check failure on line 82 in examples/13-using-operator-decorators.js GitHub Actions / build (14.x)
Check failure on line 82 in examples/13-using-operator-decorators.js GitHub Actions / build (14.x)
Check failure on line 82 in examples/13-using-operator-decorators.js GitHub Actions / build (16.x)
Check failure on line 82 in examples/13-using-operator-decorators.js GitHub Actions / build (16.x)
Check failure on line 82 in examples/13-using-operator-decorators.js GitHub Actions / build (18.x)
|
||
|
||
// third run with a tag that is valid if case insensitive | ||
facts = { tags: ['dev', 'PROD'] } | ||
await engine.run(facts); | ||
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (14.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (14.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (14.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (14.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (16.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (16.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (16.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (16.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (18.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (18.x)
Check failure on line 86 in examples/13-using-operator-decorators.js GitHub Actions / build (18.x)
|
||
|
||
} | ||
start() | ||
|
||
/* | ||
* OUTPUT: | ||
* | ||
* dev, prod WERE all valid tags | ||
* dev, deleted WERE NOT all valid tags | ||
* dev, PROD WERE NOT all valid tags | ||
* dev, PROD WERE all valid tags (case insensitive) | ||
*/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
import OperatorDecorator from './operator-decorator' | ||
|
||
const OperatorDecorators = [] | ||
|
||
OperatorDecorators.push(new OperatorDecorator('someFact', (factValue, jsonValue, next) => factValue.some(fv => next(fv, jsonValue)), Array.isArray)) | ||
OperatorDecorators.push(new OperatorDecorator('someValue', (factValue, jsonValue, next) => jsonValue.some(jv => next(factValue, jv)))) | ||
OperatorDecorators.push(new OperatorDecorator('everyFact', (factValue, jsonValue, next) => factValue.every(fv => next(fv, jsonValue)), Array.isArray)) | ||
OperatorDecorators.push(new OperatorDecorator('everyValue', (factValue, jsonValue, next) => jsonValue.every(jv => next(factValue, jv)))) | ||
OperatorDecorators.push(new OperatorDecorator('swap', (factValue, jsonValue, next) => next(jsonValue, factValue))) | ||
OperatorDecorators.push(new OperatorDecorator('not', (factValue, jsonValue, next) => !next(factValue, jsonValue))) | ||
|
||
export default OperatorDecorators |