Skip to content

Commit c6fb2fa

Browse files
committed
docs: add validators and property documentation
1 parent 16e649c commit c6fb2fa

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: Inherit Schema from a Vue Component
3+
navigation:
4+
title: Inherit from component
5+
description: Reuse a Vue component's props as part of your collection schema using property().inherit().
6+
---
7+
8+
You can reuse a Vue component's props as part of your collection schema. This helps keep your content model aligned with your UI, reduces duplication, and prevents drift.
9+
10+
## How it works
11+
12+
Nuxt Content provides a `property()` helper that augments your validator and adds the following utility:
13+
14+
- **inherit(path)**: replace the current object schema with the props JSON Schema inferred from a Vue component at `path`
15+
16+
Under the hood, Nuxt Content reads the component's props (via `nuxt-component-meta`) and converts them to JSON Schema, then merges them into your collection schema.
17+
18+
## Example
19+
20+
```ts [content.config.ts]
21+
import { defineContentConfig, defineCollection, z, property } from '@nuxt/content'
22+
23+
export default defineContentConfig({
24+
collections: {
25+
pages: defineCollection({
26+
type: 'page',
27+
source: '**/*.md',
28+
schema: z.object({
29+
// Reuse props from a local component
30+
hero: property(z.object({})).inherit('app/components/HeroSection.vue'),
31+
32+
// Reuse props from a dependency (path is resolved like an import)
33+
button: property(z.object({})).inherit('@nuxt/ui/components/Button.vue')
34+
})
35+
})
36+
}
37+
})
38+
```
39+
40+
## Notes
41+
42+
- The argument to `inherit()` is resolved like a module path. You can pass a relative path from project root or a package path.
43+
- `inherit()` expects to be used on an object field (e.g., `property(z.object({}))`).
44+
- Nested usage is supported: you can place inherited objects inside other objects and arrays; Nuxt Content recursively replaces `$content.inherit` markers.
45+
- If the component cannot be resolved, the schema falls back to the original object definition.
46+
47+
::tip
48+
Pair `inherit()` with `editor(...)` for better Studio forms if you need custom inputs on top of the component's props.
49+
::
50+
51+

0 commit comments

Comments
 (0)