Skip to content

fix: prefer hasOwnProperty over hasOwn#1421

Open
alaycock-stripe wants to merge 1 commit intoopen-circle:mainfrom
alaycock-stripe:fix-use-has-own-property
Open

fix: prefer hasOwnProperty over hasOwn#1421
alaycock-stripe wants to merge 1 commit intoopen-circle:mainfrom
alaycock-stripe:fix-use-has-own-property

Conversation

@alaycock-stripe
Copy link

@alaycock-stripe alaycock-stripe commented Mar 11, 2026

We are using Valibot for a project that requires significant browser backwards compatibility but I am unable to use some schemas because of Object.hasOwn. While this is possible to polyfill, this small change would allow us to drop our polyfill, reducing our bundle size. The tradeoff is a small reduction in readability for significantly better browser compatibility.

This change is similar to an existing polyfill, except it relies on types rather than a throwing an exception.

The change should have no runtime impact, all tests continue to pass.

Summary by CodeRabbit

  • Refactor
    • Improved object property validation for better compatibility and stability.

@vercel
Copy link

vercel bot commented Mar 11, 2026

@alaycock-stripe is attempting to deploy a commit to the Open Circle Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 11, 2026
@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Walkthrough

A single utility function in _isValidObjectKey has been updated to replace Object.hasOwn() with Object.prototype.hasOwnProperty.call() for safer property checking. No functional changes or export modifications.

Changes

Cohort / File(s) Summary
Property Check Refactoring
library/src/utils/_isValidObjectKey/_isValidObjectKey.ts
Replaced Object.hasOwn(object, key) with Object.prototype.hasOwnProperty.call(object, key) for more robust object property verification.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A safer check we now employ,
No shadow tricks to break our joy,
The prototype's direct call,
Protects our keys through one and all! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically identifies the main change: replacing Object.hasOwn with Object.prototype.hasOwnProperty.call() in the codebase to improve backwards compatibility.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@dosubot dosubot bot added the fix A smaller enhancement or bug fix label Mar 11, 2026
@fabian-hiller fabian-hiller requested a review from Copilot March 13, 2026 06:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Valibot’s object-key validation utility to avoid Object.hasOwn and instead use Object.prototype.hasOwnProperty.call, improving compatibility with older browsers and reducing the need for a polyfill.

Changes:

  • Replaced Object.hasOwn(object, key) with Object.prototype.hasOwnProperty.call(object, key) in _isValidObjectKey.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@fabian-hiller
Copy link
Member

How much of an impact it the bundle size of the polyfill? Another option for you is to copy our object schema code (Valibot is modular so you can bring your own schemas and actions) or patch this locally (your package manager probably supports patching a library).

@fabian-hiller fabian-hiller self-assigned this Mar 13, 2026
@alaycock-stripe
Copy link
Author

alaycock-stripe commented Mar 13, 2026

When using core-js to polyfill several bundles are getting a ~3kb bump. I haven't explored into why the impact is so significant.

I'll look into patching, but was curious if this change would be appreciated upstream. Your tsconfig targets ES2020 but hasOwn landed in ES2022.

@fabian-hiller
Copy link
Member

I'll look into patching, but was curious if this change would be appreciated upstream. Your tsconfig targets ES2020 but hasOwn landed in ES2022.

This is a good argument. On the other side hasOwn should have around 97% coverage across browsers and no one has complained so far. I was using hasOwn instead of hasOwnProperty because the MDN docs recommended it.

@fabian-hiller fabian-hiller added this to the v1.3 milestone Mar 13, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 13, 2026

Open in StackBlitz

npm i https://pkg.pr.new/valibot@1421

commit: 3ddb1b7

@fabian-hiller fabian-hiller modified the milestones: v1.3, v1.4 Mar 17, 2026
@fabian-hiller
Copy link
Member

I think I prefer to wait before making a decision.

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

Labels

fix A smaller enhancement or bug fix size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants