fix(index): export the real toString implementation - #2839
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
rubiin
approved these changes
Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1870.
toStringis listed in the exported object insrc/index.js, but the file never imports it. So the shorthand property just picks up the inheritedObject.prototype.toString, and the real implementation insrc/lib/util/toString.jsis never exposed:The other
to*sanitizers (toDate,toFloat,toInt,toBoolean) are each imported at the top ofindex.js, exported, and documented in the README.toStringwas the only one missing its import, and it sits in the export list right betweennormalizeEmailandisSlug, 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:
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.toStringwould 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 sanitizerstest only does atypeofcheck, which can't catch this sincetypeof validator.toStringwas already'function'. The new test inexports.test.jsasserts it isn't the prototype method, so it fails on master and passes here.Full suite is 292 passing, eslint clean.
Checklist