Skip to content

Commit

Permalink
Fixing example with nested array
Browse files Browse the repository at this point in the history
  • Loading branch information
mavarazy committed Jul 10, 2017
1 parent 6449c59 commit 3555909
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 105 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,7 @@ Let's say we need to require `state`, when `work` has a `name` `congressman`, th
```js
let rules = [{
conditions: {
"work.name": {
name: { equals: "congressman" },
}
"work.name": { is: "congressman" }
},
event: {
type: "require",
Expand All @@ -409,7 +407,7 @@ This can be expressed like this:
let rules = [{
conditions: {
hobbies: {
name: { equals: "baseball" },
name: { is: "baseball" },
}
},
event: {
Expand Down
12 changes: 6 additions & 6 deletions src/conditionsMeet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ export default function conditionsMeet(conditions, formData) {
toError(`Rule ${conditions} with ${formData} can't be processed`);
}
return Object.keys(conditions).every(ref => {
let refRule = conditions[ref];
let refCondition = conditions[ref];
if (ref === OR) {
return refRule.some(rule => conditionsMeet(rule, formData));
return refCondition.some(rule => conditionsMeet(rule, formData));
} else if (ref === AND) {
return refRule.every(rule => conditionsMeet(rule, formData));
return refCondition.every(rule => conditionsMeet(rule, formData));
} else if (ref === NOT) {
return !conditionsMeet(refRule, formData);
return !conditionsMeet(refCondition, formData);
} else {
let refVal = selectn(ref, formData);
if (Array.isArray(refVal)) {
return refVal.some(val => conditionsMeet(refRule, val));
return refVal.some(val => conditionsMeet(refCondition, val));
} else {
return checkField(refVal, refRule);
return checkField(refVal, refCondition);
}
}
});
Expand Down
189 changes: 94 additions & 95 deletions test/documentationExamples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,107 +258,106 @@ test("multi field OR", () => {
]);
});

/**
#### OR
In addition to previous rule we need `bio`, if `state` is `NY`.
```js
let rules = [{
conditions: {
or: [
{
age: { less : 70 },
country: { is: "USA" }
},
{
state: { is: "NY"}
}
]
},
event: {
type: "require",
params: { fields: [ "bio" ]}
}
}]
```
#### NOT
When we don't require `bio` we need `zip` code.
```js
let rules = [{
conditions: {
not: {
or: [
{
age: { less : 70 },
country: { is: "USA" }
test("multi field NOT", () => {
let rules = [
{
conditions: {
not: {
or: [
{
age: { less: 70 },
country: { is: "USA" },
},
{
state: { is: "NY" },
},
],
},
{
state: { is: "NY"}
}
]
}
},
event: {
type: "require",
params: { fields: [ "zip" ]}
}
}]
```
### Nested object queries
Rules engine supports querying inside nested objects, with [selectn](https://github.com/wilmoore/selectn.js),
any data query that works in [selectn](https://github.com/wilmoore/selectn.js), will work in here
Let's say we need to require `state`, when `work` has a `name` `congressman`, this is how we can do this:
```js
let rules = [{
conditions: {
"work.name": {
name: { equals: "congressman" },
}
},
event: {
type: "require",
params: { fields: [ "state" ]}
}
}]
```
### Nested arrays object queries
},
event: EVENT,
},
];

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

For example if one of the `hobbies` is `baseball`, we need to make `state` `required`.
This can be expressed like this:
return Promise.all([
engine
.run({ age: 16, country: "China", state: "Beijing" })
.then(res => expect(res).toEqual([EVENT])),
engine
.run({ age: 16, country: "China", state: "NY" })
.then(res => expect(res).toEqual([])),
engine
.run({ age: 16, country: "USA" })
.then(res => expect(res).toEqual([])),
engine.run({ age: 80, state: "NY" }).then(res => expect(res).toEqual([])),
engine
.run({ age: 69, country: "USA" })
.then(res => expect(res).toEqual([])),
]);
});

```js
let rules = [{
conditions: {
hobbies: {
name: { equals: "baseball" },
}
},
event: {
type: "require",
params: { fields: [ "state" ]}
}
}]
```
test("Nested object queries", () => {
let rules = [
{
conditions: {
"work.name": { is: "congressman" },
},
event: EVENT,
},
];

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

## Support
return Promise.all([
engine.run({ work: {} }).then(res => expect(res).toEqual([])),
engine.run({}).then(res => expect(res).toEqual([])),
engine
.run({ work: { name: "congressman" } })
.then(res => expect(res).toEqual([EVENT])),
engine
.run({ work: { name: "president" } })
.then(res => expect(res).toEqual([])),
engine
.run({ work: { name: "blacksmith" } })
.then(res => expect(res).toEqual([])),
]);
});

If you are having issues, please let us know.
We have a mailing list located at: ...
test("Nested arrays object queries", () => {
let rules = [
{
conditions: {
hobbies: {
name: { is: "baseball" },
},
},
event: EVENT,
},
];

## License
let engine = new Engine(rules, schema);
expect.assertions(5);

The project is licensed under the Apache Licence 2.0.
**/
return Promise.all([
engine.run({ hobbies: [] }).then(res => expect(res).toEqual([])),
engine.run({}).then(res => expect(res).toEqual([])),
engine
.run({ hobbies: [{ name: "baseball" }] })
.then(res => expect(res).toEqual([EVENT])),
engine
.run({
hobbies: [
{ name: "reading" },
{ name: "jumping" },
{ name: "baseball" },
],
})
.then(res => expect(res).toEqual([EVENT])),
engine
.run({ hobbies: [{ name: "reading" }, { name: "jumping" }] })
.then(res => expect(res).toEqual([])),
]);
});

0 comments on commit 3555909

Please sign in to comment.