Skip to content
Source
Sign inSign up
TanStack/table

fix(table-core): exclude non-numeric values from inNumberRange filter #6313

Closedchatman-media(GitHub profile, opens in a new tab)wants to merge 1 commit intobeta fromrefs/pull/6313/headopened

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 → true and null <= 20 → true
  • '' >= 0 → true and '' <= 20 → true
  • false → 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.
  • Tests

    • Added comprehensive test coverage for number range filtering, including edge cases with various data types.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      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.ts
      • packages/table-core/tests/unit/fns/filterFns.test.ts

      📝 Walkthrough

      Walkthrough

      The PR fixes a bug in filterFn_inNumberRange where 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

      Layer / File(s)Summary
      Implementation hardening and documentation
      packages/table-core/src/fns/filterFns.ts
      Updated filterFn_inNumberRange documentation to clarify that non-numeric values (null, undefined, empty strings, booleans) never satisfy the range check. Implementation now includes a guard that returns false when the row value is not a number or is NaN, preventing JavaScript coercion edge cases.
      Test suite for number range filtering
      packages/table-core/tests/unit/fns/filterFns.test.ts
      Added filterFn_inNumberRange to test imports. New filterFn_inNumberRange test block validates resolveFilterValue-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

      🐰 Null and undefined take their bow, No coercion tricks allowed now, Range checks pure, type-safe and true, Numbers only make it through!

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title check✅ PassedThe title accurately describes the main fix: excluding non-numeric values from the inNumberRange filter function.
      Linked Issues check✅ PassedThe PR directly addresses issue #6007 by fixing the bug where null and non-numeric values incorrectly passed the range filter with minimum value 0.
      Out of Scope Changes check✅ PassedAll changes are directly related to fixing the inNumberRange filter and adding corresponding unit tests; no out-of-scope modifications detected.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      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
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    2. KevinVandy closed this

Sign in to join this conversation.