Collections and schemas help organize your content and make sure it follows rules. This keeps your site neat and easy to manage.
Defining collections and schemas in Astro
import { defineCollection, z } from 'astro:content'; const blogCollection = defineCollection({ schema: z.object({ title: z.string(), date: z.string().transform(str => new Date(str)), description: z.string().optional(), }), }); export const collections = { blog: blogCollection, };
Use defineCollection to create a group of content items.
Use z.object from Zod to define the shape and rules of your content.
const projectCollection = defineCollection({
schema: z.object({
name: z.string(),
repoUrl: z.string().url(),
tags: z.array(z.string()).optional(),
}),
});const eventCollection = defineCollection({
schema: z.object({
title: z.string(),
date: z.string(),
location: z.string().optional(),
}),
});This example creates a blog collection with a schema that requires a title and date. Description is optional. The date string is converted to a Date object for easier use.
import { defineCollection, z } from 'astro:content'; const blogCollection = defineCollection({ schema: z.object({ title: z.string(), date: z.string().transform(str => new Date(str)), description: z.string().optional(), }), }); export const collections = { blog: blogCollection, };
Always export your collections object so Astro can find your content groups.
Use optional fields for content that may not always be present.
Transformations like converting date strings to Date objects help later when displaying content.
Collections group similar content items in Astro.
Schemas define the shape and rules for content fields.
Use defineCollection and Zod schemas to keep content organized and valid.
Practice
collection in Astro?Solution
Step 1: Understand what collections represent
Collections in Astro are used to organize related content files, like blog posts or projects, into one group.Step 2: Identify the purpose of collections
This grouping helps manage content easily and apply schemas to all items in the collection.Final Answer:
To group related content files together for easier management -> Option AQuick Check:
Collections group content = A [OK]
- Confusing collections with styling or scripts
- Thinking collections define UI components
- Assuming collections are for API routes
title in Astro's collection schema?Solution
Step 1: Recall schema field syntax in Astro
Astro schemas use objects withtypeandrequiredproperties to define fields.Step 2: Match the correct field definition
The fieldtitlemust be a string and required, so{ type: 'string', required: true }is correct.Final Answer:
"title: { type: 'string', required: true }" -> Option BQuick Check:
Schema field with type string and required true = C [OK]
- Using primitive types directly without object
- Setting wrong type like number for string fields
- Omitting required property or setting it false
const blogSchema = {
title: { type: 'string', required: true },
published: { type: 'boolean', required: false }
};What happens if a blog post content file omits the
published field?Solution
Step 1: Check the
The fieldrequiredproperty ofpublishedpublishedhasrequired: false, so it is optional.Step 2: Understand schema validation behavior
Omitting optional fields does not cause errors; the content remains valid.Final Answer:
The content is valid becausepublishedis optional -> Option DQuick Check:
Optional fields can be omitted = A [OK]
- Assuming all fields are required by default
- Thinking missing optional fields cause errors
- Believing default values are set automatically
const projectSchema = {
name: { type: 'string', required: true },
year: { type: 'number', required: true }
};But when building, you get an error about the
year field. What is the likely cause?Solution
Step 1: Understand schema type enforcement
Astro checks that the content field types match the schema types exactly.Step 2: Identify mismatch cause
Ifyearis defined as a number but the content file has it as a string, it causes a type error.Final Answer:
The content file hasyearas a string instead of a number -> Option AQuick Check:
Type mismatch between schema and content causes errors = B [OK]
- Ignoring type mismatches in content files
- Assuming missing required fields cause this error
- Thinking Astro disallows number types
title (string), an optional tags array of strings, and a date that must be a valid ISO date string. Which schema definition is correct?Solution
Step 1: Define required string field
Usetitle{ type: 'string', required: true }fortitle.Step 2: Define optional array of strings
Usetags{ type: 'array', items: { type: 'string' }, required: false }to specify an optional array of strings.Step 3: Define
Usedateas required ISO date string{ type: 'string', format: 'date-time', required: true }to enforce ISO date format.Final Answer:
{ title: { type: 'string', required: true }, tags: { type: 'array', items: { type: 'string' }, required: false }, date: { type: 'string', format: 'date-time', required: true } } -> Option CQuick Check:
Correct types and required flags with array and date format = D [OK]
- Using incorrect type syntax for arrays
- Omitting required flags or setting wrong ones
- Not specifying date format for ISO strings
Ready to prove it?
Pass the test and this pattern moves unknown
