Skip to content

Commit

Permalink
Fix: support strict() on content collection schemas when slug is …
Browse files Browse the repository at this point in the history
…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 <[email protected]>

---------

Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
bholmesdev and delucis authored Feb 7, 2024
1 parent 6eab46b commit ce42833
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-pets-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Adds support for `.strict()` on content collection schemas when a custom `slug` is present.
2 changes: 1 addition & 1 deletion packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit ce42833

Please sign in to comment.