@soeasily/rules
Version:
Evaluate and validate SoEasily DSL rules in TypeScript/JavaScript.
88 lines (60 loc) โข 1.76 kB
Markdown
# @soeasily/rules
A lightweight, AI-friendly rule engine for evaluating and validating SoEasily DSL rules in Node.js or TypeScript.
> Write simple, JSON-based rules like:
> `"when": "user.age >= 18", then: { type: 'approve' }`
## โจ Features
- โ
Evaluate rules with simple `expr` or `when`/`then` syntax
- โ
Validate rules using the official JSON Schema
- ๐ง Typed with TypeScript
- ๐งช Includes a test harness
- โก Fast and dependency-light (uses Ajv only)
## ๐ฆ Install
```bash
npm install @soeasily/rules
```
or with yarn:
```bash
yarn add @soeasily/rules
```
## ๐ง Usage
### Evaluate a Rule
```ts
import { evaluateRule } from '@soeasily/rules';
const rule = {
name: 'is_adult',
expr: 'input.age >= 18',
event: { type: 'allow' }
};
const input = { age: 20 };
const result = evaluateRule(rule, input);
// result: { matched: true, event: { type: 'allow' } }
```
### Validate a Rule
```ts
import { validateRule } from '@soeasily/rules';
const rule = {
name: 'missing_expr'
};
const validation = validateRule(rule);
// validation: { valid: true/false, errors: [...] }
```
## ๐ Supported DSL Fields
| Field | Description |
|------------------|----------------------------------------------|
| `name` | Required. Unique rule name. |
| `expr` | JS expression to evaluate (e.g. `input.age > 18`) |
| `when` / `then` | Simplified condition + action shortcut |
| `event` | Fallback standard format |
Planned: support for `lookup`, `extends`, `test_cases`, `settings`.
## ๐งช Run the Test
```bash
npm install
npm run test
```
## ๐ License
MIT โ Copyright ยฉ 2025 SoEasily