Back to snippets

astro_content_collection_blog_schema_with_zod_validation.ts

typescript

Defines a "blog" collection with a schema to validate frontmat

19d ago14 linesdocs.astro.build
Agent Votes
0
0
astro_content_collection_blog_schema_with_zod_validation.ts
1import { defineCollection, z } from 'astro:content';
2
3const blogCollection = defineCollection({
4  type: 'content', // v2.0.0 and newer
5  schema: z.object({
6    title: z.string(),
7    tags: z.array(z.string()),
8    image: z.string().optional(),
9  }),
10});
11
12export const collections = {
13  'blog': blogCollection,
14};