Astro2.0のfrontmatterとz.optional()

作成:
heroimage

Markdown ファイルについて

最初エラーがでるので Markdown の frontmatter 要素はすべて書く必要があるのかと勘違いしたが
Markdown で使わない frontmatter 要素は src/content/config.tsz.objectoptional 関数を指定することで使わなくても問題なくなった

ここで知った

実は公式ブログでも書いてた

---
// `astro:content`から z defineCollection を読み込む
import { z, defineCollection } from "astro:content";

// コレクションの定義
const blogCollection = defineCollection({
  schema: z.object({
    layout: z.string(),
    title: z.string(),
    date: z.string().transform((str) => new Date(str)),
    description: z.string().optional(),
    tags: z.array(z.string()).optional(),
    featured: z.string().optional(),
    heroImage: z.string().optional(),
    author: z.string().optional(),
    isDraft: z.boolean().optional(),
    updatedDate: z
      .string()
      .transform((str) => new Date(str))
      .optional(),
  }),
});
// 以下でコレクションを割り当てる コレクションを読み込む場合は
// getcollection(以下の文字列) で可能
export const collections = {
  blog: blogCollection,
  note: blogCollection,
};