fix: preserve sibling fields in setFieldValue (#4999)#5130
Conversation
Normalize paths in createPathState to ensure consistent comparison with the UNSET_BATCH entries and pathStateLookup keys. Previously, paths using dot notation for array indices (e.g., 'items.0.name') would not match their bracket-normalized equivalents (e.g., 'items[0].name') in the UNSET_BATCH, preventing proper cancellation of pending unset operations when a new path state was created via setFieldValue. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 463c27b The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for vee-validate-docs canceled.
|
✅ Deploy Preview for vee-validate-v5 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
Fixes vee-validate issue #4999 where calling setFieldValue on a nested property (especially within arrays of objects) could interact incorrectly with path-state bookkeeping due to inconsistent path formats (dot vs bracket array indices).
Changes:
- Normalizes field paths inside
createPathStateto makepathStateLookupaccess andUNSET_BATCHcancellation consistent for array-indexed paths. - Adds regression tests covering the reported scenario across: basic usage, Zod schema, and
FieldArray+Fieldcomponent usage. - Adds a patch changeset for release notes/versioning.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/vee-validate/src/useForm.ts | Normalizes paths in createPathState when interacting with lookup and pending-unset bookkeeping. |
| packages/vee-validate/tests/useForm.spec.ts | Adds three regression tests for nested updates within arrays of objects. |
| .changeset/fix-4999-setfieldvalue-object-loss.md | Patch changeset entry for the fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (errors.value[normalizedPathValue] && !initialErrors[normalizedPathValue]) { | ||
| nextTick(() => { | ||
| validateField(pathValue, { mode: 'silent' }); | ||
| validateField(normalizedPathValue, { mode: 'silent' }); | ||
| }); |
There was a problem hiding this comment.
initialErrors is a raw copy of opts.initialErrors (not normalized), but this check now indexes it with normalizedPathValue. If a consumer provides initial errors using dot-index paths (e.g. items.0.name), initialErrors[normalizedPathValue] will be undefined and this will incorrectly trigger a silent validateField, potentially clearing/changing the intended initial error state. Consider normalizing initialErrors keys up front, or normalize on access here (e.g., check both raw + normalized forms consistently).
Summary
setFieldValuelosing object fields when updating a nested property within an array of objectscreatePathStateto ensure consistent comparison withUNSET_BATCHentries andpathStateLookupkeysitems.0.name) would not match their bracket-normalized equivalents (e.g.,items[0].name) in theUNSET_BATCH, preventing proper cancellation of pending unset operationsChanges
packages/vee-validate/src/useForm.ts: Normalize paths increatePathStateforpathStateLookupaccess,UNSET_BATCHcomparison, error lookup, and path state storagepackages/vee-validate/tests/useForm.spec.ts: Add 3 regression tests covering the issue scenario with various configurations (basic, Zod schema, FieldArray + Field components).changeset/fix-4999-setfieldvalue-object-loss.md: Changeset for patch releaseTest plan
setFieldValue preserves sibling fields when updating nested property in array of objectssetFieldValue preserves sibling fields with individually registered nested fieldssetFieldValue preserves sibling fields with Field components inside FieldArray🤖 Generated with Claude Code