TanStackT
TanStack15mo ago
3 replies
near-sapphire

How to handle nested object validation

I am using Zod to perform the validation, and I have confirmed, that all of the errors.
I have the following schema:
const addressSchema = z.object({
    country: z.string().min(1),
    region: z.string().min(1),
    address: z.string().optional()
});
const contactSchema = z.object({
    firstName: z.string(),
    lastName: z.string(), 
    address: addressSchema
})


I have stepped through the code, and can confirm that when country, and region are missing, the two keys are created for "address.country", and "address.region",

You can even see the error message in the array when using looking at form.state.errors, how ever you cannot access the errors for address.

The only way I can see to find the errors is to use the array of all error strings.

Is there a way I am missing to get the errors for "address", or specifically "address.country" ect.

(worth noting that when using form.getFieldInfo("adress") i get all the relevant information for this field except the errors, but calling form.getFieldInfo("address.country") returns instance of null,)
Was this page helpful?