Logic Engine
Show this section IF (State is WA OR CA) AND (Income < $50,000).Government forms need complex conditional logic. The Logic Engine evaluates expression trees to determine which fields are visible based on current form values.
Why Expression Trees?
Simple visibility rules like “show X when Y equals Z” don’t scale. Real forms have conditions like:- “Show disability section if age > 65 OR has disability”
- “Show income verification if employment = ‘self-employed’ AND income > $50k”
- “Hide bank section if (payment method = ‘check’) OR (no bank account)”
(State = WA OR State = CA) AND (Income < 50000)
Data Structures
LogicRule
A single comparison:LogicGroup
A collection of conditions combined with AND or OR:LogicCondition
The recursive union:Operators
The engine supports 18 operators:| Operator | Description | Example |
|---|---|---|
eq | Equals | income = 50000 |
neq | Not equals | status != "denied" |
gt | Greater than | age > 18 |
gte | Greater than or equal | income >= 30000 |
lt | Less than | household_size < 4 |
lte | Less than or equal | age <= 65 |
contains | String/array contains | states contains "WA" |
not_contains | Does not contain | allergies not_contains "peanuts" |
starts_with | String starts with | zip starts_with "98" |
ends_with | String ends with | email ends_with ".gov" |
in | Value in list | state in ["WA", "CA", "OR"] |
not_in | Value not in list | status not_in ["denied", "cancelled"] |
exists | Has any value | phone exists |
not_exists | Is empty | middle_name not_exists |
Age Operators (for date fields)
Special operators that calculate age from a date:| Operator | Description | Example |
|---|---|---|
minAge | At least X years old | dob minAge 18 (must be 18+) |
maxAge | At most X years old | dob maxAge 65 (must be 65 or under) |
underAge | Under X years old | dob underAge 21 (show warning) |
overAge | Over X years old | dob overAge 62 (show senior info) |
Evaluation
The main evaluation function:Rule Evaluation
Group Evaluation
Nested Path Support
Field IDs can reference nested data:address.state = "WA".
Show vs Hide Actions
By default, logic shows fields when conditions are met. But sometimes you want the opposite:logicAction property inverts the logic result:
"show"(default): field visible when logic is true"hide": field visible when logic is false
Complete Example
A form section that shows income verification for high-earning self-employed applicants in certain states:Legacy Visibility (Deprecated)
The old visibility format is still supported for backwards compatibility:evaluateElementVisibility function handles both:
Helper Functions
Utilities for building logic programmatically:Form Builder UI
The Logic Builder component (insrc/components/form-builder/logic-builder.tsx) provides a visual interface:
Users can:
- Add rules that compare field values
- Create nested groups with AND/OR
- See live preview of which fields would show
Performance Considerations
The logic engine re-evaluates on every form value change. For most forms this is instant, but deep nesting could become slow. Optimizations applied:- Short-circuit evaluation — AND stops at first false, OR stops at first true
- Direct property access — no recursive tree walking for simple paths
- Type coercion caching — number/string conversions memoized per render
Edge Cases
Empty Values
Empty inputs ("", null, undefined, []) are handled consistently: