Policies
The building blocks of guardrail-sim
Policies
A policy is a named collection of rules that define what discounts are allowed in B2B pricing.
Policy Structure
interface Policy {
id: string;
name: string;
rules: PolicyRule[];
}Required Fields
- id: Unique identifier for the policy
- name: Human-readable name
- rules: Array of rules to enforce
Policy Rules
Each rule uses the json-rules-engine format with conditions and events:
interface PolicyRule {
name: string;
conditions: {
all?: RuleCondition[];
any?: RuleCondition[];
};
event: {
type: string;
params?: Record<string, unknown>;
};
priority?: number;
}Example Rule
const marginFloorRule: PolicyRule = {
name: 'margin_floor',
conditions: {
all: [
{
fact: 'calculated_margin',
operator: 'lessThan',
value: 0.15,
},
],
},
event: {
type: 'violation',
params: {
message: 'Calculated margin falls below 15% floor',
},
},
priority: 1,
};Default Policy
The default policy includes three rules:
import { defaultPolicy } from '@guardrail-sim/policy-engine';
// Includes:
// - margin_floor: 15% minimum margin
// - max_discount: 25% maximum discount
// - volume_tier: 10% base, 15% for qty >= 100Custom Policies
Create custom policies with your own rules:
import { PolicyEngine } from '@guardrail-sim/policy-engine';
import type { Policy } from '@guardrail-sim/policy-engine';
const holidayPolicy: Policy = {
id: 'holiday-2026',
name: 'Holiday Sale Policy',
rules: [
{
name: 'holiday_max',
conditions: {
all: [{ fact: 'proposed_discount', operator: 'greaterThan', value: 0.3 }],
},
event: {
type: 'violation',
params: { message: 'Holiday discount cannot exceed 30%' },
},
},
],
};
const engine = new PolicyEngine(holidayPolicy);Rule Conditions
Conditions use facts, operators, and values:
| Operator | Description |
|---|---|
equal | Exact match |
notEqual | Not equal |
lessThan | Less than value |
lessThanInclusive | Less than or equal |
greaterThan | Greater than value |
greaterThanInclusive | Greater than or equal |
in | Value in array |
notIn | Value not in array |
contains | Array contains value |
Available Facts
The policy engine provides these facts for conditions:
| Fact | Description |
|---|---|
order_value | Total order value in dollars |
quantity | Total units in order |
product_margin | Base margin (0.40 = 40%) |
proposed_discount | Requested discount (0.15 = 15%) |
calculated_margin | Margin after discount |
customer_segment | Customer tier (gold, silver, etc.) |
Best Practices
- Name rules clearly: Use descriptive names like
margin_floornotrule1 - Set priorities: Higher priority rules run first
- Provide messages: Include helpful violation messages
- Test thoroughly: Use multiple test scenarios