Skip to content

fix: show validation error when field array is empty (#4981)#5143

Open
logaretm wants to merge 1 commit intomainfrom
fix/4981-empty-array-error
Open

fix: show validation error when field array is empty (#4981)#5143
logaretm wants to merge 1 commit intomainfrom
fix/4981-empty-array-error

Conversation

@logaretm
Copy link
Owner

@logaretm logaretm commented Mar 4, 2026

Summary

  • Fixes Error message doesn't show when array is empty #4981: When all items are removed from a field array, validation errors for the array itself (e.g., z.array().min(1)) were not being shown at the correct error path.
  • The root cause was findHoistedPath in validateSchema incorrectly hoisting array-level errors to a parent path state (e.g., error at mySettings.myArray was hoisted to mySettings).
  • The fix skips hoisting when the error path matches a known field array path, ensuring the error is placed at the correct path in extraErrorsBag.

Test plan

  • Added test: removing all items from a top-level field array shows the correct validation error at the array path
  • Added test: removing all items from a nested field array shows the error at the correct nested path (not hoisted to parent)
  • All existing tests pass (357 tests, 3 skipped)

🤖 Generated with Claude Code

When all items are removed from a field array, validation errors for the
array itself (e.g., min length) were being incorrectly hoisted to a parent
path by findHoistedPath. This fix skips hoisting when the error path
matches a known field array path, ensuring the error is placed at the
correct path.

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

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 5657037

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 5657037
🔍 Latest deploy log https://app.netlify.com/projects/vee-validate-docs/deploys/69a7d8a3d35d4500081596c7

@netlify
Copy link

netlify bot commented Mar 4, 2026

Deploy Preview for vee-validate-v5 ready!

Name Link
🔨 Latest commit 5657037
🔍 Latest deploy log https://app.netlify.com/projects/vee-validate-v5/deploys/69a7d8a36bf70d0007b27ce7
😎 Deploy Preview https://deploy-preview-5143--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 vee-validate schema error placement for field arrays becoming empty (e.g. z.array().min(1)), ensuring array-level errors are not incorrectly hoisted to a parent path state.

Changes:

  • Adjust validateSchema path-state resolution to skip findHoistedPath when the schema error path matches a registered field array path.
  • Add regression tests for top-level and nested object field arrays becoming empty.
  • Add a changeset entry for a patch release.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/vee-validate/src/useForm.ts Skips hoisting schema errors for known field-array paths so errors land on the array path.
packages/vee-validate/tests/useFieldArray.spec.ts Adds regression tests asserting array-level errors appear at the correct path when all items are removed.
.changeset/fix-4981-empty-array-error.md Declares a patch changeset for the fix.

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

Comment on lines +369 to +371
const isFieldArrayPath = fieldArrays.some(a => toValue(a.path) === expectedPath);
const pathState =
findPathState(expectedPath) || (isFieldArrayPath ? undefined : findHoistedPath(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.

isFieldArrayPath compares toValue(a.path) and expectedPath as raw strings. Standard-schema issues are converted using getDotPath, which yields dot-index paths (e.g. users.0.friends), while vee-validate commonly normalizes array indices to bracket syntax (e.g. users[0].friends). If a field array path includes numeric indices, this equality check will fail and hoisting will still occur, leaving the original bug unfixed for those cases. Consider normalizing both sides (and ideally precomputing a normalized Set of field array paths once per validation run) before doing the comparison.

Copilot uses AI. Check for mistakes.
Comment on lines +528 to +544
mountWithHoc({
setup() {
form = useForm<any>({
initialValues: {
users: ['one'],
},
validationSchema: z.object({
users: z.array(z.string()).min(1, 'At least one item is required'),
}),
});

arr = useFieldArray('users');
},
template: `
<div></div>
`,
});
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.

These new tests mount an empty template and only call useForm + useFieldArray, which (per useFieldArray.ts) does not register any PathStates. Without a parent PathState candidate, findHoistedPath() would return undefined even on the pre-fix code path, so the tests may pass without actually covering the hoisting bug described in #4981. Consider rendering/registering a parent field (e.g. a <Field name="settings" /> / useField('settings'), or reproducing the setFieldValue('settings', ...) pattern from the issue) so the tests fail on the buggy behavior and validate the fix.

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.

Error message doesn't show when array is empty

2 participants