Defining Objects in Zod

From the Zod cheat sheet ยท Object Schemas ยท verified Jul 2026

Defining Objects

Create object schemas with required and optional properties

typescript
const UserSchema = z.object({
  id: z.uuid(),
  name: z.string(),
  email: z.email(),
  age: z.number().optional(),
  createdAt: z.date(),
});

type User = z.infer<typeof UserSchema>;
๐Ÿ’ก z.object() strips unknown keys by default โ€” use strictObject to reject them
โšก z.infer extracts a TypeScript type from a schema โ€” single source of truth
๐Ÿ“Œ Optional properties become T | undefined in the inferred type
๐ŸŸข Use catchall() to validate dynamic keys against a fallback schema
objectschema

More Zod tasks

Back to the full Zod cheat sheet