#Content collections config
7 messages · Page 1 of 1 (latest)
Hi @night wasp. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
You can use the glob() loader pointing at your src/content/tours directory, since you have one JSON file per entry. For example:
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const tours = defineCollection({
loader: glob({
pattern: '**/*.json', // one file per entry
base: './src/content/tours', // where your files live
}),
schema: z.object({
// your fields here, e.g.:
// id: z.string(),
// title: z.string(),
// ...
}),
});
export const collections = { tours };
This follows the same pattern as the docs example that loads Markdown files from a base directory using glob(), just adjusted to JSON and your tours path. [Content collections]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@night wasp Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@hazy temple If i use z.date() on my schema, how I should set the date on my .json file?
Let me check the knowledge base for you again. One moment! :books:
The knowledge sources only show how z.date() works with YAML frontmatter, not JSON files, so there’s no explicit guidance for JSON. [Content schema]
From the docs example:
In YAML, dates written without quotes around them are interpreted as Date objects
publishDate: z.date(), // e.g. 2024-09-17[Content schema]
For JSON, the docs don’t say whether z.date() will accept a string like "2024-09-17" or require a different format, so I can’t state the exact expected JSON representation with confidence.
If you want to be sure it works with a string date in JSON, the docs do show using a string plus a transform:
updatedDate: z.string().transform((str) => new Date(str)),
which would clearly accept a JSON value like:
{
"updatedDate": "2024-09-17"
}
So:
- There is no explicit example of
z.date()with JSON in the docs. - Using
z.string().transform(...)is the documented way to safely accept a string date from JSON.