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