diff options
author | 2024-02-07 07:26:16 -0500 | |
---|---|---|
committer | 2024-02-07 07:26:16 -0500 | |
commit | ce4283331f18c6178654dd705e3cf02efeef004a (patch) | |
tree | 0321f75c36d63031e27f6bd55a9d2e5e51b3e320 | |
parent | 6eab46b84b683bbcf2d9463d69ad3f2121c495c3 (diff) | |
download | astro-ce4283331f18c6178654dd705e3cf02efeef004a.tar.gz astro-ce4283331f18c6178654dd705e3cf02efeef004a.tar.zst astro-ce4283331f18c6178654dd705e3cf02efeef004a.zip |
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 <swithinbank@gmail.com>
---------
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
-rw-r--r-- | .changeset/strong-pets-rhyme.md | 5 | ||||
-rw-r--r-- | packages/astro/src/content/utils.ts | 2 | ||||
-rw-r--r-- | packages/astro/test/fixtures/content-collections/src/content/config.ts | 3 |
3 files changed, 8 insertions, 2 deletions
diff --git a/.changeset/strong-pets-rhyme.md b/.changeset/strong-pets-rhyme.md new file mode 100644 index 000000000..eea88944f --- /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 804925f0f..94421cec4 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 fbd4e381d..3d7c3977a 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({ |