Skip to content

A small, fast, safe, 0 dependencies, JSON rules engine

License

Notifications You must be signed in to change notification settings

webstacker/json-rules

Repository files navigation

JSON Rules

A small, fast, safe, 0 dependencies, rules engine.

Serialize rules in JSON format.

Rule Syntax

[operator, value1, value2, ...]

Usage

const jr = require("json-rules")();

Simple

jr.evaluate([">", 2, 1]); // true (2 > 1)
jr.evaluate(["+", 2, 1]); // 3 (2 + 1)
jr.evaluate(["==", "foo", "bar"]); // false (foo === bar)
jr.evaluate(["!=", "foo", "bar"]); // true (foo !== bar) 

Nested rules

jr.evaluate([">",
    2,
    ["+", 2, 1]
]); // false (2 > 3)
jr.evaluate(["and",
    ["==", 1, 1],
    [">", 2, 1]
]); // true (1 == 1 && 2 > 1)
const data = {
    "type": "user"
    "sub-type": "admin"
}

jr.evaluate(["and",
    ["==", "$.type", "user"],
    ["or",
        ["==", "$.sub-type", "admin"],
        ["==", "$.sub-type", "super"]
    ]
], data); // true (type === user && (sub-type === admin || sub-type === super))

Data reference

const data = {
    "foo": "foo",
    "bar": "bar"
};

jr.evaluate(["!=", "$.foo", "$.baz"], data); // true (foo != baz)
const data = {
    "baz": ["b", "a", "z"]
};

jr.evaluate(["==", "$.baz.1", "a"], data); // true (a == a)
const data = {
    "foo": {
        "bar": {
            "baz": ["b", "a", "z"]
        }
    }
};

jr.evaluate(["==", "$.foo.bar.baz.1", "a"], data); // true (a == a)

Add operator

function eq(x, y) {
    // ['break;', false] instructs the loop to break and return false, this
    // saves testing any further values as they must all be equal to pass
    return x === y || ['break;', false];  
}

jr.addOperator('equals', eq);

jr.evaluate(['equals', 1, 1]); // true (1 === 1) 

Alias operator

jr.addAlias('>=', 'gte');

jr.evaluate('gte', 2, 2); // true (2 >= 2)

API

jr.evaluate(rule, data)

rule

Type: Array

The array will be treated as a rule if the first element contains a known operator, otherwise it's treated as a standard array.

data

Type: Object

This can be referenced within rules using dot notation.

jr.addOperator(operator, fn)

operator

Type: string

Operator name e.g. equals.

fn

Type: function

TODO

License

MIT

About

A small, fast, safe, 0 dependencies, JSON rules engine

Resources

License

Stars

Watchers

Forks

Packages

No packages published