From ce4283331f18c6178654dd705e3cf02efeef004a Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Wed, 7 Feb 2024 07:26:16 -0500 Subject: [PATCH] Fix: support `strict()` on content collection schemas when `slug` is present (#10003) * fix: use correct data object when parsing schema * feat(test): add `strict()` to slug schema test * chore: changeset * docs: "Adds support for" Co-authored-by: Chris Swithinbank --------- Co-authored-by: Chris Swithinbank --- .changeset/strong-pets-rhyme.md | 5 +++++ packages/astro/src/content/utils.ts | 2 +- .../test/fixtures/content-collections/src/content/config.ts | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 .changeset/strong-pets-rhyme.md diff --git a/.changeset/strong-pets-rhyme.md b/.changeset/strong-pets-rhyme.md new file mode 100644 index 0000000000..eea88944f0 --- /dev/null +++ b/.changeset/strong-pets-rhyme.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Adds support for `.strict()` on content collection schemas when a custom `slug` is present. diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index 804925f0fc..94421cec41 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -126,7 +126,7 @@ export async function getEntryData( // Use `safeParseAsync` to allow async transforms let formattedError; - const parsed = await (schema as z.ZodSchema).safeParseAsync(entry.unvalidatedData, { + const parsed = await (schema as z.ZodSchema).safeParseAsync(data, { errorMap(error, ctx) { if (error.code === 'custom' && error.params?.isHoistedAstroError) { formattedError = error.params?.astroError; diff --git a/packages/astro/test/fixtures/content-collections/src/content/config.ts b/packages/astro/test/fixtures/content-collections/src/content/config.ts index fbd4e381da..3d7c3977a4 100644 --- a/packages/astro/test/fixtures/content-collections/src/content/config.ts +++ b/packages/astro/test/fixtures/content-collections/src/content/config.ts @@ -1,7 +1,8 @@ import { z, defineCollection } from 'astro:content'; const withCustomSlugs = defineCollection({ - schema: z.object({}), + // Ensure schema passes even when `slug` is present + schema: z.object({}).strict(), }); const withSchemaConfig = defineCollection({