Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: experimental content collection JSON schemas #10145

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions .changeset/slow-items-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
"astro": minor
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
---

Adds experimental JSON Schema support for content collections of with `type: 'data'`.

To enable this feature, add the experimental flag:
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved

```diff
import { defineConfig } from 'astro/config';

export default defineConfig({
experimental: {
+ contentCollectionJsonSchema: true
}
});
```

To make sure VSCode picks up the schema file, you have multiple option. You can either reference the file in every file of your content collection:
```diff
{
+ "$schema": "../../../.astro/schemas/collections/test.json",
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
"test": "test"
}
```

Or you can set your VSCode settings to match them, read more in the [VSCode docs](https://code.visualstudio.com/docs/languages/json#_json-schemas-and-settings):

```diff
"json.schemas": [
{
"fileMatch": [
"/src/content/test/**"
],
"url": "../../../.astro/schemas/collections/test.json"
}
]
```
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@
"vitefu": "^0.2.5",
"which-pm": "^2.1.1",
"yargs-parser": "^21.1.1",
"zod": "^3.22.4"
"zod": "^3.22.4",
"zod-to-json-schema": "^3.22.4"
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
},
"optionalDependencies": {
"sharp": "^0.32.6"
Expand Down
19 changes: 19 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,25 @@ export interface AstroUserConfig {
*/
contentCollectionCache?: boolean;

/**
* @docs
* @name experimental.contentCollectionJsonSchema
* @type {boolean}
* @default `false`
* @version 4.5.0
* @description
* Enables generation of JSON Schema files for content collections of type `data`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Once the changeset is figured out, those instructions, limitations etc should also be here! This is the place in docs where people will go to figure out what this does and how it works!

When we like the changeset, it can be a mostly copy-paste here, except that this can be less "here's a new feature!" and more just boring, "here's what you do."

Reminder to self: this should also link to the content collections page in docs.

*
* ```js
* {
* experimental: {
* contentCollectionJsonSchema: true,
* },
* }
* ```
*/
contentCollectionJsonSchema?: boolean;

/**
* @docs
* @name experimental.clientPrerender
Expand Down
46 changes: 46 additions & 0 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type fsMod from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { normalizePath, type ViteDevServer } from 'vite';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import type { AstroSettings, ContentEntryType } from '../@types/astro.js';
import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';
Expand Down Expand Up @@ -310,6 +312,8 @@ export async function createContentTypesGenerator({
contentConfig: observable.status === 'loaded' ? observable.config : undefined,
contentEntryTypes: settings.contentEntryTypes,
viteServer,
logger,
settings,
});
invalidateVirtualMod(viteServer);
}
Expand Down Expand Up @@ -352,6 +356,8 @@ async function writeContentFiles({
contentEntryTypes,
contentConfig,
viteServer,
logger,
settings,
}: {
fs: typeof fsMod;
contentPaths: ContentPaths;
Expand All @@ -360,9 +366,19 @@ async function writeContentFiles({
contentEntryTypes: Pick<ContentEntryType, 'contentModuleTypes'>[];
contentConfig?: ContentConfig;
viteServer: Pick<ViteDevServer, 'hot'>;
logger: Logger;
settings: AstroSettings;
}) {
let contentTypesStr = '';
let dataTypesStr = '';

const collectionSchemasDir = new URL('./schemas/collections/', contentPaths.cacheDir);
if (settings.config.experimental.contentCollectionJsonSchema) {
if (!fs.existsSync(collectionSchemasDir)) {
fs.mkdirSync(collectionSchemasDir, { recursive: true });
}
}

for (const [collection, config] of Object.entries(contentConfig?.collections ?? {})) {
collectionEntryMap[JSON.stringify(collection)] ??= { type: config.type, entries: {} };
}
Expand Down Expand Up @@ -419,6 +435,36 @@ async function writeContentFiles({
for (const entryKey of Object.keys(collection.entries).sort()) {
const dataType = collectionConfig?.schema ? `InferEntrySchema<${collectionKey}>` : 'any';
dataTypesStr += `${entryKey}: {\n id: ${entryKey};\n collection: ${collectionKey};\n data: ${dataType}\n};\n`;
if (
settings.config.experimental.contentCollectionJsonSchema &&
collectionConfig?.schema
) {
let zodSchemaForJson = collectionConfig.schema;
if (zodSchemaForJson instanceof z.ZodObject) {
zodSchemaForJson = zodSchemaForJson.extend({
$schema: z.string().optional(),
});
}
Comment on lines +458 to +463
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this seems like a reasonable way to handle it!

try {
await fs.promises.writeFile(
new URL(`./${collectionKey.replace(/"/g, '')}.json`, collectionSchemasDir),
JSON.stringify(
zodToJsonSchema(zodSchemaForJson, {
name: collectionKey.replace(/"/g, ''),
markdownDescription: true,
errorMessages: true,
}),
null,
2
)
);
} catch (err) {
logger.warn(
'content',
`An error was encountered while creating the Json schema. Proceeding without it. Error: ${err}`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this warning also needs rewording. If so please feel free to commit that yourself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON is probably all caps when referred to as a name/noun?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://json-schema.org/ yes, so I've updated the warning message

alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
alexanderniebuhr marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
}
dataTypesStr += `};\n`;
break;
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const ASTRO_CONFIG_DEFAULTS = {
experimental: {
optimizeHoistedScript: false,
contentCollectionCache: false,
contentCollectionJsonSchema: false,
clientPrerender: false,
globalRoutePriority: false,
i18nDomains: false,
Expand Down Expand Up @@ -492,6 +493,10 @@ export const AstroConfigSchema = z.object({
.boolean()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.contentCollectionCache),
contentCollectionJsonSchema: z
.boolean()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.contentCollectionJsonSchema),
clientPrerender: z
.boolean()
.optional()
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.