Organizing content in Astro projects helps keep blog posts, projects, or other data consistent and easy to manage.
💼 Career
Knowing how to define collections and schemas is essential for building structured content-driven websites with Astro, a modern web framework.
Progress0 / 4 steps
1
Create the posts collection variable
Create a constant called posts and assign it the result of calling defineCollection() with no arguments.
Astro
Hint
Use const posts = defineCollection() to create the collection variable.
2
Add a schema to the posts collection
Modify the posts constant to call defineCollection with an object that has a schema property. Use z.object to define a schema with required string fields: title, date, and author.
But when building, you get an error about the year field. What is the likely cause?
medium
A. The content file has year as a string instead of a number
B. The name field is missing in the content file
C. The schema is missing a required property for name
D. Astro does not support number types in schemas
Solution
Step 1: Understand schema type enforcement
Astro checks that the content field types match the schema types exactly.
Step 2: Identify mismatch cause
If year is defined as a number but the content file has it as a string, it causes a type error.
Final Answer:
The content file has year as a string instead of a number -> Option A
Quick Check:
Type mismatch between schema and content causes errors = B [OK]
Hint: Match content field types exactly to schema types [OK]
Common Mistakes:
Ignoring type mismatches in content files
Assuming missing required fields cause this error
Thinking Astro disallows number types
5. You want to define a collection schema in Astro where each item must have a title (string), an optional tags array of strings, and a date that must be a valid ISO date string. Which schema definition is correct?