TanStack / TanStack/form

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

Open
#2,081 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

v1 v2: needs investigation
Dominant language
TypeScript
Stars
6.7k
Forks
682
Avg merge
5d 18h
Merged PRs (30d)
7

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction in the StackBlitz src/index.tsx, then trace how the invalid_union error with an empty path becomes the "" key in fieldErrors. Compare the observed mapping with the expected field-level or root-error behavior and add coverage for the reproduced z.union case.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.