Overview
This guide shows you battle-tested patterns for solving common business logic problems with Rules. Each pattern includes complete, copy-paste ready code and explains when to use it.Tiered Calculations
Apply different values based on thresholds - perfect for pricing, discounts, shipping rates, or service levels.Pattern: Volume Discount
Apply progressively larger discounts based on order size.Key principle: Order conditions from most specific (highest threshold) to least specific. Using
between with INCLUSIVE_LEFT ensures exact values like $100 or $200 get the correct tier without ambiguity.- Volume discounts
- Shipping rates by weight/distance
- Service level pricing (Basic/Pro/Enterprise)
- Progressive tax rates
- Priority levels based on value
Pattern: Customer Tier Benefits
Map customer tiers to different benefit levels.- Membership benefits
- Access levels
- Feature flags by plan
- Support priority
Validation Rules
Check if data meets business requirements and provide clear feedback.Pattern: Required Field Validation
Ensure critical fields are completed.- Form validation
- Data quality checks
- Required fields enforcement
- Pre-submission verification
Pattern: Range Validation
Check if values fall within acceptable ranges.- Numeric range validation
- Age verification
- Date range checks
- Inventory limits
Pattern: Validation with Error Messages
Provide specific error messages for failed validations.- User-friendly validation feedback
- Multi-field validation
- Form error messages
- API request validation
Lookup Tables & Mappings
Map keys to values using lookup objects - perfect for state taxes, shipping zones, or category mappings.Pattern: State Tax Lookup
Map states to their tax rates with a default fallback.- Tax rates by location
- Shipping costs by zone
- Commission rates by region
- Currency conversion rates
Pattern: Category-Based Rules
Apply different rules based on product category.- Category-specific policies
- Product type rules
- Department-specific logic
- Industry-specific calculations
Array Processing
Process lists of items with aggregations, filtering, and transformations using JSONPath queries.Pattern: Cart Total Calculation
Sum prices and quantities across cart items using jPath to query literal arrays.Alternative syntax: Rules also support a wildcard syntax (
@fact:items/*/property) for flattened key-value structures, but most users will work with literal JSON arrays as shown in this example.- Shopping cart totals
- Invoice line items
- Batch processing
- Aggregate calculations
Pattern: Array Filtering and Selection
Filter arrays based on conditions.- Filtering by criteria
- Available items only
- Conditional selections
- Qualified subset processing
Pattern: Array Aggregations
Calculate statistics from arrays.- Analytics calculations
- Summary statistics
- Dashboard metrics
- Report generation
Multi-Condition Logic
Combine multiple conditions to make complex decisions.Pattern: Eligibility Checks
Determine if someone qualifies based on multiple criteria.- Loan qualification
- Program eligibility
- Access control
- Application approval
Pattern: Priority Scoring
Calculate priority scores based on multiple weighted factors.- Ticket prioritization
- Lead scoring
- Risk assessment
- Resource allocation
String Formatting & Templates
Format data for display or generate dynamic messages.Pattern: Dynamic Message Generation
Create personalized messages with data substitution.- Email templates
- Notification messages
- Dynamic content
- User communications
Pattern: Name Formatting
Combine name parts in various formats.- Name display variations
- User profiles
- Report headers
- Contact lists
Date & Time Calculations
Work with dates for deadlines, aging, and scheduling.Pattern: Due Date Calculation
Calculate deadlines based on creation date and urgency.- SLA calculations
- Payment terms
- Project deadlines
- Expiration dates
Pattern: Age & Overdue Calculations
Determine how old something is or if it’s overdue.- Overdue tracking
- Aging reports
- Time-based alerts
- Compliance monitoring
Conditional Calculations
Perform different calculations based on conditions.Pattern: Conditional Fees
Apply fees only when certain conditions are met.- Conditional charges
- Dynamic pricing
- Fee calculations
- Surcharge logic
Best Practices
Keep It Simple
Break complex logic into multiple small rules rather than one giant nested rule
Use Descriptive Names
Name rules clearly so their purpose is obvious:
qualifiesForFreeShipping not rule1Provide Defaults
Always include a default outcome in conditional rules to handle edge cases
Test Incrementally
Add one rule at a time and test it before adding the next
Document Complex Logic
For complex rules, add comments or use descriptive intermediate rule names
Reuse Calculations
Calculate values once and reference them multiple times rather than recalculating
What’s Next?
Examples Library
See complete real-world examples using these patterns
Operations Reference
Detailed reference for all available operators
Core Concepts
Understand the fundamentals of how Rules work
Getting Started
Build your first rule with step-by-step guidance
Need Help? Visit our Help Center or join the Community for support.