fix(form-core): clear errorSourceMap.onMount alongside errorMap.onMount #2362
main fromrefs/pull/2362/head·opened 🎯 Changes#
Fixes #2294.
FormApi.setFieldValue clears errorMap.onMount on the field's meta but leaves errorSourceMap.onMount untouched, so field.state.meta.errorSourceMap.onMount keeps reporting a source ('field' or 'form') for an error that no longer exists in errorMap. Every other field-meta write in FormApi.ts updates the two maps together, so this reads as an omission rather than intent.
This adds the matching errorSourceMap update inside the same setFieldMeta call, and two regression tests in FormApi.spec.ts covering the 'field' source (field-level onMount validator) and the 'form' source (form-level onMount validator emitting a field error).
alpha is not affected: errorSourceMap does not appear in any package on that branch, so there is nothing to port to v2.
Note that #2245 edits the same block for an unrelated reason. There is no functional overlap; happy to rebase if it lands first.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm test:pr.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Clearing a field value now consistently removes its initial validation errors, including errors originating from field- or form-level validation.
- Updated field state now stays synchronized after values are changed.
- 1
Fix looks right for #2294. I traced the other field-meta writes to check the "every other write updates both maps together" claim:
FieldApi.ts:889-890,1324-1328,1362-1366,1561-1566all pairerrorMapwitherrorSourceMap, andFormApi.ts:2073-2077,2289-2293,2992-2996do too. SosetFieldValuewas the one field-level omission and this closes it.One spot that stays asymmetric after this PR:
FormApi.ts:1566,errorMap = Object.assign(errorMap, { onMount: undefined })in the base-store derived state (shouldInvalidateOnMountbranch). That clears the form-levelerrorMap.onMountwith no matchingerrorSourceMapwrite. It's only fine if the form-level base store has noerrorSourceMapfield at all (theerrorSourceMapwrites I found are all field-meta, none form-level). Could you confirm that's the case, so it's clear this isn't a second instance of the same bug one scope up? Separately, that line mutatescurrBaseStore.errorMapin place rather than copying, which is a pre-existing smell in a.stateread but not something this PR needs to touch.Confirmed, errorSourceMap is field-only. BaseFormState has none, so 1566 is fine.
alliasgher(GitHub profile, opens in a new tab) linked this to fix #2294
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:
533bc64b-7798-4859-a71e-706df265d339📥 Commits
Reviewing files that changed from the base of the PR and between 57a855b42b9b99cee5ca12890b70c7df84e2982c and ec0512d8000293d1b1b2a766e4f96b15eefd4de5.
📒 Files selected for processing (3)
.changeset/olive-forms-clear.mdpackages/form-core/src/FormApi.tspackages/form-core/tests/FormApi.spec.tsIncluded review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
Walkthrough#
setFieldValuenow clearserrorSourceMap.onMounttogether witherrorMap.onMount. Tests cover field-sourced and form-sourced errors. A changeset records a patch release for@tanstack/form-core.Changes#
Form error clearing
packages/form-core/src/FormApi.ts,.changeset/olive-forms-clear.mdsetFieldValueclearserrorSourceMap.onMountwhen it clearserrorMap.onMount. The changeset records a patch release.packages/form-core/tests/FormApi.spec.tsonMounterrors and source markers clear after setting a field value.Estimated code review effort: 2 (Simple) | ~10 minutes
Merge Risk: ⚪ Minimal · up to
ec051This localized change clears stale field-error source metadata alongside the error itself and adds regression coverage for both supported sources; no actionable merge-blocking risk remains.
Suggested reviewers:
pascalmh🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#2294, documents testing, and confirms the changeset.#2294by clearing errorSourceMap.onMount in setFieldValue and adding regression tests for field-level and form-level error sources.Full details: Docstring Coverage
Explanation
No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files. (1 skipped: 1 unsupported.)
✨ 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