Skip to content

fix(index): export the real toString implementation - #2839

Merged
rubiin merged 1 commit into
validatorjs:masterfrom
simonkundrik:fix/tostring-export
Aug 8, 2026
Merged

fix(index): export the real toString implementation#2839
rubiin merged 1 commit into
validatorjs:masterfrom
simonkundrik:fix/tostring-export

Conversation

@simonkundrik

Copy link
Copy Markdown
Contributor

Fixes #1870.

toString is listed in the exported object in src/index.js, but the file never imports it. So the shorthand property just picks up the inherited Object.prototype.toString, and the real implementation in src/lib/util/toString.js is never exposed:

const validator = require('validator');

validator.toString('test');                        // '[object Object]'
validator.toString === Object.prototype.toString;  // true

The other to* sanitizers (toDate, toFloat, toInt, toBoolean) are each imported at the top of index.js, exported, and documented in the README. toString was the only one missing its import, and it sits in the export list right between normalizeEmail and isSlug, which is exactly where the import would have gone. So this looks like an import that got dropped rather than an intentional omission.

Adding the import makes it behave like the util it points at:

validator.toString('test');  // 'test'
validator.toString(123);     // '123'
validator.toString(null);    // ''

I went with wiring up the import rather than deleting the export, since that's what the one commenter on the issue asked for, and removing it wouldn't really change anything for callers (validator.toString would still resolve through the prototype). Happy to flip it to a removal if you'd rather shrink the API surface.

Worth noting the existing should export sanitizers test only does a typeof check, which can't catch this since typeof validator.toString was already 'function'. The new test in exports.test.js asserts it isn't the prototype method, so it fails on master and passes here.

Full suite is 292 passing, eslint clean.

Checklist

  • PR contains only changes related; no stray files, etc.
  • README updated (where applicable)
  • Tests written (where applicable)
  • References provided in PR (where applicable)

toString was listed in the exported object in src/index.js but was never
imported, so the shorthand property resolved to the inherited
Object.prototype.toString instead. That made validator.toString identical
to Object.prototype.toString, and validator.toString('test') returned
'[object Object]' rather than 'test'.

Import it from ./lib/util/toString so the export lines up with the other
to* sanitizers, which are all imported and documented. Also adds the
missing README row and tests.

Fixes validatorjs#1870
@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d4e02ee) to head (5b49cbc).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2839   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2598      2599    +1     
  Branches       658       658           
=========================================
+ Hits          2598      2599    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rubiin
rubiin requested a review from tux-tn August 6, 2026 09:53

@tux-tn tux-tn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rubiin
rubiin merged commit cdb7daf into validatorjs:master Aug 8, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Although toString method is exposed from the library, it's not referring the actual implementation.

3 participants