defineCollection()

defineCollection() declares a content collection's loader and Zod schema in the content config.

Since Astro 2.0 Spec ↗

Syntax

const blog = defineCollection({ loader, schema })

Returns

CollectionConfig — A collection definition object exported from the collections map.

Examples

// src/content.config.ts
import { defineCollection, z } from 'astro:content';
import { glob } from 'astro/loaders';

const blog = defineCollection({
  loader: glob({ pattern: '**/*.md', base: './src/data/blog' }),
  schema: z.object({
    title: z.string(),
    date: z.date(),
    draft: z.boolean().default(false),
  }),
});

export const collections = { blog };
Output
Registers a "blog" collection whose Markdown frontmatter is validated against the Zod schema at build time.

Notes

Lives in src/content.config.ts. The Content Layer API uses loaders (glob, file, or custom). Schema validation gives full TypeScript types via CollectionEntry. Use image() helper for asset fields.

See also