DEV Community

Cover image for JSON Rules: Change Logic, Skip the Rebuild
Crafts 69 Guy
Crafts 69 Guy

Posted on

JSON Rules: Change Logic, Skip the Rebuild

Your Business Logic Doesn't Belong in Code

Hey there! Let me ask you something: how many times have you deployed an app just to change one tiny rule?

"Users need to be 21 now, not 18."
"Actually, gold members get 15% off, not 10%."

Rebuild. Redeploy. Wait. Repeat.

There's a better way.

What If Rules Were Just... Data?

Instead of this:

if (user.age >= 18 && user.tier === 'gold') {
  applyDiscount();
}
Enter fullscreen mode Exit fullscreen mode

You write this:

const discountRule = {
  and: [
    { gte: ['user.age', 18] },
    { eq: ['user.tier', 'gold'] }
  ]
};
Enter fullscreen mode Exit fullscreen mode

See the difference? The second one is just JSON. You can store it in a database, fetch it from an API, or update it without touching your code.

That's exactly what rule-engine-js does.

Quick Example

import { createRuleEngine, createRuleHelpers } from 'rule-engine-js';

const engine = createRuleEngine();
const rules = createRuleHelpers();

const canAccessPremium = rules.and(
  rules.gte('user.age', 18),
  rules.eq('user.verified', true)
);

const user = { age: 25, verified: true };
engine.evaluateExpr(canAccessPremium, user).success; // true
Enter fullscreen mode Exit fullscreen mode

Clean, right?

TypeScript? We Got You

Here's my favorite part. If you're using TypeScript, you get full path autocomplete:

interface UserContext {
  user: { name: string; age: number };
  order: { total: number; status: string };
}

const rules = createRuleHelpers<UserContext>();

// Start typing 'user.' and your IDE suggests: name, age
rules.gte('user.age', 18);      // ✓ autocomplete works
rules.gte('user.name', 18);     // ✗ TypeScript yells at you (name isn't a number!)
Enter fullscreen mode Exit fullscreen mode

Suggestion showcase

No more typos. No more guessing field names. Your IDE just knows.

Why This Matters (Especially for Mini Apps)

If you're building apps on platforms like Zalo Mini App, you know the pain. Every update goes through review. Every rebuild takes time.

With JSON rules:

  • Update instantly → Change rules server-side, no rebuild
  • A/B test easily → Serve different rules to different users
  • Track everything → Rules are just versioned JSON files

The Good Stuff

  • 📦 Tiny: ~11KB gzipped, zero dependencies
  • Fast: Built-in caching, sub-millisecond evaluation
  • 🔒 Secure: Prototype pollution protection included
  • 🎯 20+ operators: comparison, logic, string, array, state changes

Try It

npm install rule-engine-js
Enter fullscreen mode Exit fullscreen mode

That's it. No config needed.

Links:


Ship rules, not rebuilds.


🚀 Checkout my GitHub

🚀 Follow me on X(Twitter)

🚀 Connect with me on Linkedin

Top comments (0)