withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.93k stars 2.49k forks source link

Astro check does not validate content layer definitions #12417

Closed marceloverdijk closed 1 week ago

marceloverdijk commented 1 week ago

Astro Info

Astro                    v5.0.0-beta.7
Node                     v20.12.1
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/mdx
                         @astrojs/sitemap

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I'm having blog content defined as (note the categories array with the allowed enum values):

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

const blog = defineCollection({
  type: 'content',
  // Type-check frontmatter using a schema
  schema: z.object({
    title: z.string(),
    description: z.string(),
    // Transform string to Date object
    pubDate: z.coerce.date(),
    updatedDate: z.coerce.date().optional(),
    heroImage: z.string().optional(),
    categories: z.array(z.enum([
      'Category',
      'Another category',
    ])).optional(),
  }),
});

export const collections = { blog };

When I have a post in mdx like all works as expected:

---
title: 'Post 1'
description: 'Lorem ipsum dolor sit amet'
pubDate: 'Nov 01 2024'
categories: ['Category', 'Another category']
---

Lorem ipsum dolor sit amet.

But when I make a typo in the category, e.g. categories: ['InvalidCategory', 'Another category'], the astro check does not come back with an error:

npm run astro check

> my-website@0.1.0 astro
> astro check

07:28:51 [WARN] [config] The adapter @astrojs/cloudflare provides experimental support for "astro:env getSecret". You may experience issues or breaking changes until this feature is fully supported by the adapter.
07:28:51 [WARN] [config] The adapter @astrojs/cloudflare has limited support for "sharp". Certain features may not work as expected.
07:28:51 [vite] Re-optimizing dependencies because vite config has changed
07:28:51 [content] Syncing content

When I run astro build then it gives the expected error:

npm run astro build

> my-website@0.1.0 astro
> astro build

07:28:11 [WARN] [config] The adapter @astrojs/cloudflare provides experimental support for "astro:env getSecret". You may experience issues or breaking changes until this feature is fully supported by the adapter.
07:28:11 [WARN] [config] The adapter @astrojs/cloudflare has limited support for "sharp". Certain features may not work as expected.
07:28:11 [content] Syncing content
[InvalidContentEntryDataError] blog → 2024-11-01-post-1 data does not match collection schema.
categories.0: Invalid enum value. Expected 'Category' | 'Another category', received 'InvalidCategory'
  Hint:
    See https://docs.astro.build/en/guides/content-collections/ for more information on content schemas.
  Error reference:
    https://docs.astro.build/en/reference/errors/invalid-content-entry-data-error/
  Location:
    /Users/marceloverdijk/workspace/my-website/src/content/blog/2024-11-01-post-1.mdx:0:0
  Stack trace:
    at getEntryDataAndImages (file:///Users/marceloverdijk/workspace/my-website/node_modules/astro/dist/content/utils.js:158:26)
    at async syncData (file:///Users/marceloverdijk/workspace/my-website/node_modules/astro/dist/content/loaders/glob.js:89:28)

What's the expected result?

An error when running astro check.

Link to Minimal Reproducible Example

.

Participation

Princesseuh commented 1 week ago

astro check is a type checker, similar to tsc. At this point, the behavior you describe is expected.

This may change in the future as we experiment more with content intellisense (currently experimental), but for now, this is intended.

marceloverdijk commented 1 week ago

Thanks @Princesseuh for the clarification. Is there another ticket to track this, or should I perhaps create a feature request?

Princesseuh commented 1 week ago

You can post a feature request here: https://github.com/withastro/roadmap/discussions