Use natural language to create rules for processing business logic
Assuming the data set is:
{
"testKey": "bar",
"fooKey": null
}
const { validate } = require("business-rules-processor");
const valid = validate('{testKey} is any ("foo", "bar")', data);
console.log(valid) // true
const { parseRule, validate } = require("business-rules-processor");
const rules = parseRule('{fooKey} is not empty')
const valid = validate(rules, data);
console.log(valid) // false
The structure of a rule is always:
<Key> <operator> [value]
Rules may be combined using and
/or
, and may be
grouped using parentheses:
(<Key> <operator> <value> and <Key> <operator> <value>) or ...
Certain operators do not require [value]
.
-
Key
: a string surrounded by single curly brances:{foo}
- Key may use dot notation
- The value will be extracted from the data set
-
Value
: May beboolean
,string
,number
,null
or anotherKey
-
eq
: "equal to"neq
: "not equal to"-
Only accepts a single
Value
{foo} eq 1
{foo} neq "bar"
-
-
gte
: "Greater than or equal to"gt
: "Greater than"lte
: "Less than or equal to"lt
: "Less than"-
Only accepts a single
Value
{foo} gt 1
{foo} lte {bar}
etc...
-
-
between
,not between
Validates that the
Key
falls between twoValue
, inclusively.-
Accepts a tuple
Value
:(<Value>, <Value>)
Does check strings:
{foo} between ("bar", "baz")
more commonly used with numbers:
{foo} between (1, 100)
-
-
any
,not any
Checks the
Key
value against allValue
in a list-
Accepts a list of
Value
:(<Value>, <Value>, <Value>, ...)
. -
Case insensitive.
{foo} not any ("bar", "baz", "zap")
-
-
matches
,imatches
,not matches
,not imatches
Checks to see if
Key
matches a substring ofValue
-
Accepts a single
Value
-
imatches
is case insensitive{foo} matches "foobar"
-
-
is empty
,is not empty
Verifies that the provided
Key
is present in the data set (or not)-
Accepts no
Value
-
null
values are considered empty{foo} is empty
-
- Use
run.js
to validate a data set against a rule
-
Using the default testing data set
> node ./run.js '{asdf} is not empty' Dataset satisfies the rule: false > node ./run.js '{asdf} is empty' Dataset satisfies the rule: true
-
Provide your own dataset
Some data set I've stored in the local directory
{ "testKey": "Some value for testing" }
# Resolves data file relative to `process.cwd()` > node ./run.js -d "./test-data.json" '{testKey} imatches "some value"' Dataset satisfies the rule: true > node ./run.js -d "./test-data.json" '{testKey} not imatches "some value"' Dataset satisfies the rule: false