fix(table-core): exclude non-numeric values from inNumberRange filter #6313
beta fromrefs/pull/6313/head·opened Summary#
filterFn_inNumberRange compares row values with rowValue >= min && rowValue <= max, relying on JavaScript's loose relational coercion. Non-numeric values coerce to numbers in that context, so they slip into the range:
null >= 0→trueandnull <= 20→true'' >= 0→trueand'' <= 20→truefalse→0,true→1(both inside[0, 20])
As a result, a nullable numeric column leaks empty rows into a [0, max] range filter. This matches the report in #6007 (filtering age from 0–20 also returns rows where age is null). The bug only surfaces when the lower bound is 0 (or negative), because that's when null/'' coerce inside the range — a lower bound of 1 already excludes them.
inNumberRange is the auto-assigned filter for numeric columns and is documented as "Number range inclusion", so non-numeric values should never be considered in range.
Fix#
Guard the comparison so only finite numbers can fall inside the range:
const rowValue = row.getValue<number>(columnId)
if (typeof rowValue !== 'number' || Number.isNaN(rowValue)) {
return false
}
return rowValue >= min && rowValue <= max
Genuine numbers (including 0, negatives and the inclusive boundaries) and open-ended ranges (['', 20] → [-Infinity, 20]) keep working exactly as before; only null/undefined/''/booleans are now correctly excluded. The sibling between/betweenInclusive filters are unaffected — they delegate to greaterThan/lessThan, which use strict > on a 0-coerced value and already exclude null at the lower boundary.
Tests#
Added a filterFn_inNumberRange test block in packages/table-core/tests/unit/fns/filterFns.test.ts that runs the user filter through resolveFilterValue (mirroring the real filtering pipeline) and asserts:
- numbers inside the range, on the inclusive boundaries, and out of range
null,undefined,''and booleans are excluded from[0, 20]- open-ended ranges still match real numbers while excluding empty values
The new tests fail on beta without the fix (4 failing) and pass with it. Full @tanstack/table-core suite (313 tests) passes; tsc, eslint and prettier are clean.
Closes #6007
Summary by CodeRabbit#
Bug Fixes
- Improved number range filter robustness by fixing edge cases where non-numeric values (
null,undefined, empty strings, booleans) could incorrectly pass filter checks due to JavaScript type coercion. The filter now explicitly validates numeric types and consistently excludes non-numeric values as intended.
- Improved number range filter robustness by fixing edge cases where non-numeric values (
Tests
- Added comprehensive test coverage for number range filtering, including edge cases with various data types.
- 1
KevinVandy closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
67057a5c-e76e-458a-bb98-5dd5725a657e📥 Commits
Reviewing files that changed from the base of the PR and between 5ca8a67712421fb13b0b3167b6fb3cfdb87aca8a and d597e82dc0e8f85c7530c3874fd7d62b407b85f6.
📒 Files selected for processing (2)
packages/table-core/src/fns/filterFns.tspackages/table-core/tests/unit/fns/filterFns.test.ts📝 Walkthrough
Walkthrough#
The PR fixes a bug in
filterFn_inNumberRangewhere null and other non-numeric values were incorrectly passing range checks due to JavaScript coercion. The implementation is hardened with explicit type validation, documentation clarified, and comprehensive tests added.Changes#
Number Range Filter Type Safety
packages/table-core/src/fns/filterFns.tsfilterFn_inNumberRangedocumentation to clarify that non-numeric values (null, undefined, empty strings, booleans) never satisfy the range check. Implementation now includes a guard that returnsfalsewhen the row value is not anumberor isNaN, preventing JavaScript coercion edge cases.packages/table-core/tests/unit/fns/filterFns.test.tsfilterFn_inNumberRangeto test imports. NewfilterFn_inNumberRangetest block validatesresolveFilterValue-driven range filtering, inclusive boundary behavior, correct exclusion of non-numeric and nullable inputs, and behavior when only an upper bound is supplied.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~10 minutes
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#6007by fixing the bug where null and non-numeric values incorrectly passed the range filter with minimum value 0.✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share