Examples
Shopping Cart
Business rules as constraints: quantity limits, coupon validation, inventory checks, and auth-gated checkout.
Try it
Adjust quantities to exceed stock limits, try coupon codes “SAVE10” or “HALF”, toggle auth, and attempt checkout. Watch constraints enforce business rules automatically.
How it works
A cart module manages items, coupons, and checkout while an auth module gates checkout – all connected through constraints with priority ordering.
- Derivations –
subtotal,discount,tax,total, andfreeShippingform a composition chain where each depends on the previous - Constraints –
quantityLimit(priority 80) clamps overstocked items;couponValidation(priority 70) validates codes via API;checkoutReady(priority 60) gates on auth + cart validity usingafter - Cross-module – checkout reads
auth.isAuthenticatedviacrossModuleDeps. Toggling auth off blocks checkout even if the cart is valid - Retry – the checkout resolver uses exponential backoff (2 attempts) to handle transient server failures
Summary
What: A shopping cart with quantity limits, coupon validation, tax calculation, free shipping threshold, and auth-gated checkout.
How: Three constraints with priority ordering handle business rules automatically. quantityLimit fires first, then couponValidation, then checkoutReady (which uses after to wait for the others).
Why it works: Business rules are constraints, not imperative code. Adding a new rule (bundle discount, minimum order) is just another constraint definition. Priority and after ordering ensure rules execute in the right sequence without manual orchestration.
Source code
// Source file "shopping-cart.ts" not found

