TanStackT
TanStack7mo ago
18 replies
unsightly-emerald

Validate object field as a single value with Zod

I'm building a form to create an organization in my project. The form includes an address field, which is an object, as shown in the attached schema.

The issue I'm facing is with how validation errors are handled. When I submit the form, the errors returned by props.formApi.getAllErrors() put all the address-related errors into form.errors, instead of nesting them under fields.address.

Currently, the only way I can validate the address field as an object is by attaching the validator directly to the field itself. However, this approach causes only the address field to be validated on submit (if there is an error on this field), not the entire form.

Is there a way, either with Zod or TanStack Form, to validate an object field (like address) directly as a single value — meaning, treating the field as a whole object — instead of validating it through individual nested keys?

I'm looking for something like a mode="value" to treat the object as the field's value, and maybe something like mode="object" to allow deep key error handling — similar to how mode="array" works for arrays.
image.png
Was this page helpful?