json-rule-evaluator
Demo at: https://gifted-mcclintock-348e6f.netlify.app/
Installation
npm i -S json-rule-evaluator
Usage
import RuleEvaluator, { Rule } from 'json-rule-evaluator';
const sampleData = {
some: {
boolean: true,
number: 12,
string: 'abc',
},
other: {
boolean: false,
number: 2,
string: 'xyz',
},
};
const testRule: Rule = {
$and: [
{
path: 'some.number',
value: 12,
operator: 'gt',
},
{
path: 'some.boolean',
value: true,
operator: '==',
},
{
$or: [
{
path: 'some.string',
ref: 'other.string',
operator: '!=',
options: {
caseSensitive: false,
},
},
{
path: 'some.boolean',
ref: 'other.boolean',
operator: '==',
},
],
},
],
};
const evaluator = new RuleEvaluator(testRule);
const processed = evaluator.test(sampleData);
console.log(processed);
Available Operators
Boolean
Number
- ==
- !=
- lt (lesser than)
- gt (greater than)
- lte (lesser than or equal)
- gte (greater than or equal)
String
- ==
- !=
- contains
- startsWith
- endsWith
- doesntContain
- doesntStartWith
- doesntEndWith