json-schema-to-typescript
json-schema-to-typescript copied to clipboard
Make schema titles as property types optional
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.