Skip to content

React useForm: nested validation error paths are rejected by types and survive parent updates #875

Open

Description

@karthickeyan-dev

Summary

Server validation supports nested objects and arrays and returns dotted error keys such as profile.name and contacts.0.email. React's useForm() receives these keys at runtime, but its error types and error methods accept only top-level body keys.

After a validation failure, replacing a parent object or array with setData() also leaves its descendant errors behind. The same happens with clearErrors('profile') or clearErrors('contacts').

Versions

  • void: 1.10.0
  • @void/react: 1.10.0
  • React / React DOM: 19.3.1
  • TypeScript: 7.0.2, with strict: true
  • Zod: 4.6.7

Reproduction

In a React Pages app, add this action:

// pages/repro/index.server.ts
import { defineHandler } from 'void';
import { z } from 'zod';

export const loader = defineHandler(() => ({}));

export const action = defineHandler.withValidator({
  body: z.object({
    profile: z.object({ name: z.string().min(1, 'Required name') }),
    contacts: z.array(z.object({ email: z.string().min(1, 'Required email') })),
  }),
})(() => {});

Add a page with buttons to replace each parent after validation fails:

// pages/repro/index.tsx
import { Form, useForm } from '@void/react';

export default function ReproPage() {
  const form = useForm('/repro', {
    profile: { name: '' },
    contacts: [{ email: '' }],
  });

  return (
    <Form form={form}>
      <pre>{JSON.stringify({ data: form.data, errors: form.errors }, null, 2)}</pre>
      <button type="submit" disabled={form.pending}>
        Validate
      </button>
      <button type="button" onClick={() => form.setData('profile', { name: 'Alex' })}>
        Fix profile
      </button>
      <button
        type="button"
        onClick={() => form.setData('contacts', [{ email: 'alex@example.com' }])}
      >
        Fix contacts
      </button>
      <button type="button" onClick={() => form.clearErrors('profile', 'contacts')}>
        Clear parent errors
      </button>
      <button type="button" onClick={() => form.clearErrors()}>
        Clear all errors
      </button>
    </Form>
  );
}
  1. Open /repro and click Validate. The error map contains profile.name and contacts.0.email.
  2. Click Fix profile and Fix contacts. The data changes, but both old errors remain.
  3. Click Clear parent errors. Both errors still remain.
  4. Click Clear all errors. Both errors disappear.

Inside the same component, these expressions also fail type checking:

form.errors['profile.name']; // TS7053
form.errors['contacts.0.email']; // TS7053
form.setError('profile.name', 'Required name'); // TS2345
form.clearErrors('profile.name'); // TS2345
form.clearErrors('contacts.0.email'); // TS2345

Calling clearErrors('profile.name') in JavaScript does clear that error; the public TypeScript signature rejects it.

Expected behavior

  • Error access, setError() and clearErrors() accept the nested paths that server validation returns, including array indices.
  • Changing a parent object or array clears its descendant errors, consistent with changing a top-level scalar clearing that field's error.
  • Clearing errors for a parent also clears descendant paths, without clearing unrelated errors.

Verification and likely cause

Confirmed against the installed 1.10.0 packages using Hono and Void's validation implementation to generate the dotted paths, then React DOM with happy-dom and a stub router returning those validation errors. A top-level scalar error clears when changed; the object and array errors survive both parent updates and parent error clearing. A separate TypeScript check produces the five diagnostics above. The page example above is a reproduction recipe; it was not run as a separate browser app.

The current source at a0dab707d3f4d54368696d071cce40efaf893e50 still defines FormErrors and the error methods using keyof TData. React's setData and clearErrors only remove exact error keys. Runtime verification covered React 1.10.0; other adapters were not tested.

Impact and workaround

A form with a nested profile and an array of contacts can keep showing validation errors after those values have been corrected. Reading or clearing an individual nested error requires bypassing the public types.

For now, a cast allows reading the dotted errors, and clearErrors() removes them after a parent update. Clearing all errors also removes messages for unrelated fields.

Activity

  1. self-assigned this
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions