|
| 1 | +--- |
| 2 | +title: Schema Validators |
| 3 | +navigation: |
| 4 | + title: Validators |
| 5 | +description: Define collection schemas with your preferred validator and full type-safety. |
| 6 | +--- |
| 7 | + |
| 8 | +Nuxt Content supports defining collection schemas with multiple validators. Out of the box, this includes popular libraries like **Zod v3 / v4** and **Valibot** (examples below). The system is extensible and can support other validators via JSON Schema adapters. Schemas enforce data consistency and drive generated types and Studio forms. |
| 9 | + |
| 10 | +## Using Zod v3 |
| 11 | + |
| 12 | + |
| 13 | +### Install |
| 14 | + |
| 15 | +```bash |
| 16 | +pnpm add -D zod zod-to-json-schema |
| 17 | +# or |
| 18 | +npm i -D zod zod-to-json-schema |
| 19 | +``` |
| 20 | + |
| 21 | +Prefer importing `z` directly from `zod`. |
| 22 | + |
| 23 | +```ts [content.config.ts] |
| 24 | +import { defineContentConfig, defineCollection, property } from '@nuxt/content' |
| 25 | +import { z } from 'zod' // or 'zod/v3' if your setup exposes this subpath |
| 26 | +
|
| 27 | +export default defineContentConfig({ |
| 28 | + collections: { |
| 29 | + blog: defineCollection({ |
| 30 | + type: 'page', |
| 31 | + source: 'blog/*.md', |
| 32 | + schema: z.object({ |
| 33 | + title: z.string(), |
| 34 | + description: z.string().optional(), |
| 35 | + date: z.date(), |
| 36 | + draft: z.boolean().default(false), |
| 37 | + tags: z.array(z.string()).optional(), |
| 38 | + image: z.object({ |
| 39 | + src: property(z.string()).editor({ input: 'media' }), |
| 40 | + alt: z.string() |
| 41 | + }) |
| 42 | + }) |
| 43 | + }) |
| 44 | + } |
| 45 | +}) |
| 46 | +``` |
| 47 | + |
| 48 | +::note |
| 49 | +Dates are serialised as ISO strings under the hood (JSON Schema `format: date-time`). |
| 50 | +:: |
| 51 | + |
| 52 | +::warning |
| 53 | +The `z` re-export from `@nuxt/content` is deprecated and will be removed in a future release. Import `z` from `zod` (or `zod/v3`) instead. |
| 54 | +:: |
| 55 | + |
| 56 | +## Using Zod v4 |
| 57 | + |
| 58 | +Zod v4 provides a native JSON Schema export. No `zod-to-json-schema` dependency is required. |
| 59 | + |
| 60 | +### Install |
| 61 | + |
| 62 | +```bash |
| 63 | +pnpm add -D zod |
| 64 | +# or |
| 65 | +npm i -D zod |
| 66 | +``` |
| 67 | + |
| 68 | +```ts [content.config.ts] |
| 69 | +import { defineContentConfig, defineCollection, property } from '@nuxt/content' |
| 70 | +import { z } from 'zod/v4' |
| 71 | + |
| 72 | +export default defineContentConfig({ |
| 73 | + collections: { |
| 74 | + docs: defineCollection({ |
| 75 | + type: 'page', |
| 76 | + source: 'docs/**/*.md', |
| 77 | + schema: z.object({ |
| 78 | + title: z.string(), |
| 79 | + description: z.string().optional(), |
| 80 | + updatedAt: z.date(), |
| 81 | + draft: z.boolean().optional(), |
| 82 | + tags: z.array(z.string()).optional(), |
| 83 | + hero: z.object({ |
| 84 | + image: property(z.string()).editor({ input: 'media' }), |
| 85 | + caption: z.string().optional() |
| 86 | + }) |
| 87 | + }) |
| 88 | + }) |
| 89 | + } |
| 90 | +}) |
| 91 | +``` |
| 92 | + |
| 93 | +## Using Valibot |
| 94 | + |
| 95 | +Use Valibot primitives to define your schema. |
| 96 | +### Install |
| 97 | + |
| 98 | +```bash |
| 99 | +pnpm add -D valibot @valibot/to-json-schema |
| 100 | +# or |
| 101 | +npm i -D valibot @valibot/to-json-schema |
| 102 | +``` |
| 103 | + |
| 104 | +```ts [content.config.ts] |
| 105 | +import { defineContentConfig, defineCollection, property } from '@nuxt/content' |
| 106 | +import { object, string, boolean, array, date, optional } from 'valibot' |
| 107 | + |
| 108 | +export default defineContentConfig({ |
| 109 | + collections: { |
| 110 | + docs: defineCollection({ |
| 111 | + type: 'page', |
| 112 | + source: 'docs/**/*.md', |
| 113 | + schema: object({ |
| 114 | + title: string(), |
| 115 | + description: optional(string()), |
| 116 | + updatedAt: date(), |
| 117 | + draft: optional(boolean()), |
| 118 | + tags: optional(array(string())), |
| 119 | + hero: object({ |
| 120 | + image: property(string()).editor({ input: 'media' }), |
| 121 | + caption: optional(string()) |
| 122 | + }) |
| 123 | + }) |
| 124 | + }) |
| 125 | + } |
| 126 | +}) |
| 127 | +``` |
| 128 | + |
| 129 | +## Choosing a validator |
| 130 | + |
| 131 | +- **Zod v3**: battle-tested, rich ecosystem; great DX with re-exported `z`. |
| 132 | +- **Valibot**: lightweight and fast; bring your own importer from `valibot`. |
| 133 | + |
| 134 | +Only install and use the validator you need. Nuxt Content auto-detects supported validators that are installed. |
| 135 | + |
| 136 | +## Support for other validators |
| 137 | + |
| 138 | +Nuxt Content converts your collection schema to JSON Schema Draft-07 internally. If your preferred validator can be transformed to Draft-07 (or has a compatible adapter), it can be supported. Currently, Zod (v3 and v4) and Valibot are auto-detected. If you’d like first-class support for another validator, consider opening an issue or PR in the [Nuxt Content repository](https://github.com/nuxt/content). |
| 139 | + |
| 140 | +## Editor metadata (optional) |
| 141 | + |
| 142 | +You can enrich fields for Studio via `property(...).editor({ ... })` with both validators. See the Studio docs for mapping details. |
| 143 | + |
| 144 | +::tip{to="/docs/studio/content"} |
| 145 | +Learn how editor metadata maps to form inputs in Studio. |
| 146 | +:: |
| 147 | + |
| 148 | + |
0 commit comments