import { z } from 'zod'
export const Customer = z.object({
firstName: z.string(),
lastName: z.string(),
age: z.number(),
address: z.object({
streetAddress: z.string(),
city: z.string(),
state: z.string(),
postalCode: z.string(),
}),
contact: z.array(
z.object({
type: z.string(),
value: z.string(),
}),
),
})
// type and value have the same name
export type Customer = z.infer<typeof Customer>
Typescript can fully infer the type from the value:
// either throws an error, or it's a valid Customer
const customer = Customer.parse(request.body)