Fix: NaN Issues in Expression Parser for One-to-Many Relationships #86
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix NaN Issues in Expression Parser for One-to-Many Relationships
Problem Description
When using the computed interface with Directus one-to-many (o2M) relationships, users were experiencing NaN (Not a Number) values in calculated fields. This was particularly problematic for:
Root Cause
"5" + "3"resulted in"53"instead of8Example Scenarios Where NaN Occurred
Solution Implemented
1. Comprehensive NaN Prevention Pattern
Applied
(+value || 0)pattern to ALL numeric operations:2. Operations Fixed
Aggregated Operations (Primary Fix):
ASUM- Aggregated sum of related itemsAMIN- Aggregated minimum of related itemsAMAX- Aggregated maximum of related itemsAAVG- Aggregated average of related itemsAMUL- Aggregated multiplication of related itemsUnary Operations:
ABS,SQRT,SUM,AVERAGE,CEIL,FLOOR,ROUND,EXP,LOG,MAX,MINBinary Operations:
SUM,SUBTRACT,MULTIPLY,DIVIDE,REMAINDER,ROUND,MAX,MIN,POWER3. Type Safety Improvements
4. Comprehensive Test Coverage
Added 65+ test cases covering:
Impact
✅ o2M relationships now work correctly with computed fields
✅ No more NaN values in aggregated calculations
✅ Proper string-to-number conversion for all Directus data
✅ Backward compatibility maintained with existing expressions
✅ Enhanced type safety throughout the expression parser
Example Usage After Fix
This fix ensures that computed interface expressions work reliably with all Directus relationship types, especially one-to-many relationships where aggregated calculations are essential.