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.

Starting with this version, using a function as a schema is deprecated. The function-based schema will continue to work for now, but it will be removed in a future version. 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, deprecated 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.

Suppressing Deprecation Warning

If you are not ready to migrate yet, you can suppress the deprecation warning by setting the suppressDeprecationWarnings option in your configuration:

import { suppressDeprecatedWarnings } from "@content-collections/core";
 
suppressDeprecatedWarnings("legacySchema");

On this page