fix(form-core): don't throw when a group validator errors an unmounted field#2233
fix(form-core): don't throw when a group validator errors an unmounted field#2233momomuchu wants to merge 2 commits into
Conversation
…unted field meta FormGroupApi.distributeFieldErrors called setFieldMeta with an updater that unconditionally spread prev, which is undefined when a group validator returns fields errors for a sub-field with no fieldMetaBase entry yet. Apply the same (prev = defaultFieldMeta) guard already used in FormApi's form-level field-error fan-out (see TanStack#1706, TanStack#2056). Fixes TanStack#2231
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughFixes a TypeError in FormGroupApi.distributeFieldErrors that occurred when a group validator returned errors for a field not yet mounted, by defaulting the previous field meta to defaultFieldMeta. Adds a regression test and a changeset entry. ChangesFormGroupApi Unmounted Field Fix
Estimated code review effort: 1 (Trivial) | ~5 minutes Sequence Diagram(s)sequenceDiagram
participant Validator
participant FormGroupApi
participant FormApi
Validator->>FormGroupApi: return fields errors for unmounted field
FormGroupApi->>FormApi: setFieldMeta(fullName, updater)
FormApi->>FormGroupApi: updater(prev = undefined)
FormGroupApi->>FormGroupApi: default prev to defaultFieldMeta
FormGroupApi-->>FormApi: merged errorMap/errorSourceMap
Related issues: Suggested labels: bug, form-core, patch Suggested reviewers: none identified from provided context 🐰 A field not yet born, no meta to its name, 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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. Comment |
FormGroupApi.distributeFieldErrorsthrowsTypeError: Cannot read properties of undefined (reading 'errorMap')when aFormGroupvalidator returns{ fields }errors for a sub-field that is not mounted yet (nofieldMetaBaseentry). This happens in multi-step forms where each step is aFormGroupand fields render progressively.setFieldMeta's updater is invoked withprev.fieldMetaBase[field], which isundefinedfor a never-mounted field. The fix defaults the updater parameter todefaultFieldMeta((prev = defaultFieldMeta) => ...), mirroring the same fix already applied inFormApi.tsfor this bug class (#1706, #2056). The distributed error is stored and surfaces correctly when the field later mounts.Added a
FormGroupApi.spec.tscase: a group validator returns errors for an unmounted field, validate runs, and it must not throw. It throws the reported TypeError without the fix and passes with it. Full form-core suite (499 tests) and type tests pass, eslint and tsc clean.Closes #2231
Summary by CodeRabbit
Bug Fixes
Tests