How do I merge two Zod schemas?

Hey guys, I am creating a form with react-hook-form and Zod. The form was too long, so I decided to divide it into multi step form, with each step having separate Zod schema. I am storing the form data locally until all steps are complete. Now, I want a Zod schema for combining the schemas and perform a validation before I send the data to the server. I am unable to find the right solution. Can somebody please help me?
Solution
const schema1 = z.object({
  name: z.string(),
});

const schema2 = z.object({
  age: z.number(),
});

const schema3 = schema1.and(schema2)
Was this page helpful?