RuleBuilder
Visual editor for a WHEN/THEN rule: a nestable all/any/none condition tree plus the actions it fires.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
fields | RuleField[] | — | Fields the conditions may test. The first is used for new rows. |
actions | RuleActionDef[] | — | Actions the THEN side may fire. Omit to render conditions only. |
value | Rule | — | Controlled rule value. |
defaultValue | Rule | — | Initial rule when uncontrolled. |
onChange | (rule: Rule) => void | — | Called when the rule changes. |
maxDepth | number | 3 | Deepest nesting level for groups. |
readOnly | boolean | — | Displays static values instead of editing controls. |
header | ReactNode | — | Header slot above WHEN (rule name, enable switch, status tag). |
footer | ReactNode | — | Footer slot below THEN (Save/Test buttons, validation summary). |
labels | { when?: ReactNode; then?: ReactNode } | — | Custom labels for the WHEN and THEN section headings. |
Field & Action definitions
const FIELDS: RuleField[] = [
{
key: "request.method",
label: "Method",
type: "enum",
options: [
{ value: "GET", label: "GET" },
{ value: "POST", label: "POST" },
],
},
{
key: "request.path",
label: "Path",
type: "string",
placeholder: "/api/users",
},
{ key: "response.status", label: "Status code", type: "number" },
{ key: "auth.mfa", label: "MFA enabled", type: "bool" },
];const ACTIONS: RuleActionDef[] = [
{ type: "allow", label: "Allow", tone: "ok" },
{ type: "block", label: "Block", tone: "danger" },
{
type: "rate_limit",
label: "Rate limit",
tone: "info",
params: [
{ key: "requests", label: "Requests", type: "number" },
{ key: "window", label: "Window", type: "text" },
],
},
];Examples
Interactive rule builder
Helper functions
createRule, createGroup, createCondition
Factory functions for building a rule tree programmatically:
import { createRule, createGroup, createCondition } from "@solarius/onset";
// Create a new rule with one blank condition
const rule = createRule(fields);
// Create a group with a combinator
const group = createGroup("any", [createCondition(fields[0])]);
// Create a single condition
const condition = createCondition(fields[0]);compileRule
Walk the rule tree and emit it as a target language. Reference compilers live in rule-compilers.ts — dslRuleCompiler, typescriptRuleCompiler, and jsonRuleCompiler.
import { compileRule, dslRuleCompiler } from "@solarius/onset";
const dsl = compileRule(rule, dslRuleCompiler, {
fields: FIELDS,
actions: ACTIONS,
name: "access-rule",
});
// "rule access-rule { when { all { request.method is 'POST' } } then { allow } }"evaluateRule
Run a rule tree against a context object. The semantics match compileRule, so live preview and generated code agree.
import { evaluateRule } from "@solarius/onset";
const ctx = {
request: { method: "POST", path: "/login" },
auth: { mfa: true },
};
// Evaluate the WHEN-tree (rule.when) against the context
const matches = evaluateRule(rule.when, ctx);
// and count the conditions for display
const total = countConditions(rule.when);