-
Notifications
You must be signed in to change notification settings - Fork 493
/
Copy path13-using-operator-decorators.mts
105 lines (91 loc) · 2.44 KB
/
13-using-operator-decorators.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* 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
*/
import "colors";
import { Engine } from "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: { tags: string[] };
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: string, jsonValue: string, 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);
// third run with a tag that is valid if case insensitive
facts = { tags: ["dev", "PROD"] };
await engine.run(facts);
}
export default 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)
*/