fix(form-core): support value types with a custom equals() method in deep equality #2261
main fromrefs/pull/2261/head·opened Closes #2195
Problem#
evaluate (the deep-equality helper used for dirty-checking) reports two equal value-type instances as unequal. Value types like Temporal.PlainDate, Luxon DateTime, etc. expose their state through private fields/getters, so they have no own enumerable keys and a non-plain prototype — which hits the no-enumerable-keys guard added in #2140 and returns false. A field backed by such a value is then considered dirty even after it's reset to its original value.
Fix#
Before that guard, delegate to a custom equals() method when both operands share a constructor that exposes one. This covers Temporal types, Luxon DateTime, Immutable collections, and any value type following the equals() convention, while leaving plain objects/arrays and the existing Date/Map/Set cases untouched.
Tests#
Added a form-core unit test using a value type with a private field and an equals() method (mirroring Temporal's shape — no own enumerable keys). Reverting the fix fails it; the full form-core suite passes, plus typecheck and lint.
Summary by CodeRabbit#
- Bug Fixes
- Improved form value comparisons for objects with custom
equals()methods. - Added support for value types such as Temporal and Luxon
DateTimeinstances, including objects with private state. - Ensured equal instances compare correctly while differing instances remain distinct.
- Improved form value comparisons for objects with custom
- 1
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Inline comments: In `@packages/form-core/src/utils.ts`: - Around line 522-528: Update the constructor-based equality branch in evaluate to require both objA.equals and objB.equals to be callable before delegating to objA.equals(objB). Add a test in utils.spec.ts covering an object with equals() compared against an object without it, asserting they are not considered equal.🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID:
4939f6b5-c10a-44c7-8ff0-7d0e3d405dff📥 Commits
Reviewing files that changed from the base of the PR and between 5d1128141a705ebb24ade1275b3117bb4c8b1bdc and 7ed7642626cc92c966801a4a252457fec3ed87d3.
📒 Files selected for processing (3)
.changeset/evaluate-custom-equals.mdpackages/form-core/src/utils.tspackages/form-core/tests/utils.spec.ts
@@ -514,3 +514,15 @@514514 return true515515 }516516517+ // Delegate to a custom value-equality method (Temporal types, Luxon DateTime,518+ // Immutable collections, etc.) when both operands share a constructor that519+ // exposes one. This must run before the no-enumerable-keys guard below, which520+ // would otherwise treat every such value as unequal since they expose their521+ // state through getters/private fields rather than own enumerable keys (#2195).522+ if (523+ objA.constructor === objB.constructor &&524+ typeof (objA as { equals?: unknown }).equals === 'function'525+ ) {526+ return (objA as unknown as { equals: (other: unknown) => boolean }).equals(527+ objB,528+ )🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Require both operands to expose a callable
equals()method.Line 524 checks only
objA.equals. Two object literals shareObjectas their constructor, soevaluate({ equals: () => true }, {})returnstrueeven though the operands have different keys. This can mark a form value as equal to its default incorrectly.Check
objB.equalsbefore delegating. Add this one-sided case topackages/form-core/tests/utils.spec.ts.Proposed fix
if ( objA.constructor === objB.constructor && - typeof (objA as { equals?: unknown }).equals === 'function' + typeof (objA as { equals?: unknown }).equals === 'function' && + typeof (objB as { equals?: unknown }).equals === 'function' ) {📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if ( objA.constructor === objB.constructor && typeof (objA as { equals?: unknown }).equals === 'function' && typeof (objB as { equals?: unknown }).equals === 'function' ) { return (objA as unknown as { equals: (other: unknown) => boolean }).equals( objB, )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/form-core/src/utils.ts` around lines 522 - 528, Update the constructor-based equality branch in evaluate to require both objA.equals and objB.equals to be callable before delegating to objA.equals(objB). Add a test in utils.spec.ts covering an object with equals() compared against an object without it, asserting they are not considered equal.✅ Addressed in commit c424cca
View your CI Pipeline Execution ↗ for commit c424ccaea39bd4b1c72057bea351e5f268c45154
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 3m 24s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 38s View ↗
☁️ Nx Cloud last updated this comment at
2026-08-08 08:13:39UTCMore templates
- @tanstack/form-example-angular-array
- @tanstack/form-example-angular-large-form
- @tanstack/form-example-angular-multi-step-wizard
- @tanstack/form-example-angular-simple
- @tanstack/form-example-angular-standard-schema
- @tanstack/form-example-lit-array
- @tanstack/form-example-lit-large-form
- @tanstack/form-example-lit-multi-step-wizard
- @tanstack/form-example-lit-simple
- @tanstack/form-example-lit-standard-schema
- @tanstack/form-example-lit-ui-libraries
- @tanstack/form-example-preact-multi-step-wizard
- @tanstack/form-example-preact-simple
- @tanstack/form-example-svelte-array
- @tanstack/form-example-svelte-large-form
- @tanstack/form-example-svelte-multi-step-wizard
- @tanstack/form-example-svelte-simple
- @tanstack/form-example-svelte-standard-schema
- @tanstack/form-example-react-array
- @tanstack/form-example-react-compiler
- @tanstack/form-example-react-composition
- @tanstack/form-example-react-devtools
- @tanstack/form-example-react-dynamic
- @tanstack/form-example-react-expo
- @tanstack/field-errors-from-form-validators
- @tanstack/form-example-react-large-form
- @tanstack/form-example-react-multi-step-wizard
- @tanstack/form-example-react-next-server-actions
- @tanstack/form-example-react-next-server-actions-zod
- @tanstack/form-example-react-query-integration
- @tanstack/form-example-remix
- @tanstack/form-example-react-simple
- @tanstack/form-example-react-standard-schema
- @tanstack/form-example-react-tanstack-start
- @tanstack/form-example-react-ui-libraries
- @tanstack/form-example-solid-array
- @tanstack/form-example-solid-devtools
- @tanstack/form-example-solid-large-form
- @tanstack/form-example-solid-multi-step-wizard
- @tanstack/form-example-solid-simple
- @tanstack/form-example-solid-standard-schema
- @tanstack/form-example-vue-array
- @tanstack/form-example-vue-multi-step-wizard
- @tanstack/form-example-vue-simple
- @tanstack/form-example-vue-standard-schema
@tanstack/angular-form
npm i https://pkg.pr.new/@tanstack/angular-form@2261@tanstack/form-core
npm i https://pkg.pr.new/@tanstack/form-core@2261@tanstack/form-devtools
npm i https://pkg.pr.new/@tanstack/form-devtools@2261@tanstack/lit-form
npm i https://pkg.pr.new/@tanstack/lit-form@2261@tanstack/preact-form
npm i https://pkg.pr.new/@tanstack/preact-form@2261@tanstack/react-form
npm i https://pkg.pr.new/@tanstack/react-form@2261@tanstack/react-form-devtools
npm i https://pkg.pr.new/@tanstack/react-form-devtools@2261@tanstack/react-form-nextjs
npm i https://pkg.pr.new/@tanstack/react-form-nextjs@2261@tanstack/react-form-remix
npm i https://pkg.pr.new/@tanstack/react-form-remix@2261@tanstack/react-form-start
npm i https://pkg.pr.new/@tanstack/react-form-start@2261@tanstack/solid-form
npm i https://pkg.pr.new/@tanstack/solid-form@2261@tanstack/solid-form-devtools
npm i https://pkg.pr.new/@tanstack/solid-form-devtools@2261@tanstack/svelte-form
npm i https://pkg.pr.new/@tanstack/svelte-form@2261@tanstack/vue-form
npm i https://pkg.pr.new/@tanstack/vue-form@2261commit:
c424cca:warning: Please install the
to ensure uploads and comments are reliably processed by Codecov.
Codecov Report#
:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 89.79%. Comparing base (
6892ed0) to head (c424cca). :warning: Report is 264 commits behind head on main. :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.Additional details and impacted files
@@ Coverage Diff @@ ## main #2261 +/- ## ========================================== - Coverage 90.35% 89.79% -0.56% ========================================== Files 38 66 +28 Lines 1752 3235 +1483 Branches 444 814 +370 ========================================== + Hits 1583 2905 +1322 - Misses 149 296 +147 - Partials 20 34 +14:umbrella: View full report in Codecov by Harness. :loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Pulled the current
evaluate()from main to check the guard ordering, since that's what actually determines whether this fixes #2195 or not. The zero-own-keys guard a few lines below (keysA.length === 0 && ... prototype !== Object.prototype) is exactly what was returningfalsefor two equal Temporal/Luxon instances before this patch, they expose state through private fields and getters soObject.keys()is empty on both, and that guard was written assuming empty keys on a non-plain object means "can't compare, treat as unequal." Putting theequals()delegation above that guard is the only place it can go for the fix to matter, since by the time you reach the empty-keys check it's already too late. Confirmed that's where it landed.I see CodeRabbit's earlier comment about requiring both
objA.equalsandobjB.equalsto be callable before delegating, that's already handled in the version I'm looking at, both are checked withtypeof ... === 'function', and there's a dedicated test for the one-sided case now ({ equals: () => true }vs{}in both directions). Good that it's covered both ways since plain objects share theObjectconstructor, so the constructor check alone wouldn't have caught a lopsidedequals.Requiring
objA.constructor === objB.constructorin addition to both havingequalsis the right conservative choice, it avoids letting anequals()from one class accidentally get called with an incompatible instance of a different class that happens to also defineequalswith different semantics.The test class using a private field (
#iso) rather than a plain property is a good choice too, it's what actually reproduces the "no own enumerable keys" condition instead of just simulating it, so the test would have caught a regression in the old code path honestly rather than by coincidence.Thanks for the careful trace @MILLERMARRU — you're right that the
equals()delegation only works above the empty-keys guard, and that requiring both operands'equalsplus the matching constructor is what keeps it conservative. Appreciate the second set of eyes.FrancoKaddour(GitHub profile, opens in a new tab) linked this to fix #2195
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID:
364ea4e4-49c0-4609-a420-4ee4bf3e58c2📥 Commits
Reviewing files that changed from the base of the PR and between 7ed7642626cc92c966801a4a252457fec3ed87d3 and c424ccaea39bd4b1c72057bea351e5f268c45154.
📒 Files selected for processing (2)
packages/form-core/src/utils.tspackages/form-core/tests/utils.spec.ts🚧 Files skipped from review as they are similar to previous changes (2)
📝 Walkthrough
Walkthrough#
evaluatenow uses callableequals()methods when both objects share a constructor. Tests cover equal and differentPlainDateinstances and one-sidedequals()properties. A patch changeset documents support for custom value types.Changes#
Custom equality support
packages/form-core/src/utils.ts,packages/form-core/tests/utils.spec.ts,.changeset/evaluate-custom-equals.mdevaluateinvokes a callableequals()method before enumerable-key comparison when constructors match. Tests cover matching, nonmatching, and one-sidedequals()cases. The changeset documents the patch release.Estimated code review effort: 2 (Simple) | ~10 minutes
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#2195by using equals() for same-constructor value types, including Temporal-like instances.✨ Finishing Touches
🧪 Generate unit tests (beta)
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