Rule Engine
var rule = new Rule({
conditions: {
any: [{
all: [{
lhs: ['student.age'],
operator: 'equal',
rhs: 40
}, {
lhs: ['student.name'],
operator: 'equal',
rhs: ['user.fullname']
}
]
}, {
forEach: {
array: 'student.subjects',
as: 'subject'
all: [{
lhs: ['subject.mark'],
operator: 'equal',
rhs: 48
}, {
lhs: ['subject.name'],
operator: 'greaterThanInclusive',
rhs: 'maths'
}
]
}
}
]
}
});
rule.run({
student: xyzStudent,
user: xyzUser
}).success(function(){
// do something
}).fail(function(){
// do something else
});
Operators
Each rule condition must begin with a boolean operator(all
or any
) at its root.
The operator
compares the value returned by the fact
to what is stored in the value
property. If the result is truthy, the condition passes.
String and Numeric operators:
equal
- fact must equal value
notEqual
- fact must not equal value
these operators use strict equality (===) and inequality (!==)
String operators:
match
- fact must match value (regex)
Numeric operators:
lessThan
- fact must be less than value
lessThanInclusive
- fact must be less than or equal to value
greaterThan
- fact must be greater than value
greaterThanInclusive
- fact must be greater than or equal to value
Array operators:
in
- fact must be included in value (an array)
notIn
- fact must not be included in value (an array)
contains
- fact (an array) must include value
doesNotContain
- fact (an array) must not include value