content-collections icon indicating copy to clipboard operation
content-collections copied to clipboard

feat(core): #528 use StandardSchema for validation

Open sdorra opened this issue 7 months ago • 3 comments

Use every StandardSchema compliant library for validation.

To use StandardSchema for validation just use the library of your choice:

zod:

import { defineCollection, defineConfig } from "@content-collections/core";
import { z } from "zod";

const posts = defineCollection({
  name: "posts",
  typeName: "Post",
  schema: z.object({
    title: z.string().min(5),
    description: z.string().min(10),
    date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/),
  }),
  directory: "sources/posts",
  include: "**/*.md(x)?",
});

export default defineConfig({
  collections: [posts],
});

valibot:

import { defineCollection, defineConfig } from "@content-collections/core";
import * as v from "valibot";

const posts = defineCollection({
  name: "posts",
  typeName: "Post",
  schema: v.object({
    title: v.pipe(v.string(), v.minLength(5)),
    description: v.pipe(v.string(), v.minLength(10)),
    date: v.pipe(v.string(), v.regex(/^\d{4}-\d{2}-\d{2}$/)),
  }),
  directory: "sources/posts",
  include: "**/*.md(x)?",
});

export default defineConfig({
  collections: [posts],
});

The old syntax will still work, but it will log a deprecation message. Unfortunately, it seems impossible to mark the usage of the old syntax as deprecated. Marking the part of the union type as deprecated does not affect the usage itself.

In future versions, possibly 1.0.0, we will remove the old syntax and the dependency on Zod.

Todos

  • [x] Log deprecation message
  • [x] Update documentation
  • [x] Add a sample with Valibot
  • [x] Add a sample with ArkType
  • [x] Changelog entry
  • [x] Use new syntax on the website
  • [x] Use new syntax for samples
  • [x] API to suppress deprecation warning
  • [x] Update landing page (section: Powerful Validation)
  • [x] Update installer
  • [x] Talk to @fuma-nama about fumadocs integration

Closes: #528

sdorra avatar May 13 '25 18:05 sdorra

🦋 Changeset detected

Latest commit: 0495fe40b3aa577fb531b502ed91af9a257a2478

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@content-collections/core Minor
content-collections Minor
@content-collections/installer Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar May 13 '25 18:05 changeset-bot[bot]

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
content-collections ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 22, 2025 6:29am

vercel[bot] avatar May 13 '25 18:05 vercel[bot]

Open in StackBlitz

@content-collections/cli

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/cli@570
content-collections

npm i https://pkg.pr.new/sdorra/content-collections@570
@content-collections/installer

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/installer@570
@content-collections/core

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/core@570
@content-collections/integrations

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/integrations@570
@content-collections/markdown

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/markdown@570
@content-collections/mdx

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/mdx@570
@content-collections/next

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/next@570
@content-collections/remix-vite

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/remix-vite@570
@content-collections/vinxi

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/vinxi@570
@content-collections/vite

npm i https://pkg.pr.new/sdorra/content-collections/@content-collections/vite@570

commit: 0495fe4

pkg-pr-new[bot] avatar May 13 '25 18:05 pkg-pr-new[bot]

Thank you for supporting Standard Schema! 🔥

fabian-hiller avatar May 30 '25 00:05 fabian-hiller

This is awesome, was pleasantly surprise I don't have to install zod just for this!

ElasticBottle avatar Aug 07 '25 06:08 ElasticBottle