logoContent Collections

Deprecated Schema as Function

With version 0.9.0, Content Collections now uses Standard Schema for the schema property. This change allows you to utilize any Standard Schema compatible library, such as Zod or Valibot, to define the schema.

Using a function as a schema was deprecated with version 0.9.0 and is removed within version 0.12.0. To adopt the new schema, you must install your chosen library and update the schema definition in your configuration.

Let's examine the following example:

The old, removed way of defining the schema:

import { defineCollection, defineConfig } from "@content-collections/core";
 
const posts = defineCollection({
  name: "posts",
  directory: "content",
  include: "*.md",
  schema: (z) => ({
    title: z.string(),
    date: z.string(),
  })
});
 
export default defineConfig({
  collections: [posts],
});

If we want to continue using Zod to define the schema, we first need to install the library:

npm i zod

Then, import Zod and use it directly to define the schema:

import { defineCollection, defineConfig } from "@content-collections/core";
import { z } from "zod";
 
const posts = defineCollection({
  name: "posts",
  directory: "content",
  include: "*.md",
  schema: z.object({
    title: z.string(),
    date: z.string(),
  })
});
 
export default defineConfig({
  collections: [posts],
});

And that's it! You have successfully migrated to the new schema format.