
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
declarative-rules
Advanced tools
A lightweight, type-safe library for managing complex conditional logic in a clean, declarative, and extensible way.
Tired of messy if/else chains? Spiraling conditional complexity got you down?
Declarative Rules offers a clean, powerful, and flexible way to manage complex business logic. Define your conditions as a set of "rules," and let the engine find the right answer.
The pattern is simple. There are three main players:
Rules Class: Your rulebook! It's a super-powered Map with a friendly, fluent API for defining rules (.setRule()) and a safety net (.setDefault()).applyRules Function: The referee! It takes your rulebook and some data, runs through the rules, and returns the prize from the first one that matches.true or false.This setup keeps your business rules separate from the code that runs them, making your logic a joy to work with.
Let's start with a classic: figuring out who gets access to the VIP lounge at an exclusive club.
First, we define our Guest and the rules for entry.
import { Rules, applyRules } from 'declarative-rules';
// A guest at our club
type Guest = {
name: string;
visits: number;
isFriendOfOwner: boolean;
};
// Our simple, single-purpose predicate functions
const isVIP = (guest: Guest) => guest.name === 'Taylor Swift';
const isFriend = (guest: Guest) => guest.isFriendOfOwner;
const isLoyal = (guest: Guest) => guest.visits > 100;
Next, we use the Rules class to create our access list. The order is key—the first rule that matches wins!
const accessLevelRules = new Rules<string, Guest>()
.setRule(isVIP, 'Access All Areas 🌟')
.setRule(isFriend, 'VIP Lounge Access 🥂')
.setRule(isLoyal, 'Free Drink Voucher 🍹')
.setDefault('General Admission 🎟️'); // Everyone else gets this
Now, let's see who gets in. We use applyRules to check our guests against the rulebook.
const regularJoe = { name: 'Joe', visits: 10, isFriendOfOwner: false };
const taylor = { name: 'Taylor Swift', visits: 999, isFriendOfOwner: true };
// applyRules does the hard work for us!
const joesAccess = applyRules(regularJoe, accessLevelRules); // "General Admission 🎟️"
const taylorsAccess = applyRules(taylor, accessLevelRules); // "Access All Areas 🌟"
This is where the real magic happens! What if a rule's outcome was... another set of rules?
Let's determine the price and description of a magic item based on its properties.
First, let's define the shape of our data and the predicates that will test it. This makes our rules easier to read and maintain.
import { Rules, applyRules } from 'declarative-rules';
// The data for a magic item
type Item = {
rarity: 'legendary' | 'enchanted' | 'common';
isCursed: boolean;
};
// The final object we want to create
type ItemInfo = {
price: string;
description: string;
};
// Predicates to check the item's properties
const isLegendary = (item: Item) => item.rarity === 'legendary';
const isEnchanted = (item: Item) => item.rarity === 'enchanted';
const isLegendaryAndCursed = (item: Item) => isLegendary(item) && item.isCursed;
Now we'll build two rulebooks: one for the item's description and a more complex one for its pricing, which will use the first one.
// --- First, the Description Rulebook ---
// This one is simple: it just returns a string.
const descriptionRules = new Rules<string, Item>()
.setRule(isLegendary, 'Forged in dragon fire! 🔥')
.setRule(isEnchanted, 'Glows with a faint magical aura. ✨')
.setDefault('A standard, well-made item.');
// --- Now, the Pricing Rulebook ---
// The values here are FUNCTIONS that call applyRules with our other rulebook!
const pricingRules = new Rules<(item: Item) => ItemInfo, Item>()
.setRule(isLegendaryAndCursed, (item) => ({
price: 'Priceless',
description: 'A powerful but dangerous artifact!', // Custom description
}))
.setRule(isLegendary, (item) => ({
price: '10,000 Gold',
description: applyRules(item, descriptionRules), // Nested call!
}))
.setRule(isEnchanted, (item) => ({
price: '500 Gold',
description: applyRules(item, descriptionRules), // Nested call!
}))
.setDefault((item) => ({
price: '50 Gold',
description: applyRules(item, descriptionRules), // Nested call!
}));
The flow is like a waterfall:
applyRules with pricingRules. It finds the first match (e.g., isLegendary) and returns the function associated with it.applyRules with descriptionRules to get the final, detailed description.This keeps our logic neatly organized, even when it gets complex!
💡 Pro Tip: When nesting, the context object passed to the root
applyRulescall is automatically available to all child rules. If a child rule needs extra data, simply include it in the context object passed to the initial call.
.setRule(). The first match always wins.Rules instance should end with .setDefault(). This is your safety net, ensuring you never have an unhandled case.FAQs
A lightweight, type-safe library for managing complex conditional logic in a clean, declarative, and extensible way.
The npm package declarative-rules receives a total of 216 weekly downloads. As such, declarative-rules popularity was classified as not popular.
We found that declarative-rules demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.