1 | # typescript-json-schema
|
2 |
|
3 | [![npm version](https://img.shields.io/npm/v/typescript-json-schema.svg)](https://www.npmjs.com/package/typescript-json-schema) [![Build Status](https://travis-ci.org/YousefED/typescript-json-schema.svg?branch=master)](https://travis-ci.org/YousefED/typescript-json-schema)
|
4 |
|
5 | Generate json-schemas from your Typescript sources.
|
6 |
|
7 | ## Features
|
8 |
|
9 | * Compiles your Typescript program to get complete type information.
|
10 | * Translates required properties, extends, annotation keywords, property initializers as defaults. You can find examples for these features in the [test examples](https://github.com/YousefED/typescript-json-schema/tree/master/test/programs).
|
11 |
|
12 | ## Usage
|
13 |
|
14 | ### Command line
|
15 |
|
16 | * Install with `npm install typescript-json-schema -g`
|
17 | * Generate schema from a typescript type: `typescript-json-schema project/directory/tsconfig.json TYPE`
|
18 |
|
19 | To generate files for only _some_ types in `tsconfig.json` specify
|
20 | filenames or globs with the `--include` option. This is especially useful for large projects.
|
21 |
|
22 | In case no `tsconfig.json` is available for your project, you can directly specify the .ts files (this in this case we use some built-in compiler presets):
|
23 |
|
24 | * Generate schema from a typescript type: `typescript-json-schema "project/directory/**/*.ts" TYPE`
|
25 |
|
26 | The `TYPE` can either be a single, fully qualified type or `*` to generate the schema for all types.
|
27 |
|
28 | ```
|
29 | Usage: typescript-json-schema <path-to-typescript-files-or-tsconfig> <type>
|
30 |
|
31 | Options:
|
32 | --refs Create shared ref definitions. [boolean] [default: true]
|
33 | --aliasRefs Create shared ref definitions for the type aliases. [boolean] [default: false]
|
34 | --topRef Create a top-level ref definition. [boolean] [default: false]
|
35 | --titles Creates titles in the output schema. [boolean] [default: false]
|
36 | --defaultProps Create default properties definitions. [boolean] [default: false]
|
37 | --noExtraProps Disable additional properties in objects by default. [boolean] [default: false]
|
38 | --propOrder Create property order definitions. [boolean] [default: false]
|
39 | --required Create required array for non-optional properties. [boolean] [default: false]
|
40 | --strictNullChecks Make values non-nullable by default. [boolean] [default: false]
|
41 | --useTypeOfKeyword Use `typeOf` keyword (https://goo.gl/DC6sni) for functions. [boolean] [default: false]
|
42 | --out, -o The output file, defaults to using stdout
|
43 | --validationKeywords Provide additional validation keywords to include [array] [default: []]
|
44 | --include Further limit tsconfig to include only matching files [array] [default: []]
|
45 | --ignoreErrors Generate even if the program has errors. [boolean] [default: false]
|
46 | --excludePrivate Exclude private members from the schema [boolean] [default: false]
|
47 | --uniqueNames Use unique names for type symbols. [boolean] [default: false]
|
48 | ```
|
49 |
|
50 | ### Programmatic use
|
51 |
|
52 | ```ts
|
53 | import {resolve} from "path";
|
54 |
|
55 | import * as TJS from "typescript-json-schema";
|
56 |
|
57 | // optionally pass argument to schema generator
|
58 | const settings: TJS.PartialArgs = {
|
59 | required: true
|
60 | };
|
61 |
|
62 | // optionally pass ts compiler options
|
63 | const compilerOptions: TJS.CompilerOptions = {
|
64 | strictNullChecks: true
|
65 | }
|
66 |
|
67 | // optionally pass a base path
|
68 | const basePath = "./my-dir";
|
69 |
|
70 | const program = TJS.getProgramFromFiles([resolve("my-file.ts")], compilerOptions, basePath);
|
71 |
|
72 | // We can either get the schema for one file and one type...
|
73 | const schema = TJS.generateSchema(program, "MyType", settings);
|
74 |
|
75 |
|
76 | // ... or a generator that lets us incrementally get more schemas
|
77 |
|
78 | const generator = TJS.buildGenerator(program, settings);
|
79 |
|
80 | // all symbols
|
81 | const symbols = generator.getUserSymbols();
|
82 |
|
83 | // Get symbols for different types from generator.
|
84 | generator.getSchemaForSymbol("MyType");
|
85 | generator.getSchemaForSymbol("AnotherType");
|
86 | ```
|
87 |
|
88 | ```ts
|
89 | // In larger projects type names may not be unique,
|
90 | // while unique names may be enabled.
|
91 | const settings: TJS.PartialArgs = {
|
92 | uniqueNames: true
|
93 | };
|
94 |
|
95 | const generator = TJS.buildGenerator(program, settings);
|
96 |
|
97 | // A list of all types of a given name can then be retrieved.
|
98 | const symbolList = generator.getSymbols("MyType");
|
99 |
|
100 | // Choose the appropriate type, and continue with the symbol's unique name.
|
101 | generator.getSchemaForSymbol(symbolList[1].name);
|
102 |
|
103 | // Also it is possible to get a list of all symbols.
|
104 | const fullSymbolList = generator.getSymbols();
|
105 | ```
|
106 |
|
107 | `getSymbols('<SymbolName>')` and `getSymbols()` return an array of `SymbolRef`, which is of the following format:
|
108 |
|
109 | ```ts
|
110 | type SymbolRef = {
|
111 | name: string;
|
112 | typeName: string;
|
113 | fullyQualifiedName: string;
|
114 | symbol: ts.Symbol;
|
115 | };
|
116 | ```
|
117 |
|
118 | `getUserSymbols` and `getMainFileSymbols` return an array of `string`.
|
119 |
|
120 | ### Annotations
|
121 |
|
122 | The schema generator converts annotations to JSON schema properties.
|
123 |
|
124 | For example
|
125 |
|
126 | ```ts
|
127 | export interface Shape {
|
128 | /**
|
129 | * The size of the shape.
|
130 | *
|
131 | * @minimum 0
|
132 | * @TJS-type integer
|
133 | */
|
134 | size: number;
|
135 | }
|
136 | ```
|
137 |
|
138 | will be translated to
|
139 |
|
140 | ```json
|
141 | {
|
142 | "$ref": "#/definitions/Shape",
|
143 | "$schema": "http://json-schema.org/draft-07/schema#",
|
144 | "definitions": {
|
145 | "Shape": {
|
146 | "properties": {
|
147 | "size": {
|
148 | "description": "The size of the shape.",
|
149 | "minimum": 0,
|
150 | "type": "integer"
|
151 | }
|
152 | },
|
153 | "type": "object"
|
154 | }
|
155 | }
|
156 | }
|
157 | ```
|
158 |
|
159 | Note that we needed to use `@TJS-type` instead of just `@type` because of an [issue with the typescript compiler](https://github.com/Microsoft/TypeScript/issues/13498).
|
160 |
|
161 | ## Background
|
162 |
|
163 | 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.
|
164 |
|
165 | ## Debugging
|
166 |
|
167 | `npm run debug -- test/programs/type-alias-single/main.ts --aliasRefs true MyString`
|
168 |
|
169 | And connect via the debugger protocol.
|
170 |
|