Policy Engine API
API reference for @guardrail-sim/policy-engine
Policy Engine API
Complete API reference for the @guardrail-sim/policy-engine package.
PolicyEngine
The main class for policy evaluation.
Constructor
Creates a new policy engine instance with the given policy.
evaluate
Evaluate a discount request against the policy.
Parameters:
- order: Order details including value, quantity, margin, and customer segment
- proposedDiscount: Discount as decimal (0.15 = 15%)
Returns: EvaluationResult with approval status, violations, and calculated margin.
Example:
getPolicy
Get the current policy.
calculateAllocations
Calculate how a discount should be allocated across line items.
Parameters:
- discountAmount: Total discount amount
- lineItems: Array of line items with subtotals
- method: Allocation method ('across' proportionally, 'each' evenly)
Returns: Array of allocations per line item.
Example:
calculateMaxDiscount
Calculate the maximum allowed discount for an order.
Returns: Maximum discount as decimal.
defaultPolicy
Pre-configured policy with standard B2B pricing rules.
Includes:
- margin_floor: 15% minimum margin
- max_discount: 25% maximum discount
- volume_tier: 10% base, 15% for orders >= 100 units
getUCPErrorCode
Map a policy violation to a UCP-compatible error code.
Example:
VIOLATION_TO_UCP_ERROR
Mapping of violation rules to UCP error codes.