Content Model Guide - Schema Design Patterns
Overview
Section titled “Overview”This guide covers best practices for designing content schemas in Astro using Content Collections. Well-designed schemas ensure type safety, content consistency, and excellent developer experience.
Core Principles
Section titled “Core Principles”- Type Safety First: Leverage Zod for runtime validation
- Future-Proof: Design for content evolution
- DRY: Reuse schema components
- Validation: Catch errors at build time
- Flexibility: Support various content needs
Schema Design Patterns
Section titled “Schema Design Patterns”1. Base Schema Pattern
Section titled “1. Base Schema Pattern”Create reusable base schemas for common fields:
import { z, defineCollection } from 'astro:content';
// Reusable schema componentsconst seoSchema = z.object({ metaTitle: z.string().max(60).optional(), metaDescription: z.string().max(160).optional(), ogImage: z.string().optional(), noindex: z.boolean().default(false),});
const authorSchema = z.object({ name: z.string(), email: z.string().email().optional(), avatar: z.string().optional(), bio: z.string().optional(), social: z.object({ twitter: z.string().optional(), github: z.string().optional(), linkedin: z.string().optional(), }).optional(),});
const