Socket
Book a DemoInstallSign in
Socket

json-rule-evaluator

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-rule-evaluator

---

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
15
200%
Maintainers
0
Weekly downloads
 
Created
Source

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);
// Output:
// {
//   result: false,
//   details: {
//     operator: "$and",
//     status: "false",
//     children: [{
//       rule: {
//         path: "some.number",
//         value: 12,
//         operator: "gt"
//       },
//       status: "false"
//     }, {
//       rule: {
//         path: "some.boolean",
//         value: true,
//         operator: "=="
//       },
//       status: "not_evaluated"
//     }, {
//       operator: "$or",
//       status: "not_evaluated",
//       children: [{
//         rule: {
//           path: "some.string",
//           ref: "other.string",
//           operator: "!=",
//           options: {
//             caseSensitive: false
//           }
//         },
//         status: "true"
//       },
//       {
//         rule: {
//           path: "some.boolean",
//           ref: "other.boolean",
//           operator: "=="
//         },
//         status: "not_evaluated"
//       }]
//     }]
//   }
// }

Available Operators

Boolean
  • ==
  • !=
  • and
  • or
Number
  • ==
  • !=
  • lt (lesser than)
  • gt (greater than)
  • lte (lesser than or equal)
  • gte (greater than or equal)
String
  • ==
  • !=
  • contains
  • startsWith
  • endsWith
  • doesntContain
  • doesntStartWith
  • doesntEndWith

FAQs

Package last updated on 11 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts