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

Conversation

alexanderniebuhr
Copy link
Member

@alexanderniebuhr alexanderniebuhr commented Feb 16, 2024

Changes

  • initial implementation of IntelliSense for Content Collection JSON roadmap#696
  • the implementation does not handle automatic inserting of schemas into files for lsp to pick that up
  • using this implementation JSON should be supported, and yaml should be supported if user used redhats yaml vscode extension
  • implementation using an experimental flag, to test things out
import { defineConfig } from 'astro/config';

export default defineConfig({
	experimental: {
+		contentCollectionJsonSchema: true
	}
});
import { defineCollection, z } from 'astro:content';

const test = defineCollection({
	type: 'data',
	schema: z.object({
+		"$schema": z.string().optional(),
		test: z.string()
	}),
});

export const collections = { test };
  • third caveat, users have to manually reference the schema in their content collection data files
{
+  "$schema": "../../../.astro/schemas/collections/i18n.json",
  "homepage": {
    "greeting": "ME",
    "preamble": "ME"
  }
}

Testing

  • not added any tests yet, since we would most likely need some snapshot tests 🤔

Docs

  • I think the config reference is pulled automatically for the experimental flag
  • unsure whether we need docs &or where we would document experimental features (cc @sarah11918)

Copy link

changeset-bot bot commented Feb 16, 2024

🦋 Changeset detected

Latest commit: 413bafd

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Feb 16, 2024
@alexanderniebuhr
Copy link
Member Author

alexanderniebuhr commented Feb 16, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @alexanderniebuhr and the rest of your teammates on Graphite Graphite

@github-actions github-actions bot added docs pr A PR that includes documentation for review semver: minor Change triggers a `minor` release labels Feb 16, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

This PR is blocked because it contains a minor changeset. A reviewer will merge this at the next release if approved.

packages/astro/src/content/types-generator.ts Outdated Show resolved Hide resolved
packages/astro/src/content/types-generator.ts Outdated Show resolved Hide resolved
packages/astro/src/content/types-generator.ts Outdated Show resolved Hide resolved
packages/astro/src/content/types-generator.ts Outdated Show resolved Hide resolved
packages/astro/src/content/types-generator.ts Outdated Show resolved Hide resolved
@ematipico ematipico added this to the 4.5 milestone Feb 19, 2024
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

Looks great! Have a few suggestions still, but not blocking so I'm preemptively approving.

.changeset/slow-items-heal.md Outdated Show resolved Hide resolved
.changeset/slow-items-heal.md Outdated Show resolved Hide resolved
packages/astro/src/content/types-generator.ts Outdated Show resolved Hide resolved
Comment on lines +442 to +447
let zodSchemaForJson = collectionConfig.schema;
if (zodSchemaForJson instanceof z.ZodObject) {
zodSchemaForJson = zodSchemaForJson.extend({
$schema: z.string().optional(),
});
}
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!

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Thanks for this @alexanderniebuhr ! Left some things to think about for the changeset, which will then take us on to the actual docs content!

Because this is experimental, we typically would not mention it in docs on e.g. the content collections page. So, we don't need another docs PR, because this should be the only place it appears while experimental! 🙌

.changeset/slow-items-heal.md Outdated Show resolved Hide resolved
.changeset/slow-items-heal.md Outdated Show resolved Hide resolved
.changeset/slow-items-heal.md Show resolved Hide resolved
* @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.

} 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 and others added 3 commits March 5, 2024 15:07
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

I think just some tiny nits to clean up here, otherwise, LGTM!

.changeset/slow-items-heal.md Outdated Show resolved Hide resolved
packages/astro/src/@types/astro.ts Outdated Show resolved Hide resolved
packages/astro/src/content/types-generator.ts Outdated Show resolved Hide resolved
alexanderniebuhr and others added 2 commits March 5, 2024 20:48
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
@ematipico ematipico merged commit 65692fa into main Mar 8, 2024
14 checks passed
@ematipico ematipico deleted the nbhr/feat_experimental_content_collection_JSON_schemas branch March 8, 2024 10:54
@astrobot-houston astrobot-houston mentioned this pull request Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs pr A PR that includes documentation for review pkg: astro Related to the core `astro` package (scope) semver: minor Change triggers a `minor` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants