Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/full-hounds-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes Zod meta not correctly being rendered on top-level schema when converted into JSON Schema
4 changes: 4 additions & 0 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,13 @@ async function generateJSONSchema(
}

if (zodSchemaForJson instanceof z.ZodObject) {
const existingMeta = z.globalRegistry.get(zodSchemaForJson);
zodSchemaForJson = zodSchemaForJson.extend({
$schema: z.string().optional(),
});
if (existingMeta) {
z.globalRegistry.add(zodSchemaForJson, existingMeta);
}
}

try {
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/data-collections-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@ describe('Content Collections - data collections', () => {
},
},
required: ['homepage'],
title: 'Translations',
description: 'Translation strings for the site',
}),
JSON.stringify(JSON.parse(rawJson)),
);
});

it('Preserves .meta() definitions in the generated JSON schema', async () => {
const schema = JSON.parse(
await fixture.readFile('../.astro/collections/i18n.schema.json'),
);
assert.equal(schema.title, 'Translations');
assert.equal(schema.description, 'Translation strings for the site');
});

it('Generates schema file when the schema uses the image function', async () => {
const schemaExists = await fixture.pathExists('../.astro/collections/image.schema.json');
assert.equal(schemaExists, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const i18n = defineCollection({
greeting: z.string(),
preamble: z.string(),
})
}),
}).meta({ title: 'Translations', description: 'Translation strings for the site' }),
});

const func = defineCollection({
Expand Down
Loading