1.2.1 • Published 10 years ago
rules-runner v1.2.1
Introduction
rules-runner allows you to cleanly abstract your rules away from your application code
- run your dataset against a config JSON object
- results can modify your dataset or can return a new dataset of outcomes
- It's isomorphic and has minimal package dependencies - great for the browser and the server
Installation
npm install rules-runner
Examples
var Rules = require("rules-runner");
var config = {
"Must be 16 or older if no adult is present": {
//if ALL "tests" in the if statement match,
if: {
"person.age": {
lessThan: 16
},
"person.adultPresent": false
},
//process all of the outcomes
then: {
"person.error": "Must be 16 or older if no adult is present",
"errors.all[]": "person"
}
},
"Must be employed": {
if: {
"company.isEmployed": false
},
then: {
"company.error": "Must be employed",
"errors.all[]": "company" //add [] to the end of a key to push values onto an array
}
};
};
var data = {
person: {
age: 15,
adultPresent: false
},
company: {
isEmployed: false
}
};
var rules = new Rules(config);
rules.run(data);
assert.equal(data.person.error, "Must be 16 or older if no adult is present");
assert.equal(data.company.error, "Must be employed");
assert.deepEqual(data.errors.all, ["person", "company"]);
//by default, rules modify data object
//only want results leaving data unchanged?
//var results = rules.run(data, {rulesModifyData: false});
##Use an array of if
statements to treat conditions as else if
or OR like
var config = {
"Person will be in house if person is tired or hungry": {
if: [
{"person.tired": true}, //if this matches
{"person.hungry": true} //OR if this matches
],
then: {
"person.location": "house" //then run this
}
}
};
var data = {
person: {
tired: false,
hungry: true
}
};
var rules = new Rules(config);
rules.run(data);
assert.equal(data.person.location, 'house');
##otherwise
will process if no conditions match
var config = {
"Person will be in house if person is tired or hungry": {
if: [
{"person.tired": true},
{"person.hungry": true}
],
then: {
"person.location": "house"
},
otherwise: { // if all conditions are false
"person.location": 'work'
}
}
};
var data = {
person: {
tired: false,
hungry: false
}
};
var rules = new Rules(config);
rules.run(data);
assert.equal(data.person.location, 'work');
Comparators/Tests
- between:
"person.age": {between: [1, 20]}
- equality/scalar values:
"person.exists": true
"person.firstName": "John"
- contains: `"person.name": {contains: "Jr"}
(also checks for values in arrays)
- greaterThan:
"person.age": {greaterThan: 20}
- in:
"person.state": {in: ["CA", "TX", "NY"]}
- lessThan:
"person.age": {lessThan: 21}
- matches:
"person.name": {matches: "/(john|bob|mary)/i"i
- not:
"person.state": {not: "CA"}
,"person.state": {not: {in: ["CA", "TX"]}}
Options
- caseSensitive
- default:
true
contains
andequals
ignore case
- default:
- rulesModifyData
- default:
true
- matching rules modify original data set
rules.run()
returns modified dataset - when
false
, rules create a new object, which gets returned
- default:
- strict
- default:
false
- useful for debugging. when
true
, if a rule path (i.e.if: "person.age"
) isn't found in data, an error is thrown - when
false
, a rule path that isn't set in data evaluates toundefined
- default:
- stringNumbers:
- default:
true
greaterThan
,lessThan
, andbetween
comparators will parse numbers.in
will match with==
instead of===
- default:
1.2.1
10 years ago
1.2.0
10 years ago
1.1.3
10 years ago
1.1.2
10 years ago
1.1.1
10 years ago
1.1.0
10 years ago
1.0.13
10 years ago
1.0.12
10 years ago
1.0.11
10 years ago
1.0.10
10 years ago
1.0.9
10 years ago
1.0.8
10 years ago
1.0.6
10 years ago
1.0.5
10 years ago
1.0.4
10 years ago
1.0.3
10 years ago
1.0.2
10 years ago
1.0.1
10 years ago
1.0.0
10 years ago