npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

standard-rule-engine

v0.3.0

Published

A simple rule engine that uses Standard Schema to validate facts

Downloads

13

Readme

standard-rule-engine

A work in progress

A rule engine that uses the standard schema to evaluate rule facts.

The API is very much inspired by Elysia

Defining an Engine

Engines are modular building blocks that can be combined and chained, allowing you to break down complex functionality into smaller, reusable components.

import { Engine } from "standard-rule-engine";

// We can't do anything with this yet
const engine = new Engine();

Creating a rule

For our engine to actually do anything, we need to create rules that will be run. By default, all rules are run for any given set of facts.

[!IMPORTANT]
Notice the user of method chaining in the example below. This is crucial to allow for correct type inferencing throughout the library.

import { Engine } from "standard-rule-engine";

const engine = new Engine().rule("ruleName", (facts) => {
  console.log("I will be run for every set of facts", facts);
});

Defining rule facts

We can define a custom schema for a rule's facts so that the rule is only run when the facts match that schema. Facts are readonly and cannot be mutated.

[!NOTE]
Any validation library that implements the Standard Schema is supported. (zod, arktype, valibot, etc)

import { Engine } from "standard-rule-engine";
import { z } from "zod";

const engine = new Engine().rule(
  "ruleName",
  (facts) => {
    // facts is now strongly typed to what the `schema` is
    console.log(`The user's age is ${facts.age}`);
  },
  {
    schema: z.object({
      age: z.number(),
    }),
  },
);

Engine scoped schemas

You can call the .schema() method on an Engine class to have all rules that directly build off of that engine have their facts derrived from that schema. If a rule attempts to define its own schema when its parent engine has a scoped schema, the rule's schema must extend the engine's schema or there will be an error.

import { Engine } from "standard-rule-engine";
import { z } from "zod";

const engine = new Engine()
  .schema(z.object({ age: z.number() }))
  .rule("ruleName", (facts) => {
    // facts is now strongly typed to what the `schema` is
    console.log(`The user's age is ${facts.age}`);
  });
import { Engine } from "standard-rule-engine";
import { z } from "zod";

const engine = new Engine().schema(z.object({ age: z.number() })).rule(
  "ruleName",
  (facts) => {
    console.log(`The user's age is ${facts.age}`);
  },
  {
    schema: z.object({
      randomProperty: z.string(), // Error on this line because this schema doesn't extend the scoped schema
    }),
  },
);
import { Engine } from "standard-rule-engine";
import { z } from "zod";

const engine = new Engine().schema(z.object({ age: z.number() })).rule(
  "ruleName",
  (facts) => {
    console.log(`The user's age is ${facts.age}`);
  },
  {
    schema: z.object({
      age: z.number(),
      randomProperty: z.string(), // This one doesn't error
    }),
  },
);

Context

For our rules to actually do anything meaningful, they have to mutate context. Context is defined on the Engine using the .context() method. Context is unique between sessions, and is shared between rules run in that session.

import { Engine } from "standard-rule-engine";

const engine = new Engine()
  .context("amountOfRulesRun", 0)
  .rule("ruleName", (facts, { context }) => {
    // `context` is fully inferred. This is why method chaining is so useful
    context.amountOfRulesRun++;
  });
.context() signatures

Context can be called with either the name of the key in context and value, or by passing in an object as the first and only parameter. When an object is passed in, it is deeply merged with the current contexts.

import { Engine } from "standard-rule-engine";

const engine = new Engine()
  .context({ hello: "world" })
  .context("amountOfRulesRun", 0)
  .rule("ruleName", (facts, { context }) => {
    // `context` is fully inferred. This is why method chaining is so useful
    context.amountOfRulesRun++;
    console.log(context.hello);
  });

Helper functions

Context can hold things other than primitives, such as functions. This is useful for having helper methods that can be shared between rules.

import { Engine } from "standard-rule-engine";

const engine = new Engine()
  .context({ conditions: [] as string[] }) // Cast the array to more specific type for better inference
  .context("addCondition", (context, conditionName: string) => {
    context.conditions.push({ name: conditionName, appliedAt: new Date() });
  })
  .rule("ruleName", (facts, { context }) => {
    context.addCondition("Very bad condition");
  });

Helpers

Helpers allow you to define reusable functions that have access to the engine's context. They are defined on the engine and can be accessed in rules via the helpers object.

import { Engine } from "standard-rule-engine";

const engine = new Engine()
  .context("count", 0)
  .helper("increment", (context) => {
    context.count++;
  })
  .helper("add", (context, a: number, b: number) => {
    return a + b;
  })
  .rule("use-helper", (_, { context, helpers }) => {
    helpers.increment();
    const sum = helpers.add(5, 3);
    console.log(`Sum: ${sum}`);
  });

Helpers are particularly useful for:

  1. Reusable logic: Define common operations that can be used across multiple rules
  2. Complex calculations: Encapsulate complex logic in a helper function
  3. Shared functionality: Create utility functions that can be used by any rule

When defining a helper function, the context is always the first parameter. However, when calling a helper function in a rule, you don't need to pass the context - it's automatically injected for you.

Sessions

Sessions are similar to how they behave in NRules. They are how you actually execute rules against a set of facts.

TypeScript Playground Link

import { Engine } from "standard-rule-engine";
import { z } from "zod";

const engine = new Engine().context("isAdult", false).rule(
  "isUserAdultRule",
  (facts, { context }) => {
    context.isAdult = facts.age >= 18;
  },
  {
    schema: z.object({
      age: z.number(),
    }),
  },
);

const session = engine.createSession();

session.insert({ age: 18 }); // Insert facts to run rules against

session.fire(); // Evaluate all rules here

console.log(session.context.isAdult); // `context` is correctly typed based on the engine's definition