Share collection schema with other modules
I have a collection whose schema I want to use in a server handler for validation.
I tried extracting the schema to a module but that broke nuxt type generation.
How can I get the schema defined in
content.config.ts?11 Replies
Hi @tpluscode. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Short answer: there isn’t a documented, supported way to import collection schemas directly from content.config.ts into other modules/server handlers. The docs show how to define schemas for collections and how those power types and Studio forms, but they don’t expose an API to read those schemas at runtime from other parts of your app. The knowledge sources don’t mention an official export or composable for “getting the schema” out of content.config.ts. I'm sorry, but there's no information about an API to import or access the resolved schemas from content.config.ts in other modules. [Defining Collections; Schema Validators]
What you can do today
- Duplicate or share schemas at the source level:
- Define your schema in a separate module/package that both content.config.ts and your server handler can import, then adapt it to the validator you use (Zod/Valibot) in content.config.ts. The docs explicitly support Zod v3/v4 and Valibot, so hosting the schema in a shared file and importing it into content.config.ts is compatible. [Schema Validators]
- Note: the docs don’t provide guidance for Nuxt typegen when moving schemas out; if moving broke type generation in your setup, the sources don’t provide a fix. I'm sorry, but there's no information about required project structure beyond placing the schema inside content.config.ts or importing from a local module. [Defining Collections; Schema Validators]
- Validate via server-side queries instead of importing schemas:
- If your server handler’s goal is to fetch and validate content items, consider using Nuxt Content’s server-side query utils. You can call queryCollection or queryCollectionNavigation in server handlers by passing event as the first arg. This leverages the collection’s types (generated from your schema) on the calling side, though it doesn’t give you the validator instance itself. [Server Usage (queryCollection); Server Usage (navigation)]
- Studio/editor-focused options:
- Features like property().editor(...) and inherit() help keep UI and content schema in sync, but they don’t expose a runtime schema getter for external modules. [Inherit Schema; Studio Form Customisation]
If you need to reuse the exact Zod schema in both content.config.ts and server code, the pragmatic approach (based on what’s documented) is:
- Define your Zod schema in a shared TS module in your repo (e.g., lib/content-schemas.ts).
- Import that schema into content.config.ts and pass it to defineCollection({ schema: ... }).
- Import the same schema in your server handler and use it to validate input/payloads.
Caveat: The docs don’t guarantee Nuxt’s type generation will include external modules used by content.config.ts. If you hit typegen issues, the sources don’t document a workaround. You may need to keep the shared file resolvable from the project root and ensure your Node/TS config includes it; however, there’s no official guidance in the provided docs. [Defining Collections; Schema Validators]
Summary
- No documented API to “get schema from content.config.ts” at runtime.
- Supported path: define schemas with Zod/Valibot and import them into content.config.ts and your server handler from a shared file.
- For fetching content server-side, use queryCollection(event, ...) utilities. [Server Usage (queryCollection)]
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@555413648382623754> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai actually, trying to use an imported zod object causes this error
similar when exporting only the shape and calling
z.object in the collection definitionLet me check the knowledge base for you again. One moment! :books:
----------------------
Thanks for the details — that error strongly suggests a Zod version or import mismatch between what you use in your shared module and what Nuxt Content expects when converting schemas to JSON Schema.
From the docs:
- Nuxt Content supports both Zod v3 and Zod v4, but you should import Zod (the z object) consistently from one place:
- For Zod v3: install zod and zod-to-json-schema, and import z from zod (or zod/v3). [Schema Validators]
- For Zod v4: install zod and import z from zod/v4 (no zod-to-json-schema needed). [Schema Validators]
The docs also note that the z re-export from @nuxt/content is deprecated. Mixing z from @nuxt/content in content.config.ts with z from zod (or mixing v3 and v4) in your shared module can cause type/instance mismatches like the one you’re seeing. [Schema Validators]
What to try
- Pick one Zod major version across your project and stick to its import everywhere:
- If using Zod v3:
- Install: zod and zod-to-json-schema.
- In both your shared schema file and content.config.ts: import { z } from 'zod' (or 'zod/v3').
- If using Zod v4:
- Install: zod.
- In both places: import { z } from 'zod/v4'.
- Ensure your content.config.ts and the shared module both use the same z import and version.
- Avoid importing z from @nuxt/content; the docs say that re-export is deprecated. [Schema Validators]
Example patterns from the docs
Zod v3 setup (shared schema + content.config.ts):
Zod v4 setup:
If you still hit issues
- Verify you don’t have both zod v3 and v4 installed or resolved in different parts of your app.
- Make sure no file imports z from @nuxt/content.
- The docs don’t provide more troubleshooting steps for cross-file schemas beyond consistent imports/versions, so if the mismatch persists, the knowledge sources don’t include an additional official workaround. [Schema Validators]
Why this happens
Nuxt Content converts your schema to JSON Schema internally; mixing different Zod builds/instances can make the object fail internal expectations (methods like _parse, _cached, etc.), leading to the error you saw. Aligning on one Zod version and import path resolves that. [Schema Validators]
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@555413648382623754> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
ah, but
@nuxt/content depends on zod ^3.7.1 @kapa.aiLet me check the knowledge base for you again. One moment! :books:
----------------------
You’re right that Nuxt Content has support for Zod v3, but it also explicitly supports Zod v4 now—and recommends importing Zod directly from your chosen version rather than using the deprecated re-export. The key is: pick one Zod major version and import it consistently everywhere (both in your shared schema file and in content.config.ts). [Schema Validators]
What the docs say and what to do:
- Zod v3 path:
- Install zod and zod-to-json-schema.
- Import z from 'zod' (or 'zod/v3') in both places. [Schema Validators]
- Zod v4 path:
- Install zod only.
- Import z from 'zod/v4' in both places. [Schema Validators]
Avoid mixing:
- Don’t import z from @nuxt/content (deprecated) in some files and from zod in others.
- Don’t mix v3 in one module and v4 in another. That mismatch is what triggers the “missing properties _cached, _parse…” error you saw. [Schema Validators]
If you align on a single Zod version and import path across your project, you can safely define schemas in a shared module and reuse them in content.config.ts and server handlers.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@555413648382623754> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai importing
z from zod was the key. thanksLet me check the knowledge base for you again. One moment! :books:
----------------------
Great to hear it’s working now!
Yes—Nuxt Content recommends importing Zod directly from your chosen version (and not from @nuxt/content). Use a single Zod major version consistently everywhere (shared schema files and content.config.ts):
- Zod v3: import from 'zod' (or 'zod/v3') and install zod-to-json-schema. [Schema Validators]
- Zod v4: import from 'zod/v4' (no zod-to-json-schema needed). [Schema Validators]
The re-exported z from @nuxt/content is deprecated, which is why importing directly from zod avoids the mismatch you hit. [Schema Validators]
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@555413648382623754> Kapa.ai is still learning and improving, please let me know how I did by reacting below.