#Running into a [InvalidContentEntryDataError] that I am struggling to understand

1 messages · Page 1 of 1 (latest)

pliant zodiac
#

I have an Astro project which I started some time ago and I am running into this weird problem. I have this config.ts file which describes how a project should look like:

import { defineCollection, z } from 'astro:content';

const projectsCollection = defineCollection({
  schema: z.object({
    title: z.string(),
    slug: z.string(),
    shortDescription: z.string(),
    imageUrl: z.string(),
    techStack: z.array(z.string()).optional(),
    githubUrl: z.string().url().optional(),
  }),
});

export const collections = {
'projects': projectsCollection,
};
And under projects/project1.md I have this:

---
title: Example Project
slug: example
shortDescription: An example.
imageUrl: ../assets/example.png
techStack: [Whatever, Whatever]
githubUrl: https://github.com/exampleperson/exampleproject
---

## About the Project

Detailed description

## Features

- Feature 1
- Feature 2
- Feature 3```

When running npm run dev, I run into this error:
[InvalidContentEntryDataError] projects → example data does not match collection schema.

slug: Required

However, if I change slug to be optional, I am then able to run the project and everything works as expected, I am able to navigate to projects/project1 and I see the expected content. I tried looking around for what might be causing this issue and I am unable to find the root cause for this behavior.
lean kindle
#

Hi, slug is a special property in Astro when using Content Collections. So you don't need to declare it in your schema.
By default, Content Collections will use your file path to generate the slug of your file. You can override this generated slug using the slug property in your frontmatter but you don't need to declare it in your schema. See: https://docs.astro.build/en/guides/content-collections/#defining-custom-ids

So, in your case, Astro is probably confused because it doesn't expect to see slug in your schema.

Docs

Manage your content with type safety.

pliant zodiac
#

Hey, thanks, this fixed the issue for me, I am using AI to help me learn Astro and I started yesterday, this resolves the issue nicely and it's kind of my bad for not reading docs tbh.

lean kindle
#

If your rely on AI, you might need to check the docs from time to time. Sometimes it hallucinates or mixes information from different framework (often React...) so you might need to be cautious and double check the docs if you get some weird behavior. 😉