Skip to content
New issue

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

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

Already on GitHub? # to your account

Generate JSON Schema definitions from types #44

Merged
merged 1 commit into from
Mar 7, 2023

Conversation

ddbeck
Copy link
Collaborator

@ddbeck ddbeck commented Jan 19, 2023

This fell out of playing around with expanding the YAML with constituent features data. I don't know if this is a good idea, but it is an idea. This will be modestly more interesting after #40.

@ddbeck ddbeck marked this pull request as ready for review February 17, 2023 15:28
@foolip
Copy link
Collaborator

foolip commented Feb 20, 2023

@ddbeck I'm not sure I understand what this does. Is it purely refactoring, or will it catch errors that weren't caught before?

@ddbeck
Copy link
Collaborator Author

ddbeck commented Feb 21, 2023

@foolip Sorry, I did a lousy job of contextualizing this.

Right now, we've got the schema (in a broad sense) defined in two places: a JSON Schema file and a TypeScript interface in index.ts. On another PR, I changed one and not the other and thought to myself: surely these ought to be the same? And if they ought to be the same, must we remember to make sure they're the same?

So this PR does a few things:

  • It adds an npm run command to generate JSON Schema from the TypeScript interface definition in index.ts. The generated bits go into a fully-generated defs.schema.json file (unfortunately, the top-most object can't be generated because TypeScript lacks a way to restrict keys to the corresponding patternProperties in JSON Schema).
  • It adds annotations to index.ts to supply extra details to the JSON Schema generator that TypeScript doesn't have a direct analogue for (i.e., descriptions and string formats).
  • It changes the way the schema and schema test script work, to use the defs.schema.json file.

Your comment does suggest one additional step, which I have not done (but could do): test that a schema derived from index.ts does not diverge with the defs.schema.json file. This would actually catch the mistake of not updating the schema when index.ts changes (or vice versa).

@foolip
Copy link
Collaborator

foolip commented Mar 7, 2023

Thanks @ddbeck! If I understand the setup in BCD correctly that generates TypeScript definitions from the schema using json-schema-to-typescript, and this PR is generating the schema from TypeScript using ts-json-schema-generator.

I don't have any opinions about which direction is better, so this seems fine :)

@foolip foolip merged commit d1aaf0c into web-platform-dx:main Mar 7, 2023
@ddbeck ddbeck deleted the generate-schema-from-ts branch March 28, 2023 11:25
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants