Coder Social home page Coder Social logo

json-rules-engine's Introduction

JSON Rule Engine

What does it do?

Pass a Rule and Data to engine and provide synchronous response on whether the Data satisfies the Rules.

@aipeel/json-rules-engine

Installation

    npm install @aipeel/json-rules-engine

Usage

Step 1: Define Rules JSON

  1. Rules JSON must be an object

  2. Must start with AND or OR which takes array of rule

    1. Single Rule Criteria
    {
        AND: [ Criteria1 ]
    }
    
    1. Multiple Rule Criteria --> Criteria 1 and 2 must be satisfied
    {
        AND: [ Criteria1, Criteria2 .. ]
    }
    
    1. Multiple Levels of Rules --> Criteria 1 & 2 must be satisfied and either Criteria3 or Criteria4 must be satisfied
    {
        AND: [
                {
                    AND: [ Criteria1, Criteria2 ...]
                },
                {
                    OR: [Criteria3, Criteria4 ..]
                }
            ]
    }
    
  3. Each Criteria must be an object

    1. The key in criteria is an operator and value defines the metadata required for the operator to work

      Examples of Criteria

      { 
          "between": { 
                          "min": "10", 
                          "max": "20", 
                          "attribute": "discount" 
                      }
      }
      { 
          "in": { 
                  "value": ["C","Javascript","Python"], 
                  "attribute": "lang"
              }
      }
    2. List of all criteria given below

      Operator Operator Property Description Example
      between min, max, attribute Check if the property mentioned in attribute has value between min and max { "between" : { "min" : "10" , "max" : "20" , "attribute" : "discount" }}
      lessThan value, attribute Check if the property mentioned in attribute has number less than value { "lessThan" : { "value" : "80" , "attribute" : "price" } }
      lessThanEqual value, attribute Check if the property mentioned in attribute has number less than or equal value { "lessThanEqual" : { "value" : "80" , "attribute" : "price" } }
      greaterThan value, attribute Check if the property mentioned in attribute has number greater than value { "greaterThan" : { "value" : "80" , "attribute" : "price" } }
      greaterThanEqual value, attribute Check if the property mentioned in attribute has number greater than or equal value { "greaterThanEqual" : { "value" : "80" , "attribute" : "price" } }
      equal value, attribute Check if the property mentioned in attribute has value equal to value provided { "equal" : { "value" : "80" , "attribute" : "price" } }
      contains value, attribute Check if the property mentioned in attribute contains the value given. { "contains" : { "value" : "great" , "attribute" : "desc" }}
      in value, attribute Check if the property mentioned in attribute is available in value array provided. { "in" : { "value" : [ "C" , "Javascript" , "Python" ], "attribute" : "lang" }}
    3. A Rules JSON could be defined as

    { 
        "AND": [
            { "lessThan": {"value": "80", "attribute": "price"} },
            { "greaterThanEqual": {"value": "20", "attribute": "price"} },
            { "between": { "min": "10", "max": "20", "attribute": "discount" }}
        ]
    }
    {
        "OR":[
            { "contains": { "value": "great", "attribute": "desc" }},
            { "contains": { "value": "works", "attribute": "features"}},
            { "in": { "value": ["C","Javascript","Python"], "attribute": "lang"}},
        ]
    }
  4. Rules can be recursively nested as shown below

    {
        "AND": [
                { 
                    "AND": [
                        { "lessThan": {"value": "80", "attribute": "price"} },
                        { "greaterThanEqual": {"value": "20", "attribute": "price"} },
                        { "between": { "min": "10", "max": "20", "attribute": "discount" }}
                    ]
                },
                {
                    "OR":[
                        { "contains": { "value": "great", "attribute": "desc" }},
                        { "contains": { "value": "works", "attribute": "features"}},
                        { "in": { "value": ["C","Javascript","Python"], "attribute": "lang"}},
                    ]
                }
            ]
    }

Step2: Pass Data JSON and Rule JSON to Engine

    const Engine = require("@aipeel/json-rules-engine")

    Engine.apply(data,rule) // returns boolean - true or false

Complete end-to-end example below for reference

const Engine = require("@aipeel/json-rules-engine")

const rule = {
                    "AND": [
                            { 
                                "AND": [
                                    { "lessThan": {"value": "80", "attribute": "price"} },
                                    { "greaterThanEqual": {"value": "20", "attribute": "price"} },
                                    { "between": { "min": "10", "max": "20", "attribute": "discount" }}
                                ]
                            },
                            {
                                "OR":[
                                    { "contains": { "value": "great", "attribute": "desc" }},
                                    { "contains": { "value": "works", "attribute": "features"}},
                                    { "in": { "value": ["C","Javascript","Python"], "attribute": "lang"}},
                                ]
                            }
                        ]
                    }

let data = {
    id: 'ID1001',
    price: 50,
    discount: 10,
    desc: "This is a great product for developers",
    features: ["No Dependencies", "Open to Extension", "Synchronous", "works"],
    tags: ["json", "rule", "engine"],
    lang: "Javascript"
}

// example 1
console.log(`Does the data conform to rule? --> ${Engine.apply(data,rule)?"Yes":"No"}`);

//example 2
data.price = 100;
console.log(`Does the data conform to rule? --> ${Engine.apply(data,rule)?"Yes":"No"}`);

//example 3
data.price = 50;
data.discount = 21;
console.log(`Does the data conform to rule? --> ${Engine.apply(data,rule)?"Yes":"No"}`);

Output

Does the data conform to rule? --> Yes
Does the data conform to rule? --> No
Does the data conform to rule? --> No

json-rules-engine's People

Contributors

murlidharvarma avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.