Skip to content

Commit 02cea19

Browse files
committed
fix #7
1 parent 6bab1e4 commit 02cea19

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,34 @@ let rules = [{
422422

423423
Rules engine will go through all the elements in the array and trigger `require` if `any` of the elements meet the criteria.
424424

425+
### Extending available predicates
426+
427+
If for some reason the list of [predicates](https://github.com/landau/predicate) is insufficient for your needs, you can extend them pretty easy,
428+
by specifying additional predicates in global import object.
429+
430+
For example, if we want to add `range` predicate, that would verify, that integer value is in range, we can do it like this:
431+
```js
432+
import predicate from "predicate";
433+
import Engine from "json-rules-engine-simplified";
434+
435+
predicate.range = predicate.curry((val, range) => {
436+
return predicate.num(val) &&
437+
predicate.array(range) &&
438+
predicate.equal(range.length, 2) &&
439+
predicate.num(range[0]) &&
440+
predicate.num(range[1]) &&
441+
predicate.greaterEq(val, range[0]) &&
442+
predicate.lessEq(val, range[1]);
443+
});
444+
445+
let engine = new Engine([{
446+
conditions: { age: { range: [ 20, 40 ] } },
447+
event: "hit"
448+
}]);
449+
```
450+
451+
Validation will automatically catch new extension and work as expected.
452+
425453
## Events
426454

427455
Framework does not put any restrictions on event object, that will be triggered, in case conditions are meet

src/conditionsMeet.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import selectn from "selectn";
55

66
export default function conditionsMeet(conditions, formData) {
77
if (!isObject(conditions) || !isObject(formData)) {
8-
toError(`Rule ${conditions} with ${formData} can't be processed`);
8+
toError(
9+
`Rule ${JSON.stringify(conditions)} with ${formData} can't be processed`
10+
);
911
}
1012
return Object.keys(conditions).every(ref => {
1113
let refCondition = conditions[ref];

test/predicate.test.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const predicate = require("predicate");
1+
import predicate from "predicate";
2+
import Engine from "../src/Engine";
23

34
test("equal work with same strings", function() {
45
expect(predicate.eq("Will", "Will")).toBeTruthy();
@@ -14,3 +15,34 @@ test("work with empty", function() {
1415
test("and is not predicate", () => {
1516
expect(predicate.and).toBeUndefined();
1617
});
18+
19+
predicate.range = predicate.curry((val, range) => {
20+
return (
21+
predicate.num(val) &&
22+
predicate.array(range) &&
23+
predicate.equal(range.length, 2) &&
24+
predicate.greaterEq(val, range[0]) &&
25+
predicate.lessEq(val, range[1])
26+
);
27+
});
28+
29+
let engine = new Engine([
30+
{
31+
conditions: { age: { range: [20, 40] } },
32+
event: "hit",
33+
},
34+
]);
35+
36+
test("not in range left", () => {
37+
return engine.run({ age: 10 }).then(events => expect(events).toEqual([]));
38+
});
39+
40+
test("in range", () => {
41+
return engine
42+
.run({ age: 30 })
43+
.then(events => expect(events).toEqual(["hit"]));
44+
});
45+
46+
test("not in range right", () => {
47+
return engine.run({ age: 50 }).then(events => expect(events).toEqual([]));
48+
});

test/validation.predicates.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import predicate from "predicate";
2+
import { listInvalidPredicates } from "../src/validation";
3+
4+
let schema = {
5+
type: "object",
6+
properties: {
7+
firstName: { type: "string" },
8+
},
9+
};
10+
11+
test("Check predicates", () => {
12+
const conditions = [{ firstName: "somePredicate" }];
13+
14+
expect(listInvalidPredicates(conditions, schema)).toEqual(["somePredicate"]);
15+
16+
predicate.somePredicate = function() {
17+
return false;
18+
};
19+
20+
expect(listInvalidPredicates(conditions, schema)).toEqual([]);
21+
});

0 commit comments

Comments
 (0)