@@ -48,6 +48,7 @@ Options:
48
48
--rejectDateType Rejects Date fields in type definitions. [boolean] [default: false]
49
49
--id Set schema id. [string] [default: ""]
50
50
--defaultNumberType Default number type. [choices: "number", "integer"] [default: "number"]
51
+ --tsNodeRegister Use ts-node/register (needed for require typescript files). [boolean] [default: false]
51
52
```
52
53
53
54
### Programmatic use
@@ -59,12 +60,12 @@ import * as TJS from "typescript-json-schema";
59
60
60
61
// optionally pass argument to schema generator
61
62
const settings: TJS .PartialArgs = {
62
- required: true ,
63
+ required: true ,
63
64
};
64
65
65
66
// optionally pass ts compiler options
66
67
const compilerOptions: TJS .CompilerOptions = {
67
- strictNullChecks: true ,
68
+ strictNullChecks: true ,
68
69
};
69
70
70
71
// optionally pass a base path
@@ -95,7 +96,7 @@ generator.getSchemaForSymbol("AnotherType");
95
96
// In larger projects type names may not be unique,
96
97
// while unique names may be enabled.
97
98
const settings: TJS .PartialArgs = {
98
- uniqueNames: true ,
99
+ uniqueNames: true ,
99
100
};
100
101
101
102
const generator = TJS .buildGenerator (program , settings );
@@ -114,10 +115,10 @@ const fullSymbolList = generator.getSymbols();
114
115
115
116
``` ts
116
117
type SymbolRef = {
117
- name: string ;
118
- typeName: string ;
119
- fullyQualifiedName: string ;
120
- symbol: ts .Symbol ;
118
+ name: string ;
119
+ typeName: string ;
120
+ fullyQualifiedName: string ;
121
+ symbol: ts .Symbol ;
121
122
};
122
123
```
123
124
@@ -131,34 +132,34 @@ For example
131
132
132
133
``` ts
133
134
export interface Shape {
134
- /**
135
- * The size of the shape.
136
- *
137
- * @minimum 0
138
- * @TJS-type integer
139
- */
140
- size: number ;
135
+ /**
136
+ * The size of the shape.
137
+ *
138
+ * @minimum 0
139
+ * @TJS-type integer
140
+ */
141
+ size: number ;
141
142
}
142
143
```
143
144
144
145
will be translated to
145
146
146
147
``` json
147
148
{
148
- "$ref" : " #/definitions/Shape" ,
149
- "$schema" : " http://json-schema.org/draft-07/schema#" ,
150
- "definitions" : {
151
- "Shape" : {
152
- "properties" : {
153
- "size" : {
154
- "description" : " The size of the shape." ,
155
- "minimum" : 0 ,
156
- "type" : " integer"
149
+ "$ref" : " #/definitions/Shape" ,
150
+ "$schema" : " http://json-schema.org/draft-07/schema#" ,
151
+ "definitions" : {
152
+ "Shape" : {
153
+ "properties" : {
154
+ "size" : {
155
+ "description" : " The size of the shape." ,
156
+ "minimum" : 0 ,
157
+ "type" : " integer"
158
+ }
159
+ },
160
+ "type" : " object"
157
161
}
158
- },
159
- "type" : " object"
160
162
}
161
- }
162
163
}
163
164
```
164
165
@@ -172,52 +173,52 @@ Example:
172
173
173
174
``` ts
174
175
export interface ShapesData {
175
- /**
176
- * Specify individual fields in items.
177
- *
178
- * @items.type integer
179
- * @items.minimum 0
180
- */
181
- sizes: number [];
182
-
183
- /**
184
- * Or specify a JSON spec:
185
- *
186
- * @items {"type":"string","format":"email"}
187
- */
188
- emails: string [];
176
+ /**
177
+ * Specify individual fields in items.
178
+ *
179
+ * @items.type integer
180
+ * @items.minimum 0
181
+ */
182
+ sizes: number [];
183
+
184
+ /**
185
+ * Or specify a JSON spec:
186
+ *
187
+ * @items {"type":"string","format":"email"}
188
+ */
189
+ emails: string [];
189
190
}
190
191
```
191
192
192
193
Translation:
193
194
194
195
``` json
195
196
{
196
- "$ref" : " #/definitions/ShapesData" ,
197
- "$schema" : " http://json-schema.org/draft-07/schema#" ,
198
- "definitions" : {
199
- "Shape" : {
200
- "properties" : {
201
- "sizes" : {
202
- "description" : " Specify individual fields in items." ,
203
- "items" : {
204
- "minimum" : 0 ,
205
- "type" : " integer"
206
- },
207
- "type" : " array"
208
- },
209
- "emails" : {
210
- "description" : " Or specify a JSON spec:" ,
211
- "items" : {
212
- "format" : " email" ,
213
- "type" : " string"
214
- },
215
- "type" : " array"
197
+ "$ref" : " #/definitions/ShapesData" ,
198
+ "$schema" : " http://json-schema.org/draft-07/schema#" ,
199
+ "definitions" : {
200
+ "Shape" : {
201
+ "properties" : {
202
+ "sizes" : {
203
+ "description" : " Specify individual fields in items." ,
204
+ "items" : {
205
+ "minimum" : 0 ,
206
+ "type" : " integer"
207
+ },
208
+ "type" : " array"
209
+ },
210
+ "emails" : {
211
+ "description" : " Or specify a JSON spec:" ,
212
+ "items" : {
213
+ "format" : " email" ,
214
+ "type" : " string"
215
+ },
216
+ "type" : " array"
217
+ }
218
+ },
219
+ "type" : " object"
216
220
}
217
- },
218
- "type" : " object"
219
221
}
220
- }
221
222
}
222
223
```
223
224
@@ -232,12 +233,80 @@ Example:
232
233
``` typescript
233
234
type integer = number ;
234
235
interface MyObject {
235
- n: integer ;
236
+ n: integer ;
236
237
}
237
238
```
238
239
239
240
Note: this feature doesn't work for generic types & array types, it mainly works in very simple cases.
240
241
242
+ ### ` require ` a variable from a file
243
+
244
+ (for requiring typescript files is needed to set argument ` tsNodeRegister ` to true)
245
+
246
+ When you want to import for example an object or an array into your property defined in annotation, you can use ` require ` .
247
+
248
+ Example:
249
+
250
+ ``` ts
251
+ export interface InnerData {
252
+ age: number ;
253
+ name: string ;
254
+ free: boolean ;
255
+ }
256
+
257
+ export interface UserData {
258
+ /**
259
+ * Specify required object
260
+ *
261
+ * @examples require("./example.ts").example
262
+ */
263
+ data: InnerData ;
264
+ }
265
+ ```
266
+
267
+ file ` example.ts `
268
+
269
+ ``` ts
270
+ export const example: InnerData [] = [{
271
+ age: 30 ,
272
+ name: " Ben" ,
273
+ free: false
274
+ }]
275
+ ```
276
+
277
+ Translation:
278
+
279
+ ``` json
280
+ {
281
+ "$schema" : " http://json-schema.org/draft-07/schema#" ,
282
+ "properties" : {
283
+ "data" : {
284
+ "description" : " Specify required object" ,
285
+ "examples" : [
286
+ {
287
+ "age" : 30 ,
288
+ "name" : " Ben" ,
289
+ "free" : false
290
+ }
291
+ ],
292
+ "type" : " object" ,
293
+ "properties" : {
294
+ "age" : { "type" : " number" },
295
+ "name" : { "type" : " string" },
296
+ "free" : { "type" : " boolean" }
297
+ },
298
+ "required" : [" age" , " free" , " name" ]
299
+ }
300
+ },
301
+ "required" : [" data" ],
302
+ "type" : " object"
303
+ }
304
+ ```
305
+
306
+ Also you can use ` require(".").example ` , which will try to find exported variable with name 'example' in current file. Or you can use ` require("./someFile.ts") ` , which will try to use default exported variable from 'someFile.ts'.
307
+
308
+ Note: For ` examples ` a required variable must be an array.
309
+
241
310
## Background
242
311
243
312
Inspired and builds upon [ Typson] ( https://github.com/lbovet/typson/ ) , but typescript-json-schema is compatible with more recent Typescript versions. Also, since it uses the Typescript compiler internally, more advanced scenarios are possible. If you are looking for a library that uses the AST instead of the type hierarchy and therefore better support for type aliases, have a look at [ vega/ts-json-schema-generator] ( https://github.com/vega/ts-json-schema-generator ) .
0 commit comments