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

Make schema titles as property types optional #576

Open
wants to merge 27 commits into
base: master
Choose a base branch
from

Conversation

ethanpmullen
Copy link

By default, this library hoists the "title" filed of a property as the name of the property type. For example, the following schema:

export const input = {
  title: 'Example Schema',
  type: 'object',
  properties: {
    firstName: {
      title: 'First Name',
      type: 'string',
    },
    lastName: {
      title: 'Last Name',
      id: 'lastName',
      type: 'string',
    }
  },
  required: ['firstName', 'lastName'],
}

Produces this type:

export type FirstName = string;

export type LastName = string;

export interface ExampleSchema {
      firstName: FirstName;
      lastName: LastName;
      [k: string]: unknown;
}

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 or boolean) 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:

export interface ExampleSchema {
      firstName: string;
      lastName: string;
      [k: string]: unknown;
}

Default behavior remains intact and tests have been added to support this behavior.

Copy link
Owner

@bcherny bcherny left a 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:

  1. Implement this as a normalizer step instead, moving the title to a description when options.inferTypeAliasFromTitle is false (thereby, isolating the change from all downstream code).
  2. Add a test for the normalizer step, in addition to the e2e tests you added.
  3. 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",
Copy link
Owner

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,
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants