Skip to main content

Examples

Shopping Cart

Business rules as constraints: quantity limits, coupon validation, inventory checks, and auth-gated checkout.

Try it

Loading example…

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.

  1. Derivations subtotal, discount, tax, total, and freeShipping form a composition chain where each depends on the previous
  2. Constraints quantityLimit (priority 80) clamps overstocked items; couponValidation (priority 70) validates codes via API; checkoutReady (priority 60) gates on auth + cart validity using after
  3. Cross-module – checkout reads auth.isAuthenticated via crossModuleDeps. Toggling auth off blocks checkout even if the cart is valid
  4. 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

Stay in the loop. Sign up for our newsletter.

We care about your data. We'll never share your email.

Powered by Directive. This signup uses a Directive module with facts, derivations, constraints, and resolvers – zero useState, zero useEffect. Read how it works