X Tutup
Skip to content

fix: initial meta.valid is false with schema (#4855)#5127

Open
logaretm wants to merge 2 commits intomainfrom
fix/4855-initial-meta-valid
Open

fix: initial meta.valid is false with schema (#4855)#5127
logaretm wants to merge 2 commits intomainfrom
fix/4855-initial-meta-valid

Conversation

@logaretm
Copy link
Owner

@logaretm logaretm commented Mar 4, 2026

Summary

  • Fixes Wrong Initial Value of meta.valid provided with validation schema on initial page load #4855: meta.valid incorrectly started as true even when a validation schema was provided and fields hadn't been validated yet
  • Root cause: [].every(() => ...) returns true (vacuous truth). When no path states existed yet, useFormMeta computed valid as true
  • New PathState entries now start with valid: false when a schema is present, and useFormMeta returns false for the valid flag when there are no path states but a schema exists

Test plan

  • Added test: meta.valid should be false initially when a validation schema is provided
  • Added test: meta.valid should be true initially when no validation schema is provided
  • Updated 3 existing useFieldArray tests that incorrectly assumed valid: true with a schema and no registered field path states
  • All 357 tests pass (3 skipped, 3 pre-existing infrastructure failures unrelated to this change)

🤖 Generated with Claude Code

Previously meta.valid started as true due to vacuous truth from
[].every(). Now correctly returns false when a schema exists but
fields haven't been validated yet, and new PathState entries start
with valid: false when a schema is present.

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

changeset-bot bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 469669a

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-v5 ready!

Name Link
🔨 Latest commit 469669a
🔍 Latest deploy log https://app.netlify.com/projects/vee-validate-v5/deploys/69a7c98268d12500084ab6d8
😎 Deploy Preview https://deploy-preview-5127--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.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@netlify
Copy link

netlify bot commented Mar 4, 2026

Deploy Preview for vee-validate-docs canceled.

Name Link
🔨 Latest commit 469669a
🔍 Latest deploy log https://app.netlify.com/projects/vee-validate-docs/deploys/69a7c982d35d45000812e92b

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 meta.valid reporting true initially when a validation schema is present but no fields/path states have been validated/registered yet (issue #4855), avoiding the [].every(...) === true vacuous-truth edge case.

Changes:

  • Passes schema presence into useFormMeta and forces valid=false when a schema exists and there are no path states.
  • Initializes new PathState.valid to false when a schema is present.
  • Adds/updates tests around initial meta.valid behavior with/without schemas.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
packages/vee-validate/src/useForm.ts Adjusts initial valid defaults and form meta aggregation to account for schema + empty path state scenarios.
packages/vee-validate/tests/useForm.spec.ts Adds new tests for initial meta.valid with and without a schema.
packages/vee-validate/tests/useFieldArray.spec.ts Updates existing field array tests to align with the new initial meta.valid semantics.
Comments suppressed due to low confidence (7)

packages/vee-validate/src/useForm.ts:248

  • useFormMeta is passed !!opts?.validationSchema, but validationSchema supports refs/computed (see isRef(schema) watcher). !!opts.validationSchema will be true for a ref even when unref(schema) is currently null/undefined, and it won’t update if the schema ref toggles. Consider passing a reactive boolean derived from unref(schema) (or passing schema into useFormMeta and unref’ing inside) so meta.valid reflects whether a schema is actually active.
  // form meta aggregations
  const meta = useFormMeta(pathStates, formValues, originalInitialValues, errors, !!opts?.validationSchema);

packages/vee-validate/src/useForm.ts:1131

  • The vacuous-truth override (flag === 'valid' && hasSchema && !states.length) depends on hasSchema, but validationSchema supports refs/computed. If the schema ref toggles while there are no path states, a non-reactive boolean can make meta.valid incorrect. Consider making hasSchema reactive (derived from unref(schema) each time) so this stays in sync.
        if (flag === 'valid' && hasSchema && !states.length) {
          acc[flag] = false;
        } else {
          acc[flag] = states[mergeMethod](s => s[flag]);

packages/vee-validate/tests/useFieldArray.spec.ts:192

  • This test no longer asserts the initial form.meta.value.valid state; it was replaced by a comment. Since the behavior change in #4855 is specifically about the initial meta.valid value when a schema exists and no path states are registered, add an explicit expectation here (before arr.push) so the regression is actually covered by the test.
  await flushPromises();
  // With a schema and no registered field path states, meta.valid is false (#4855)
  arr.push('');
  await flushPromises();

packages/vee-validate/tests/useFieldArray.spec.ts:248

  • This test removed the assertion of the initial form.meta.value.valid value and left only a comment. Add an explicit expect(form.meta.value.valid).toBe(false) (before arr.prepend) to ensure the updated #4855 behavior is actually exercised.
  await flushPromises();
  // With a schema and no registered field path states, meta.valid is false (#4855)
  arr.prepend('');
  await flushPromises();

packages/vee-validate/tests/useFieldArray.spec.ts:304

  • This test similarly replaced the initial meta.valid assertion with a comment. Add an explicit expectation before arr.insert so the initial invalid state with a schema and no registered path states is verified (not just implied).
  await flushPromises();
  // With a schema and no registered field path states, meta.valid is false (#4855)
  arr.insert(1, '');
  await flushPromises();

packages/vee-validate/tests/useForm.spec.ts:1502

  • This test registers a field (useField('name')), so it doesn’t exercise the useFormMeta vacuous-truth fix (schema present + pathStates still empty). Add a test that provides validationSchema but registers no fields, and assert form.meta.value.valid is false initially.
        form = useForm({
          initialValues: { name: '' },
          validationSchema: {
            name(value: string) {
              return value ? true : 'Name is required';

packages/vee-validate/tests/useForm.spec.ts:1499

  • These new tests type form as Record<string, any>, while the rest of this file commonly uses FormContext<...> for useForm() results. Using FormContext here would keep the test typings consistent and provide better compile-time checking of the API surface.
    let form!: Record<string, any>;
    mountWithHoc({
      setup() {
        form = useForm({
          initialValues: { name: '' },

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

touched: false,
pending: false,
valid: true,
valid: !schema,
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.

valid: !schema is incorrect when validationSchema is passed as a Ref/computed (supported elsewhere in this file): the ref object is always truthy, so new path states start invalid even if unref(schema) is currently falsy. Initialize this flag from the schema value (e.g. !unref(schema)) to stay consistent with how formCtx.validateSchema is enabled.

Suggested change
valid: !schema,
valid: !unref(schema),

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.

Wrong Initial Value of meta.valid provided with validation schema on initial page load

2 participants

X Tutup