New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

json-rules-engine-schema-operator

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-rules-engine-schema-operator

An operator for json-rules-engine to evaluate a value against a JSON schema

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

json-rules-engine-schema-operator

npm version codecov Build Status Bundle Phobia

What's This?

It's an operator to evaluate a value against a JSON Schema for the totally awesome json-rules-engine package.

Why?

Because a JSON Schema is a predicate. A value either validates against a schema, or it doesn't. Rather than writing (and maintaining) a bunch of custom operators and bloating your codebase with them, you only need one operator - a schema operator.

Installation

npm install json-rules-engine-schema-operator
# or
yarn add json-rules-engine-schema-operator

Usage

This package is BYOV - bring your own validator (highly recommend AJV!!)

import Ajv from 'ajv';
import { Engine } from 'json-rules-engine';
import createSchemaOperator from 'json-rules-engine-schema-operator';

const ajv = new Ajv();
const validator = (subject,schema) => ajv.validate(schema,subject);

const engine = new Engine();
engine.addOperator(createSchemaOperator(validator));

and now you can do this:

engine.addRule({
  conditions: {
    any: [
      {
        fact: 'firstName',
        operator: 'schema',
        value: {
          type: 'string',
          pattern: '^J',
        },
      },
    ],
  },
  event: {
    type: 'j_firstName',
  },
});

Custom Operator Name

By default, the name of the operator added to the engine is schema. This is configurable by passing in a custom name via the second optional parameter options:

const name = 'jsonSchemaOperator';
engine.addOperator(validator, { name });

engine.addRule({
  conditions: {
    any: [
      {
        fact: 'firstName',
        operator: name,
        value: {
          type: 'string',
          pattern: '^J',
        },
      },
    ],
  },
  event: {
    type: 'j_firstName',
  },
});

I ❤️ JSON schema and json-rules-engine both so much, that I created a package json-schema-rules-engine that works very similarly, but it relies entirely on JSON schemas for predicates (or "operators"), which greatly simplifies the concept of the rules engine.

Keywords

FAQs

Package last updated on 10 Nov 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc