Fix #191: Prevent crash when unregistering renamed field#533
Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 WalkthroughWalkthroughAdds defensive checks to FinalForm's field unregister path to safely handle mutator-induced field renames without crashing, plus a regression test verifying the fix for field rename/unregister stability. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Comment |
- Added guards for validators and fieldSubscribers existence - Prevents crash when field is renamed by mutator before unregister - Test simulates rename + unregister scenario
40fbc5e to
0e58277
Compare
erikras-dinesh-agent
left a comment
There was a problem hiding this comment.
LGTM! ✅
Good defensive guards in the unregister cleanup path:
- Added
.validatorscheck before accessing validators - Added
.entriescheck before accessing fieldSubscribers entries - Added extra guards on the
lastOnecalculation
Test is well-designed — uses a renameField mutator to reproduce the real-world scenario where field state moves to a new name, leaving the old unregister closure with a stale name reference. Bundle size bump justified.
Approved — CodeRabbit status is dismissed (clean).
erikras-richard-agent
left a comment
There was a problem hiding this comment.
LGTM! The null guards in the unregister path are correct — checking state.fields[name].validators and state.fieldSubscribers[name].entries before accessing prevents crashes when fields are renamed mid-lifecycle. CI all green. ✅
|
Published in |
Problem: When a field is renamed using a mutator (e.g.,
renameField), calling the unregister function from the originalregisterFieldcall crashes with "Cannot read property 'validators' of undefined". This happens because the unregister closure references the old field name, which no longer exists instate.fieldsafter the rename.Root Cause: The unregister function is a closure over the original field
name. When a mutator renames the field:state.fields[oldName]tostate.fields[newName]oldNamestate.fields[oldName].validators, which is now undefinedSolution: Added defensive guards in the unregister function:
state.fields[name]ANDstate.fields[name].validatorsexist before accessingstate.fieldSubscribers[name]ANDstate.fieldSubscribers[name].entriesexist before accessingTesting:
Fixes #191
Summary by CodeRabbit
Bug Fixes
Tests