Menu
Dev.to #architecture·November 26, 2026

Designing a Rule Engine for Spatial Planning and Code Compliance

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 #architecture

Overview of the Bathroom Clearance Rules Engine

The 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.

Key Architectural Requirements

  1. Distinguish between legal code minimums (IRC) and functional comfort guidelines (NKBA) in a single structured pass.
  2. Automatically switch fixture geometry rules based on three common US bathroom topologies: `powder_room`, `single_wall`, and `primary_suite`.
  3. Detect physical collisions, such as door-arc strikes or fixture overlaps, and provide precise fix hints.
  4. Compute material takeoffs (e.g., floor and wet-surround tile) directly from the resolved geometry.

Beyond Binary Pass/Fail: A Three-State Compliance Model

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."

typescript
export type BathRuleStatus = 'NKBA_PASS' | 'IRC_PASS_TIGHT' | 'FAIL';export type BathOverallStatus = 'NKBA_COMFORT' | 'IRC_PASS_TIGHT' | 'CODE_FAIL';

Topology-Driven Geometry Classification

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.

rules enginetypescriptgeometryspatial algorithmscode complianceCADsoftware architecturedomain-driven design

Comments

Loading comments...