Skip to content

z.union errors surface under "" key instead of field-level errors #2081

@astahmer

Description

@astahmer

Describe the bug

Body:

When using a Zod z.union as a form schema, validation failures produce an invalid_union error with path: [] (root). TanStack Form maps this to the "" (empty string) key in fieldErrors, making it impossible to associate the error with any specific field.

Repro:

const schema = z.union([
  z.object({ firstName: z.string().min(1), lastName: z.string().min(1) }),
  z.object({ email: z.string() }),
]);
// On failure: fieldErrors = { "": ["Invalid input"] }

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-zcs7lghv?file=src%2Findex.tsx&preset=node

Steps to reproduce

submit + inspect console.logs

Expected behavior

errors should surface on the relevant fields, or there should be a supported way to map root-level union errors to specific field paths.

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

any

TanStack Form adapter

None

TanStack Form version

1.28.5

TypeScript version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions