-
Notifications
You must be signed in to change notification settings - Fork 393
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
Make schema titles as property types optional #576
base: master
Are you sure you want to change the base?
Conversation
Make property title expansion optional
Fix tests
Update package.json
Version name in schema name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems reasonable overall, but the implementation needs a bit more iteration. Could you please:
- Implement this as a normalizer step instead, moving the
title
to adescription
whenoptions.inferTypeAliasFromTitle
isfalse
(thereby, isolating the change from all downstream code). - Add a test for the normalizer step, in addition to the e2e tests you added.
- Ensure that titles are emitted as code comments. I don't think we should elide them from the output entirely.
@@ -1,6 +1,6 @@ | |||
{ | |||
"name": "json-schema-to-typescript", | |||
"version": "13.1.2", | |||
"name": "@linktr.ee/json-schema-to-typescript", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this change from this PR.
@@ -179,7 +193,15 @@ export async function compile(schema: JSONSchema4, name: string, options: Partia | |||
const formatted = format(generated, _options) | |||
log('white', 'formatter', time(), '✅ Result:', formatted) | |||
|
|||
return formatted | |||
return { | |||
typescript: formatted, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change. Please avoid changing the result shape, and instead focus on making the most targeted change possible.
/** | ||
* Expand the title field of each property into a separate type | ||
*/ | ||
useSchemaTitleAsPropertyType: boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call this inferTypeAliasFromTitle
By default, this library hoists the "title" filed of a property as the name of the property type. For example, the following schema:
Produces this type:
Per the JSON schema specifications, titles are annotations for description purposes, so it can often be a clearer interface to use the actual type (like
string
orboolean
) rather than the "Title" in the JSON schema file.To use this feature, all you have to do is pass in
useSchemaTitleAsPropertyType=false
, and the following schema will be produced:Default behavior remains intact and tests have been added to support this behavior.