Fumadocs

Validation

Validate your files via Zod

Usage

The file validation is based on Zod. When an invalid file is detected, it will throw an error immediately.

To customise the schemas, you have to install zod.

npm install zod

Now you can override or extend the default zod schemas with the schema option.

Example

Extend the default frontmatter schema:

app/source.ts
import { createMDXSource, defaultSchemas } from 'fumadocs-mdx';
import { z } from 'zod';
import { loader } from 'fumadocs-core/source';
import { map } from '@/.map';
 
const frontmatterSchema = defaultSchemas.frontmatter.extend({
  preview: z.string().optional(),
});
 
export const utils = loader({
  baseUrl: '/docs',
  rootDir: 'docs',
  source: createMDXSource(map, { schema: { frontmatter: frontmatterSchema } }),
});

Typescript

Thanks to the Typescript magic, your custom Zod schema type should be automatically inferred.

source.ts
utils.getPages().data; // should be your own frontmatter

Infer the type of pages and meta files with build-in type helpers of Source API.

import type { InferMetaType, InferPageType } from 'fumadocs-core/source';
 
export type Page = InferPageType<typeof utils>;
export type Meta = InferMetaType<typeof utils>;

Last updated on

On this page

Edit on Github