Skip to content

fix: isObject should return false for functions#2684

Open
chatman-media wants to merge 1 commit into
typestack:developfrom
chatman-media:fix/is-object-rejects-functions
Open

fix: isObject should return false for functions#2684
chatman-media wants to merge 1 commit into
typestack:developfrom
chatman-media:fix/is-object-rejects-functions

Conversation

@chatman-media

Copy link
Copy Markdown

Bug

The isObject / @IsObject() validator incorrectly accepts function values as valid objects.

The README documentation is explicit (emphasis mine):

@IsObject() — Checks if the object is valid Object (null, functions, arrays will return false).

However, the implementation in src/decorator/typechecker/IsObject.ts includes typeof value === 'function' in the condition:

// before
return value != null && (typeof value === 'object' || typeof value === 'function') && !Array.isArray(value);

This causes isObject(() => 'x') to return true, which contradicts the documented behavior.

Closes #2515

Root cause

The || typeof value === 'function' branch was included in the check, presumably to mirror a generic "non-primitive" guard. Since functions are not plain data objects, the docs correctly exclude them — the implementation just didn't follow through.

Fix

Remove the typeof value === 'function' branch:

// after
return value != null && typeof value === 'object' && !Array.isArray(value);

Test evidence

Two function values (() => 'function' and a named function expression) were added to the invalidValues array in the IsObject test suite. Before the fix, 2 tests failed; after the fix, all 5 IsObject tests pass and the full suite (806 tests) continues to pass.

PASS test/functional/validation-functions-and-decorators.spec.ts
  IsObject
    ✓ should not fail if validator.validate said that its valid
    ✓ should fail if validator.validate said that its invalid
    ✓ should not fail if method in validator said that its valid
    ✓ should fail if method in validator said that its invalid
    ✓ should return error object with proper data

Tests: 806 passed, 806 total

The `isObject` function incorrectly returned `true` for function values
because it checked `typeof value === 'function'` in addition to
`typeof value === 'object'`. The README documentation explicitly states
that functions should return false, but the implementation contradicted
this.

Fixes typestack#2515
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

fix: isObject returns true for functions but docs say it should be false

1 participant