Skip to content

Commit eaf3b87

Browse files
committed
feat: Generate titles from definition names
All root child definitions have a 'title' added to their definition if the opt-in definitionTitles option is set
1 parent d500564 commit eaf3b87

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ By default, the command-line generator will use the `tsconfig.json` file in the
4949
--markdown-description Generate `markdownDescription` in addition to `description`.
5050
--functions <functions> How to handle functions. `fail` will throw an error. `comment` will add a comment. `hide` will treat the function like a NeverType or HiddenType.
5151
(choices: "fail", "comment", "hide", default: "comment")
52+
--definition-titles Generates titles from definition names
5253
--minify Minify generated schema (default: false)
5354
--unstable Do not sort properties
5455
--strict-tuples Do not allow additional items on tuples

src/Config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface Config {
1616
additionalProperties?: boolean;
1717
discriminatorType?: "json-schema" | "open-api";
1818
functions?: FunctionOptions;
19+
definitionTitles?: boolean;
1920
}
2021

2122
export type CompletedConfig = Config & typeof DEFAULT_CONFIG;
@@ -36,4 +37,5 @@ export const DEFAULT_CONFIG: Omit<Required<Config>, "path" | "type" | "schemaId"
3637
additionalProperties: false,
3738
discriminatorType: "json-schema",
3839
functions: "comment",
40+
definitionTitles: false,
3941
};

src/SchemaGenerator.ts

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ export class SchemaGenerator {
135135
const name = child.getName();
136136
if (!(name in definitions)) {
137137
definitions[name] = this.typeFormatter.getDefinition(child.getType());
138+
if (this.config?.definitionTitles) {
139+
definitions[name].title = name;
140+
}
138141
}
139142
return definitions;
140143
}, childDefinitions);

test/config.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ describe("config", () => {
229229
}),
230230
);
231231

232+
it(
233+
"definition-titles",
234+
assertSchema("definition-titles", {
235+
type: "MyObject",
236+
expose: "export",
237+
topRef: false,
238+
definitionTitles: true,
239+
jsDoc: "none",
240+
}),
241+
);
242+
232243
it(
233244
"jsdoc-complex-none",
234245
assertSchema("jsdoc-complex-none", {

test/config/definition-titles/main.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export interface ExportInterface {
2+
exportValue: string;
3+
}
4+
export type ExportAlias = ExportInterface;
5+
6+
interface PrivateInterface {
7+
privateValue: string;
8+
}
9+
type PrivateAlias = PrivateInterface;
10+
11+
interface MixedInterface {
12+
mixedValue: ExportAlias;
13+
}
14+
export type MixedAlias = PrivateInterface;
15+
16+
export type PublicAnonymousTypeLiteral = {
17+
publicValue: string;
18+
};
19+
20+
type PrivateAnonymousTypeLiteral = {
21+
privateValue: string;
22+
};
23+
24+
export interface MyObject {
25+
exportInterface: ExportInterface;
26+
exportAlias: ExportAlias;
27+
28+
privateInterface: PrivateInterface;
29+
privateAlias: PrivateAlias;
30+
31+
mixedInterface: MixedInterface;
32+
mixedAlias: MixedAlias;
33+
34+
publicAnonymousTypeLiteral: PublicAnonymousTypeLiteral;
35+
privateAnonymousTypeLiteral: PrivateAnonymousTypeLiteral;
36+
}
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"additionalProperties": false,
4+
"definitions": {
5+
"ExportAlias": {
6+
"$ref": "#/definitions/ExportInterface",
7+
"title": "ExportAlias"
8+
},
9+
"ExportInterface": {
10+
"additionalProperties": false,
11+
"properties": {
12+
"exportValue": {
13+
"type": "string"
14+
}
15+
},
16+
"required": [
17+
"exportValue"
18+
],
19+
"title": "ExportInterface",
20+
"type": "object"
21+
},
22+
"MixedAlias": {
23+
"additionalProperties": false,
24+
"properties": {
25+
"privateValue": {
26+
"type": "string"
27+
}
28+
},
29+
"required": [
30+
"privateValue"
31+
],
32+
"title": "MixedAlias",
33+
"type": "object"
34+
},
35+
"PublicAnonymousTypeLiteral": {
36+
"additionalProperties": false,
37+
"properties": {
38+
"publicValue": {
39+
"type": "string"
40+
}
41+
},
42+
"required": [
43+
"publicValue"
44+
],
45+
"title": "PublicAnonymousTypeLiteral",
46+
"type": "object"
47+
}
48+
},
49+
"properties": {
50+
"exportAlias": {
51+
"$ref": "#/definitions/ExportAlias"
52+
},
53+
"exportInterface": {
54+
"$ref": "#/definitions/ExportInterface"
55+
},
56+
"mixedAlias": {
57+
"$ref": "#/definitions/MixedAlias"
58+
},
59+
"mixedInterface": {
60+
"additionalProperties": false,
61+
"properties": {
62+
"mixedValue": {
63+
"$ref": "#/definitions/ExportAlias"
64+
}
65+
},
66+
"required": [
67+
"mixedValue"
68+
],
69+
"type": "object"
70+
},
71+
"privateAlias": {
72+
"additionalProperties": false,
73+
"properties": {
74+
"privateValue": {
75+
"type": "string"
76+
}
77+
},
78+
"required": [
79+
"privateValue"
80+
],
81+
"type": "object"
82+
},
83+
"privateAnonymousTypeLiteral": {
84+
"additionalProperties": false,
85+
"properties": {
86+
"privateValue": {
87+
"type": "string"
88+
}
89+
},
90+
"required": [
91+
"privateValue"
92+
],
93+
"type": "object"
94+
},
95+
"privateInterface": {
96+
"additionalProperties": false,
97+
"properties": {
98+
"privateValue": {
99+
"type": "string"
100+
}
101+
},
102+
"required": [
103+
"privateValue"
104+
],
105+
"type": "object"
106+
},
107+
"publicAnonymousTypeLiteral": {
108+
"$ref": "#/definitions/PublicAnonymousTypeLiteral"
109+
}
110+
},
111+
"required": [
112+
"exportInterface",
113+
"exportAlias",
114+
"privateInterface",
115+
"privateAlias",
116+
"mixedInterface",
117+
"mixedAlias",
118+
"publicAnonymousTypeLiteral",
119+
"privateAnonymousTypeLiteral"
120+
],
121+
"type": "object"
122+
}

ts-json-schema-generator.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const args = new Command()
3636
.choices(["fail", "comment", "hide"])
3737
.default("comment"),
3838
)
39+
.option("--definition-titles", "Generate titles from defintion names", false)
3940
.option("--minify", "Minify generated schema", false)
4041
.option("--unstable", "Do not sort properties")
4142
.option("--strict-tuples", "Do not allow additional items on tuples")
@@ -56,6 +57,7 @@ const args = new Command()
5657

5758
const config: Config = {
5859
minify: args.minify,
60+
definitionTitles: args.definitionTitles,
5961
path: args.path,
6062
tsconfig:
6163
typeof args.tsconfig === "string" ? args.tsconfig : findConfigFile(process.cwd(), (f) => tsSys.fileExists(f)),

0 commit comments

Comments
 (0)