Skip to content

fix: useField with object values now validates correctly (#5088)#5142

Open
logaretm wants to merge 1 commit intomainfrom
fix/5088-object-field-validation
Open

fix: useField with object values now validates correctly (#5088)#5142
logaretm wants to merge 1 commit intomainfrom
fix/5088-object-field-validation

Conversation

@logaretm
Copy link
Owner

@logaretm logaretm commented Mar 4, 2026

Summary

  • Fixes useField with object values not validating. #5088: When using useField with object values and a schema that reports validation errors at nested sub-paths (e.g., yup reporting errors at someObject.id for a field registered as someObject), the errors were stored in extraErrorsBag and never cleaned up during subsequent validations.
  • This caused form.meta.valid to remain false even after resetForm with valid values, because stale nested-path errors persisted in the extra error bag.
  • Two fixes applied:
    1. In the validateSchema callback, also clean up extraErrorsBag entries for the original error path when it gets hoisted to a parent field path.
    2. In resetForm, clear extraErrorsBag before setting new errors to ensure stale nested-path errors don't persist across resets.

Test plan

  • Added regression test that uses yup schema with nested object validation, delayed field rendering via v-if, and resetForm with valid values
  • Verified all existing tests pass (356 tests, 3 pre-existing unrelated failures in package resolution)
  • Pre-commit hooks (lint, prettier, vitest) pass

🤖 Generated with Claude Code

When a schema (e.g., yup) reports validation errors at nested sub-paths
(like 'someObject.id') but the field is registered at a parent path
(like 'someObject'), the errors were stored in extraErrorsBag and never
cleaned up. This caused form.meta.valid to remain false even after
resetForm with valid values.

Two fixes:
1. In validateSchema callback, also clean up extraErrorsBag entries for
   the original error path when it gets hoisted to a parent field path.
2. In resetForm, clear extraErrorsBag before setting new errors to
   ensure stale nested-path errors don't persist.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 4, 2026 06:56
@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 2b5f759

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

@netlify
Copy link

netlify bot commented Mar 4, 2026

Deploy Preview for vee-validate-docs canceled.

Name Link
🔨 Latest commit 2b5f759
🔍 Latest deploy log https://app.netlify.com/projects/vee-validate-docs/deploys/69a7d783f61a700008a6f1e5

@netlify
Copy link

netlify bot commented Mar 4, 2026

Deploy Preview for vee-validate-v5 ready!

Name Link
🔨 Latest commit 2b5f759
🔍 Latest deploy log https://app.netlify.com/projects/vee-validate-v5/deploys/69a7d78313572e000851dbc3
😎 Deploy Preview https://deploy-preview-5142--vee-validate-v5.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a vee-validate schema-validation edge case where nested-path schema errors (e.g. someObject.id) could be stored in extraErrorsBag and remain stale after subsequent validations/resets, causing form.meta.valid to stay false incorrectly.

Changes:

  • Clean up extraErrorsBag entries when schema errors are hoisted from a nested path to a parent field path during schema validation.
  • Clear extraErrorsBag during resetForm() to prevent stale unmounted-field errors from persisting across resets.
  • Add a regression test covering delayed field rendering + resetForm() with valid object values and a yup schema.

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 Deletes hoisted nested-path entries from extraErrorsBag during schema validation; clears extraErrorsBag on resetForm().
packages/vee-validate/tests/useForm.spec.ts Adds a regression test reproducing the stale nested-path error scenario with delayed rendering and resetForm().
.changeset/fix-5088-object-field-validation.md Adds a patch changeset entry for the fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +389 to +394
// Also clean up extra errors for the expected path when it was hoisted to a different path
// This handles cases where schema reports errors at nested sub-paths (e.g., 'someObject.id')
// but the field is registered at a parent path (e.g., 'someObject')
if (pathState && expectedPath !== path && extraErrorsBag.value[expectedPath]) {
delete extraErrorsBag.value[expectedPath];
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraErrorsBag keys are stored using normalizeFormPath(), but this cleanup checks/deletes using the raw expectedPath. If the schema reports a path using dot-index notation (e.g. items.0.id) while extraErrorsBag stores items[0].id, the stale entry won’t be removed. Normalize expectedPath (and use it for the comparison) before reading/deleting from extraErrorsBag to ensure cleanup works for all path formats.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useField with object values not validating.

2 participants