# jsre

> JSON based rules engine

Latest version **0.6.0** (published 2019-12-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install jsre
pnpm add jsre
yarn add jsre
bun add jsre
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2019-12-02 |
| First published | 2019-11-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12.11.0 |
| Dependencies | 3 |
| Unpacked size | 166.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Aaron Bawcom |
| Maintainers | clearly |
| Keywords | json, rules, engine |

## Links

- npm: https://www.npmjs.com/package/jsre
- Repository: https://github.com/clearly/jsre
- Homepage: https://github.com/clearly/jsre#readme
- Issues: https://github.com/clearly/jsre/issues
- npm.io page: https://npm.io/package/jsre

## Dependencies (3)

- [ajv](https://npm.io/package/ajv.md) ^6.10.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.13.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.6.0 (latest) — 2019-12-02
- 0.5.0 — 2019-12-02
- 0.4.5 — 2019-12-01
- 0.4.4 — 2019-12-01
- 0.4.3 — 2019-12-01
- 0.4.2 — 2019-12-01
- 0.4.1 — 2019-11-30
- 0.4.0 — 2019-11-30
- 0.3.0 — 2019-11-30
- 0.2.2 — 2019-11-30
- 0.2.1 — 2019-11-30
- 0.2.0 — 2019-11-30
- 0.1.0 — 2019-11-30
- 0.0.1 — 2019-11-29

## README

# Build Info
![Statements](badges/badge-statements.svg) ![Functions](badges/badge-functions.svg) ![Lines](badges/badge-lines.svg) ![Branches](badges/badge-branches.svg)

# JSon Rules Engine
There are several JSON rules engines avaialable so why build another one?
As usual, the ones out there were not really a good fit. So this package is
based on the following focus:

1. Derived facts should be computed to the document of inspection before the
business rules are run
1. Use the latest ES7 based capabilities in the implementation to eliminate
unnecessary boiler plate
1. Focus on inspecting a single large JSON object that could come back from
an API or an object moving through a streaming platform
1. Allow for the use of YAML, it is just more concise
1. Lean on `AJV` for rule specification validation
1. Lean on `lodash` for a lot of heavy lifting
1. Like other rule engines, re-use the `rules` data structure to put the results
of calculation to make it easier to diagnose failures
1. In the results returned, returned a focused list of `failures` and `passes` to simplify processing the output
1. Allow for optional dependencies to encode a hierarchy into flat lists
diagnosing errors
 
# Example

```
test('example rule pass', () => {
  const testDocument = {
    prop1 : {
      prop2 : {
        prop3 : 1
      }
    }
  };
  
  const rules = [
    {
      conditions: {
        all: [
          {
            operator: "equal",
            lhs: 1,
            rhs: {
              path: "prop1.prop2.prop3"
            }
          }
        ]
      }
    }
  ];
  
  const engine = new Engine( { rules } );
  const results = engine.run( testDocument );
  console.log( JSON.stringify( { results }, null, 2 ) );  
  expect(results).toHaveProperty('success');
  expect(results.success).toBe(true);
});

Output:
  {
    "results": {
      "rules": [
        {
          "conditions": {
            "all": [
              {
                "operator": "equal",
                "lhs": 1,
                "rhs": {
                  "path": "prop1.prop2.prop3"
                },
                "success": true,
                "calculation": {
                  "lhs": 1,
                  "rhs": 1,
                  "not": false
                }
              }
            ]
          },
          "success": true
        }
      ],
      "failures": [],
      "passes": [
        {
          "operator": "equal",
          "lhs": 1,
          "rhs": {
            "path": "prop1.prop2.prop3"
          },
          "success": true,
          "calculation": {
            "lhs": 1,
            "rhs": 1,
            "not": false
          }
        }
      ],
      "success": true
    }
  }

```

# Operators
Operators are very easy to add and are kept pluggable in a directory called
`operator`

# Requests
Requests for added functionality are welcome and will be implemented without
asking for pull requests or $ =)

# Docs
[Documentation](docs/conditions.md)

---
_Source: https://npm.io/package/jsre · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
