This article discusses the architecture of a pure TypeScript rules engine for validating bathroom layouts against building codes (IRC) and comfort guidelines (NKBA). It highlights the challenges of encoding spatial geometry rules, managing multiple compliance states, and adapting logic for different room topologies, offering insights into building a deterministic, dependency-free rules engine for complex spatial constraints.
Read original on Dev.to #architectureThe core of this system is a deterministic, pure TypeScript rules engine designed to validate bathroom layouts. It processes various inputs like room dimensions and fixture types to ensure compliance with International Residential Code (IRC) and National Kitchen & Bath Association (NKBA) guidelines. The engine aims to provide more nuanced feedback than a simple pass/fail, distinguishing between strict code violations and sub-optimal, yet legal, configurations.
Instead of a simple boolean `isValid`, the engine uses a three-state enum for rule outcomes: `NKBA_PASS`, `IRC_PASS_TIGHT`, and `FAIL`. This allows the system to communicate whether a layout meets comfort standards, barely passes legal code, or is a definite violation. Each check also includes `fixHint` details, offering actionable advice like "Widen toilet alcove by at least 2" to reach the 30" IRC code minimum."
export type BathRuleStatus = 'NKBA_PASS' | 'IRC_PASS_TIGHT' | 'FAIL';export type BathOverallStatus = 'NKBA_COMFORT' | 'IRC_PASS_TIGHT' | 'CODE_FAIL';The engine dynamically adjusts its clearance equations based on the bathroom layout. It classifies rooms into `powder_room` (typically narrow, no shower), `single_wall` (fixtures on one wall, common in 5x8 baths), or `primary_suite` (wider rooms, fixtures split across walls). This classification ensures accurate spatial calculations, particularly for handling potential overlaps in multi-wall layouts where simple linear subtractions would be insufficient.
System Design Takeaway: Context-Aware Rule Engines
When designing rule engines for complex domains, consider how different contexts or configurations (like room topologies here) necessitate distinct rule sets or calculation methodologies. A flexible classification system allows for a more accurate and maintainable rule base, avoiding over-generalization that can lead to incorrect validations or missed edge cases.