Valibot Guide

Learn how to add schema validation to your file system using Valibot in renoun.

import { Directory, withSchema } from 'renoun/file-system'
import * as v from 'valibot'

const frontmatterSchema = {
  title: v.string(),
  date: v.date(),
  summary: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
}

const posts = new Directory({
  path: 'posts',
  include: '*.mdx',
  loaders: {
    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 valibot
pnpm add valibot
bun add valibot
yarn add valibot

Define Schema

Now, we’ll create a schema using valibot for the front matter of an MDX file:

import * as v from 'valibot'

const frontmatterSchema = {
  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/file-system'
import * as v from 'valibot'

const frontmatterSchema = {
  title: v.string(),
  date: v.date(),
  summary: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
}

const posts = new Directory({
  path: 'posts',
  include: '*.mdx',
  loaders: {
    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.

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.