Valibot Guide
Learn how to add schema validation to your file system using Valibot in renoun.
import { Directory, withSchema } from 'renoun'
import * as v from 'valibot'
const frontmatterSchema = v.object({
  title: v.string(),
  date: v.date(),
  summary: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
})
const posts = new Directory({
  path: 'posts',
  filter: '*.mdx',
  loader: {
    mdx: withSchema(
      { frontmatter: frontmatterSchema },
      (path) => import(`./posts/${path}.mdx`)
    ),
  },
})
Introduction
In this guide, we’ll walk through how to use Valibot to add schema validation to your file system. Using Valibot ensures that your targeted files conform to an expected structure, providing type safety and validation.
Before You Begin
Before you start, make sure you have a basic understanding of how the File System API works in renoun. We’ll also be using MDX files in this guide, so make sure you’re familiar with the MDX Guide as well.
Using Valibot
Valibot is the open-source schema library for TypeScript, designed with bundle size, type safety, and developer experience in mind. Let’s look at how you can use Valibot to add schema validation to your file system in renoun.
Install
First, install valibot using your package manager:
npm install valibotpnpm add valibotyarn add valibotbun add valibotDefine Schema
Now, we’ll create a schema using valibot for the front matter of an MDX file:
import * as v from 'valibot'
const frontmatterSchema = v.object({
  title: v.string(),
  date: v.date(),
  summary: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
})
Apply to a Directory
We can now apply the Valibot frontmatterSchema to your collection using the schema option in the collection utility:
import { Directory, withSchema } from 'renoun'
import * as v from 'valibot'
const frontmatterSchema = v.object({
  title: v.string(),
  date: v.date(),
  summary: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
})
const posts = new Directory({
  path: 'posts',
  filter: '*.mdx',
  loader: {
    mdx: withSchema(
      { frontmatter: frontmatterSchema },
      (path) => import(`./posts/${path}.mdx`)
    ),
  },
})
Now, the frontmatter export in your MDX files will be validated against the frontmatterSchema we defined using Valibot. If the data does not match the schema, an error will be thrown.
Beyond Front Matter
While the example in this guide focused on validating front matter in MDX files, the same approach can be applied to validate any kind of export within a file. Whether you need to enforce a specific structure for other metadata, content fields, or custom data exports, Valibot provides the flexibility to define schemas that fit your file system requirements.
Validating MDX headings
When using the MDX remark plugin that auto-exports headings, TypeScript does not know the shape of that export unless you include it in your schema.
In this scenario, we manually type the exported headings in our schema so validation and type inference work as expected:
import { Directory, withSchema } from 'renoun'
import { isValidElement } from 'react'
import * as v from 'valibot'
const mdxSchema = v.object({
  headings: v.array(
    v.object({
      id: v.string(),
      level: v.number(),
      children: v.custom<NonNullable<React.ReactNode>>(isValidElement),
      text: v.string(),
    })
  ),
})
const docs = new Directory({
  path: 'docs',
  filter: '*.mdx',
  loader: {
    mdx: withSchema(mdxSchema, (path) => import(`@/docs/${path}.mdx`)),
  },
})Conclusion
By using Valibot, you can add reliable schema validation to your collections in renoun. This ensures that your data is always in the expected format, making your application more robust and maintainable.
For more information, refer to the Valibot documentation.