Skip to content

Commit 3555909

Browse files
committed
Fixing example with nested array
1 parent 6449c59 commit 3555909

File tree

3 files changed

+102
-105
lines changed

3 files changed

+102
-105
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,7 @@ Let's say we need to require `state`, when `work` has a `name` `congressman`, th
387387
```js
388388
let rules = [{
389389
conditions: {
390-
"work.name": {
391-
name: { equals: "congressman" },
392-
}
390+
"work.name": { is: "congressman" }
393391
},
394392
event: {
395393
type: "require",
@@ -409,7 +407,7 @@ This can be expressed like this:
409407
let rules = [{
410408
conditions: {
411409
hobbies: {
412-
name: { equals: "baseball" },
410+
name: { is: "baseball" },
413411
}
414412
},
415413
event: {

src/conditionsMeet.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ export default function conditionsMeet(conditions, formData) {
88
toError(`Rule ${conditions} with ${formData} can't be processed`);
99
}
1010
return Object.keys(conditions).every(ref => {
11-
let refRule = conditions[ref];
11+
let refCondition = conditions[ref];
1212
if (ref === OR) {
13-
return refRule.some(rule => conditionsMeet(rule, formData));
13+
return refCondition.some(rule => conditionsMeet(rule, formData));
1414
} else if (ref === AND) {
15-
return refRule.every(rule => conditionsMeet(rule, formData));
15+
return refCondition.every(rule => conditionsMeet(rule, formData));
1616
} else if (ref === NOT) {
17-
return !conditionsMeet(refRule, formData);
17+
return !conditionsMeet(refCondition, formData);
1818
} else {
1919
let refVal = selectn(ref, formData);
2020
if (Array.isArray(refVal)) {
21-
return refVal.some(val => conditionsMeet(refRule, val));
21+
return refVal.some(val => conditionsMeet(refCondition, val));
2222
} else {
23-
return checkField(refVal, refRule);
23+
return checkField(refVal, refCondition);
2424
}
2525
}
2626
});

test/documentationExamples.test.js

+94-95
Original file line numberDiff line numberDiff line change
@@ -258,107 +258,106 @@ test("multi field OR", () => {
258258
]);
259259
});
260260

261-
/**
262-
#### OR
263-
264-
In addition to previous rule we need `bio`, if `state` is `NY`.
265-
266-
```js
267-
let rules = [{
268-
conditions: {
269-
or: [
270-
{
271-
age: { less : 70 },
272-
country: { is: "USA" }
273-
},
274-
{
275-
state: { is: "NY"}
276-
}
277-
]
278-
},
279-
event: {
280-
type: "require",
281-
params: { fields: [ "bio" ]}
282-
}
283-
}]
284-
```
285-
286-
#### NOT
287-
288-
When we don't require `bio` we need `zip` code.
289-
290-
```js
291-
let rules = [{
292-
conditions: {
293-
not: {
294-
or: [
295-
{
296-
age: { less : 70 },
297-
country: { is: "USA" }
261+
test("multi field NOT", () => {
262+
let rules = [
263+
{
264+
conditions: {
265+
not: {
266+
or: [
267+
{
268+
age: { less: 70 },
269+
country: { is: "USA" },
270+
},
271+
{
272+
state: { is: "NY" },
273+
},
274+
],
298275
},
299-
{
300-
state: { is: "NY"}
301-
}
302-
]
303-
}
304-
},
305-
event: {
306-
type: "require",
307-
params: { fields: [ "zip" ]}
308-
}
309-
}]
310-
```
311-
312-
### Nested object queries
313-
314-
Rules engine supports querying inside nested objects, with [selectn](https://github.com/wilmoore/selectn.js),
315-
any data query that works in [selectn](https://github.com/wilmoore/selectn.js), will work in here
316-
317-
Let's say we need to require `state`, when `work` has a `name` `congressman`, this is how we can do this:
318-
319-
```js
320-
let rules = [{
321-
conditions: {
322-
"work.name": {
323-
name: { equals: "congressman" },
324-
}
325-
},
326-
event: {
327-
type: "require",
328-
params: { fields: [ "state" ]}
329-
}
330-
}]
331-
```
332-
333-
### Nested arrays object queries
276+
},
277+
event: EVENT,
278+
},
279+
];
334280

335-
Sometimes we need to make changes to the form if some nested condition is true.
281+
let engine = new Engine(rules, schema);
282+
expect.assertions(5);
336283

337-
For example if one of the `hobbies` is `baseball`, we need to make `state` `required`.
338-
This can be expressed like this:
284+
return Promise.all([
285+
engine
286+
.run({ age: 16, country: "China", state: "Beijing" })
287+
.then(res => expect(res).toEqual([EVENT])),
288+
engine
289+
.run({ age: 16, country: "China", state: "NY" })
290+
.then(res => expect(res).toEqual([])),
291+
engine
292+
.run({ age: 16, country: "USA" })
293+
.then(res => expect(res).toEqual([])),
294+
engine.run({ age: 80, state: "NY" }).then(res => expect(res).toEqual([])),
295+
engine
296+
.run({ age: 69, country: "USA" })
297+
.then(res => expect(res).toEqual([])),
298+
]);
299+
});
339300

340-
```js
341-
let rules = [{
342-
conditions: {
343-
hobbies: {
344-
name: { equals: "baseball" },
345-
}
346-
},
347-
event: {
348-
type: "require",
349-
params: { fields: [ "state" ]}
350-
}
351-
}]
352-
```
301+
test("Nested object queries", () => {
302+
let rules = [
303+
{
304+
conditions: {
305+
"work.name": { is: "congressman" },
306+
},
307+
event: EVENT,
308+
},
309+
];
353310

354-
Rules engine will go through all the elements in the array and trigger `require` if `any` of the elements meet the criteria
311+
let engine = new Engine(rules, schema);
312+
expect.assertions(5);
355313

356-
## Support
314+
return Promise.all([
315+
engine.run({ work: {} }).then(res => expect(res).toEqual([])),
316+
engine.run({}).then(res => expect(res).toEqual([])),
317+
engine
318+
.run({ work: { name: "congressman" } })
319+
.then(res => expect(res).toEqual([EVENT])),
320+
engine
321+
.run({ work: { name: "president" } })
322+
.then(res => expect(res).toEqual([])),
323+
engine
324+
.run({ work: { name: "blacksmith" } })
325+
.then(res => expect(res).toEqual([])),
326+
]);
327+
});
357328

358-
If you are having issues, please let us know.
359-
We have a mailing list located at: ...
329+
test("Nested arrays object queries", () => {
330+
let rules = [
331+
{
332+
conditions: {
333+
hobbies: {
334+
name: { is: "baseball" },
335+
},
336+
},
337+
event: EVENT,
338+
},
339+
];
360340

361-
## License
341+
let engine = new Engine(rules, schema);
342+
expect.assertions(5);
362343

363-
The project is licensed under the Apache Licence 2.0.
364-
**/
344+
return Promise.all([
345+
engine.run({ hobbies: [] }).then(res => expect(res).toEqual([])),
346+
engine.run({}).then(res => expect(res).toEqual([])),
347+
engine
348+
.run({ hobbies: [{ name: "baseball" }] })
349+
.then(res => expect(res).toEqual([EVENT])),
350+
engine
351+
.run({
352+
hobbies: [
353+
{ name: "reading" },
354+
{ name: "jumping" },
355+
{ name: "baseball" },
356+
],
357+
})
358+
.then(res => expect(res).toEqual([EVENT])),
359+
engine
360+
.run({ hobbies: [{ name: "reading" }, { name: "jumping" }] })
361+
.then(res => expect(res).toEqual([])),
362+
]);
363+
});

0 commit comments

Comments
 (0)