UNPKG

5.09 kBMarkdownView Raw
1# ts-json-schema-generator
2
3![Test](https://github.com/vega/ts-json-schema-generator/workflows/Test/badge.svg)
4[![codecov](https://codecov.io/gh/vega/ts-json-schema-generator/branch/master/graph/badge.svg)](https://codecov.io/gh/vega/ts-json-schema-generator)
5[![npm dependencies](https://david-dm.org/vega/ts-json-schema-generator.svg)](https://www.npmjs.com/package/ts-json-schema-generator)
6[![npm version](https://img.shields.io/npm/v/ts-json-schema-generator.svg)](https://www.npmjs.com/package/ts-json-schema-generator)
7
8Extended version of [https://github.com/xiag-ag/typescript-to-json-schema](https://github.com/xiag-ag/typescript-to-json-schema).
9
10Inspired by [`YousefED/typescript-json-schema`](https://github.com/YousefED/typescript-json-schema). Here's the differences list:
11
12- this implementation avoids the use of `typeChecker.getTypeAtLocation()` (so probably it keeps correct type aliases)
13- processing AST and formatting JSON schema have been split into two independent steps
14- not exported types, interfaces, enums are not exposed in the `definitions` section in the JSON schema
15
16## Contributors
17
18This project is made possible by a [community of contributors](https://github.com/vega/ts-json-schema-generator/graphs/contributors). We welcome contributions of any kind (issues, code, documentation, examples, tests,...). Please read our [code of conduct](https://github.com/vega/vega/blob/master/CODE_OF_CONDUCT.md).
19
20## CLI Usage
21
22```bash
23npm install --save ts-json-schema-generator
24./node_modules/.bin/ts-json-schema-generator --path 'my/project/**.*.ts' --type 'My.Type.Full.Name'
25```
26
27Note that different platforms (e.g. Windows) may different path separators so you may have to adjust the command above.
28
29## Programmatic Usage
30
31```js
32// main.js
33
34const tsj = require("ts-json-schema-generator");
35const fs = require("fs");
36
37const config = {
38 path: "path/to/source/file",
39 tsconfig: "path/to/tsconfig.json",
40 type: "*", // Or <type-name> if you want to generate schema for that one type only
41};
42
43const output_path = "path/to/output/file";
44
45const schema = tsj.createGenerator(config).createSchema(config.type);
46const schemaString = JSON.stringify(schema, null, 2);
47fs.writeFile(output_path, schemaString, (err) => {
48 if (err) throw err;
49});
50```
51
52Run the schema generator via `node main.js`.
53
54## Options
55
56```
57-p, --path 'index.ts'
58 The path to the TypeScript source file. If this is not provided, the type will be searched in the project specified in the `.tsconfig`.
59
60-t, --type 'My.Type.Full.Name'
61 The type the generated schema will represent. If omitted, the generated schema will contain all
62 types found in the files matching path. The same is true if '*' is specified.
63
64-e, --expose <all|none|export>
65 all: Create shared $ref definitions for all types.
66 none: Do not create shared $ref definitions.
67 export (default): Create shared $ref definitions only for exported types (not tagged as `@internal`).
68
69-f, --tsconfig 'my/project/tsconfig.json'
70 Use a custom tsconfig file for processing typescript (see https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) instead of the default:
71 {
72 "compilerOptions": {
73 "noEmit": true,
74 "emitDecoratorMetadata": true,
75 "experimentalDecorators": true,
76 "target": "ES5",
77 "module": "CommonJS",
78 "strictNullChecks": false,
79 }
80 }
81
82-j, --jsDoc <extended|none|basic>
83 none: Do not use JsDoc annotations.
84 basic: Read JsDoc annotations to provide schema properties.
85 extended (default): Also read @nullable, and @asType annotations.
86
87--unstable
88 Do not sort properties.
89
90--strict-tuples
91 Do not allow additional items on tuples.
92
93--no-top-ref
94 Do not create a top-level $ref definition.
95
96--no-type-check
97 Skip type checks for better performance.
98
99--no-ref-encode
100 Do not encode references. According to the standard, references must be valid URIs but some tools do not support encoded references.
101
102--validationKeywords
103 Provide additional validation keywords to include.
104
105-o, --out
106 Specify the output file path. Without this option, the generator logs the response in the console.
107
108--additional-properties <true|false>
109 Controls whether or not to allow additional properties for objects that have no index signature.
110
111 true: Additional properties are allowed
112 false (default): Additional properties are not allowed
113```
114
115## Current state
116
117- `interface` types
118- `enum` types
119- `union`, `tuple`, `type[]` types
120- `Date`, `RegExp` types
121- `string`, `boolean`, `number` types
122- `"value"`, `123`, `true`, `false`, `null`, `undefined` literals
123- type aliases
124- generics
125- `typeof`
126- `keyof`
127- conditional types
128
129## Run locally
130
131`yarn --silent run run --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'`
132
133## Debug
134
135`yarn --silent run debug --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'`
136
137And connect via the debugger protocol.
138
139[AST Explorer](https://astexplorer.net/) is amazing for developers of this tool!