#Content collections with object arrays

9 messages · Page 1 of 1 (latest)

sinful carbon
#

Hello. I'm trying to use Content Collections API with JSON files that initially contained an array of objects.
Since Astro had trouble reading that structure, I created a Zod schema where the root object has a property that is an array of that same schema.

However, even after this adjustment, Astro still throws the following error:

[InvalidContentEntryDataError] talks → dataday data does not match collection schema.

  title**: **title: Required
  startDate**: **startDate: Required
  endDate**: **endDate: Required
  type**: **type: Required
  track**: **track: Required

  Hint:

It seems Astro is still trying to validate each element inside the array as if it were the root entry instead of using the outer object structure.

content.config.ts

const talkSchema = z.object({
    title: z.string(),
    description: z.string().optional(),
    startDate: z.string(),
    endDate: z.string(),
    speaker: z.string().optional(),
    sponsors: z.array(z.string()).optional(),
    type: z.enum(["talk", "break", "sponsor"]),
    track: z.array(z.string()),
});

const talks = defineCollection({
    loader: glob({ pattern: "**/*.json", base: "src/content/talks" }),
    schema: z.object({
        talks: z.array(talkSchema),
    }),
});

export type Talk = z.infer<typeof talkSchema>;
export const collections = {
    talks,
};```

My JSON example:

{
"talks": [
{
"title": "Registro y Recepción",
"startDate": "2025-11-08 08:30:00",
"endDate": "2025-11-08 09:00:00",
"sponsors": [],
"type": "break",
"track": ["track1"]
}
]
}

cold marsh
#

Hi! Reusing your code snippets, I tried to set up a project to reproduce the issue but... I can't: it works fine on my side. You can check https://stackblitz.com/edit/astro-glob-loader-json?file=src%2Fcontent.config.ts,src%2Fcontent%2Ftalks%2Fdataday.json,src%2Fpages%2Findex.astro&on=stackblitz

No errors, and you can see the data in the console, getCollection() gives:

[
  {
    id: 'dataday',
    data: {
      talks: [
        {
          title: 'Registro y Recepción',
          startDate: '2025-11-08 08:30:00',
          endDate: '2025-11-08 09:00:00',
          sponsors: [],
          type: 'break',
          track: [ 'track1' ]
        }
      ]
    },
    filePath: 'src/content/talks/dataday.json',
    digest: '29e5cc5fbb15b899',
    collection: 'talks'
  }
]

So, the issue must be elsewhere... 🤔 Can you tell us your Astro and Node versions?

sinful carbon
cold marsh
#

Hmm, yes this is weird... I wanted to be sure you don't use an odd version of Node.
From the paths I see you're using Windows. I guess D:/ is a secondary drive. Can you try on your main drive?
Just trying to narrow down where the issue could come from... We had seen weird issues on Windows because of the paths so I wonder if this could be related somehow.

sinful carbon
cold marsh
#

Can you try outside of OneDrive? This is know to cause issues with Git repository... If it's still doesn't work, then I'll see if I can find a Windows user to reproduce this because I'm out of ideas at the moment.

sinful carbon
#

Hello Armand. I clone my project again and noticed that a second content.config.ts file existed in the src folder (I'm not exactly sure why, I suposse it was due to cache files when I pulled the branch). Therefore, I had been importing the incorrect content.config.ts file}

#

The fault was entirely mine. Thanks a lot for your help 🤙🏾

cold marsh
#

No problem! Glad you figured out the issue! 🎉