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

Rethink JSON Schema generation #2722

Open
ddbeck opened this issue Feb 27, 2025 · 3 comments
Open

Rethink JSON Schema generation #2722

ddbeck opened this issue Feb 27, 2025 · 3 comments
Labels
dependencies Pull requests that update a dependency file package:web-features schema Schema changes, proposals, and bugs tools and infrastructure Project internal tooling, such as linters, GitHub Actions, or repo settings

Comments

@ddbeck
Copy link
Collaborator

ddbeck commented Feb 27, 2025

ts-json-schema-generator only incompletely generates a schema based on our TypeScript types. It seems to have some longstanding bugs around keyof. I doubt they'll be fixed any time soon.

We should explore alternatives for keeping our TypeScript types and JSON Schema in sync.

Some routes we might consider:

@ddbeck ddbeck added dependencies Pull requests that update a dependency file package:web-features schema Schema changes, proposals, and bugs tools and infrastructure Project internal tooling, such as linters, GitHub Actions, or repo settings labels Feb 27, 2025
ddbeck added a commit to jcscottiii/web-features that referenced this issue Feb 27, 2025
ddbeck added a commit that referenced this issue Feb 27, 2025
Currently, the alternatives field is described to be an array. But the
type of item is missing. This change adds the type as string for each alternative.

See-also: #2722
Co-authored-by: Daniel D. Beck <daniel@ddbeck.com>
@jcscottiii
Copy link
Contributor

jcscottiii commented Feb 27, 2025

Author JSON Schema directly and generate TypesScript types

If you're open to it, I would vote for this approach. I will also suggest another tool named quicktype. It supports multiple languages (including typescript). If you want to try it out, you could paste the JSON schema into their online tool and change the source type to JSON Schema and change the language to typescript. (They also have Typescript Zod too)


webstatus.dev uses it on the data.schema.json here to generate the Go types. A few things to note:

  • It gets a little confused on the fields that are either or 1) a single string or 2) array of strings when using their Go generator. (Let me know if you want more details. Or you can try the Go generator on their online tool)
    • Unrelated question: Would you be open to switching to just an array of strings in the next major version of the schema? If not, that's okay.

As a result, webstatus.dev keeps a slightly modified version of the schema in the tree [1].

After making that modification, it works great!

@ddbeck
Copy link
Collaborator Author

ddbeck commented Feb 27, 2025

Thank you for the quicktype recommendation, @jcscottiii! I much prefer the idea of using a tool that someone has first hand experience with.

@ddbeck
Copy link
Collaborator Author

ddbeck commented Feb 27, 2025

And #1981 (comment) covers making the types consistent for arrays of strings. It is planned for the next major release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file package:web-features schema Schema changes, proposals, and bugs tools and infrastructure Project internal tooling, such as linters, GitHub Actions, or repo settings
Projects
None yet
Development

No branches or pull requests

2 participants