Use zod to define the schema of a single json file, similar to defining a collection schema, but for 1 json file #806
Replies: 3 comments 1 reply
-
Yes, I think singletons support would be great |
Beta Was this translation helpful? Give feedback.
-
@florian-lefebvre, as a contributor who seems interested in this feature, would you mind helping me with my pull request to implement it? On this line I need to convert the relative path into an absolute path so that it can be detected when a JSON file that has a validation schema is imported on this line. Any ideas? |
Beta Was this translation helpful? Give feedback.
-
Nate has already commented on this idea at withastro/astro#9850 (review), but to leave my thoughts here, I think this should be left to content collections as well. Or at least via a third-party integration. You can create a Vite plugin that handles |
Beta Was this translation helpful? Give feedback.
-
Body
Summary
Be able to use zod to define the schema of a single json file at a time within
astro.config.mjs
, similar to https://docs.astro.build/en/guides/content-collections/#defining-a-collection-schema but for 1 json file.Background & Motivation
For some websites (such as simple portfolios), it is useful to be able to define all of the content within one file, and then the whole website can be updated by simply editing the file and waiting for some automated tool to build it. To achieve this, a
src/content.json
file could be used and used like so:But this approach would not allow the use of astro's image component because to do that you have to have a image object that has been imported. To fix this you could place the file in a content collection, but that's pretty hacky since a content collection is designed for multiple files. One proposal (#785) would allow a content collection to be represented as a single file, but that would not allow a propeties like "websiteName" or "websiteDescription" to be specified in the root of the collection as far as I am aware.
Goals
All of the following should only apply on a per-file basis:
Example
Note: withastro/astro/pull/9850/ (the PR to implement this API) has lots of discussion about different API ideas.
Defining the type checking for the file:
astro.config.mjs
Defining the file:
src/content.json
Using the file:
src/pages/index.astro
Beta Was this translation helpful? Give feedback.
All reactions