├── .gitattributes ├── .github ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── test.yml ├── .gitignore ├── .npmignore ├── .prettierrc.json ├── LICENSE ├── README.md ├── api.md ├── bin └── typescript-json-schema ├── example ├── invoice │ └── line.ts ├── misc │ └── dimension.ts └── product.ts ├── package.json ├── test ├── error.test.ts ├── out-option.test.ts ├── programs │ ├── abstract-class │ │ ├── main.ts │ │ └── schema.json │ ├── abstract-extends │ │ ├── main.ts │ │ └── schema.json │ ├── annotation-default │ │ ├── main.ts │ │ └── schema.json │ ├── annotation-id │ │ ├── main.ts │ │ └── schema.json │ ├── annotation-items │ │ ├── main.ts │ │ └── schema.json │ ├── annotation-ref │ │ ├── main.ts │ │ └── schema.json │ ├── annotation-required │ │ ├── examples.ts │ │ ├── main.ts │ │ └── schema.json │ ├── annotation-title │ │ ├── main.ts │ │ └── schema.json │ ├── annotation-tjs │ │ ├── main.ts │ │ └── schema.json │ ├── any-unknown │ │ ├── main.ts │ │ └── schema.json │ ├── argument-id │ │ ├── main.ts │ │ └── schema.MyObject.json │ ├── array-and-description │ │ ├── main.ts │ │ └── schema.json │ ├── array-empty │ │ ├── main.ts │ │ └── schema.json │ ├── array-readonly │ │ ├── main.ts │ │ └── schema.json │ ├── array-types │ │ ├── main.ts │ │ └── schema.json │ ├── builtin-names │ │ ├── main.ts │ │ └── schema.json │ ├── class-extends │ │ ├── main.ts │ │ └── schema.json │ ├── class-single │ │ ├── main.ts │ │ └── schema.json │ ├── comments-comment │ │ ├── main.ts │ │ └── schema.json │ ├── comments-from-lib │ │ ├── main.ts │ │ └── schema.json │ ├── comments-imports │ │ ├── color.ts │ │ ├── main.ts │ │ ├── schema.json │ │ └── text.ts │ ├── comments-inline-tags │ │ ├── main.ts │ │ └── schema.json │ ├── comments-override │ │ ├── main.ts │ │ └── schema.json │ ├── comments │ │ ├── main.ts │ │ └── schema.json │ ├── const-as-enum │ │ ├── main.ts │ │ └── schema.json │ ├── const-keyword │ │ ├── main.ts │ │ └── schema.json │ ├── custom-dates │ │ ├── main.ts │ │ └── schema.json │ ├── dates │ │ ├── main.ts │ │ └── schema.json │ ├── default-properties │ │ ├── main.ts │ │ └── schema.json │ ├── enums-compiled-compute │ │ ├── main.ts │ │ └── schema.json │ ├── enums-mixed │ │ ├── main.ts │ │ └── schema.json │ ├── enums-number-initialized │ │ ├── main.ts │ │ └── schema.json │ ├── enums-number │ │ ├── main.ts │ │ └── schema.json │ ├── enums-string │ │ ├── main.ts │ │ └── schema.json │ ├── enums-value-in-interface │ │ ├── main.ts │ │ └── schema.json │ ├── extra-properties │ │ ├── main.ts │ │ └── schema.json │ ├── force-type-imported │ │ ├── main.ts │ │ ├── schema.json │ │ └── widget.ts │ ├── force-type │ │ ├── main.ts │ │ └── schema.json │ ├── generate-all-types │ │ ├── main.ts │ │ └── schema.json │ ├── generic-anonymous │ │ ├── main.ts │ │ └── schema.json │ ├── generic-arrays │ │ ├── main.ts │ │ └── schema.json │ ├── generic-hell │ │ ├── main.ts │ │ └── schema.json │ ├── generic-multiargs │ │ ├── main.ts │ │ └── schema.json │ ├── generic-multiple │ │ ├── main.ts │ │ └── schema.json │ ├── generic-recursive │ │ ├── main.ts │ │ └── schema.json │ ├── generic-simple │ │ ├── main.ts │ │ └── schema.json │ ├── ignored-required │ │ ├── main.ts │ │ └── schema.json │ ├── imports │ │ ├── main.ts │ │ ├── module1.ts │ │ ├── module2.ts │ │ └── schema.json │ ├── interface-extends │ │ ├── main.ts │ │ └── schema.json │ ├── interface-extra-props │ │ ├── main.ts │ │ └── schema.json │ ├── interface-multi │ │ ├── main.ts │ │ └── schema.json │ ├── interface-recursion │ │ ├── main.ts │ │ └── schema.json │ ├── interface-single │ │ ├── main.ts │ │ └── schema.json │ ├── key-in-key-of-multi-underscores │ │ ├── main.ts │ │ └── schema.json │ ├── key-in-key-of-multi │ │ ├── main.ts │ │ └── schema.json │ ├── key-in-key-of-single │ │ ├── main.ts │ │ └── schema.json │ ├── map-types │ │ ├── main.ts │ │ └── schema.json │ ├── module-interface-deep │ │ ├── main.ts │ │ └── schema.json │ ├── module-interface-single │ │ ├── main.ts │ │ └── schema.json │ ├── namespace-deep-1 │ │ ├── main.ts │ │ └── schema.json │ ├── namespace-deep-2 │ │ ├── main.ts │ │ └── schema.json │ ├── namespace │ │ ├── main.ts │ │ └── schema.json │ ├── never │ │ ├── main.ts │ │ └── schema.json │ ├── no-ref │ │ ├── main.ts │ │ └── schema.json │ ├── no-unrelated-definitions │ │ ├── main.ts │ │ ├── schema.MyObject.json │ │ ├── schema.MyOtherObject.json │ │ ├── schema.MyOtherObjectWithOverride.json │ │ ├── schema.program.json │ │ └── tsconfig.json │ ├── numeric-keys-and-others │ │ ├── main.ts │ │ └── schema.json │ ├── object-numeric-index-as-property │ │ ├── main.ts │ │ └── schema.json │ ├── object-numeric-index │ │ ├── main.ts │ │ └── schema.json │ ├── optionals-derived │ │ ├── main.ts │ │ └── schema.json │ ├── optionals │ │ ├── main.ts │ │ └── schema.json │ ├── private-members │ │ ├── main.ts │ │ └── schema.json │ ├── prop-override │ │ ├── main.ts │ │ ├── schema.json │ │ └── third-party.ts │ ├── satisfies-keyword │ │ ├── main.ts │ │ └── schema.json │ ├── strict-null-checks │ │ ├── main.ts │ │ └── schema.json │ ├── string-literals-inline │ │ ├── main.ts │ │ └── schema.json │ ├── string-literals │ │ ├── main.ts │ │ └── schema.json │ ├── string-template-literal │ │ ├── main.ts │ │ └── schema.json │ ├── symbol │ │ ├── main.ts │ │ └── schema.json │ ├── tsconfig │ │ ├── exclude.ts │ │ ├── includedAlways.ts │ │ ├── includedByConfig.ts │ │ └── tsconfig.json │ ├── type-alias-never │ │ ├── main.ts │ │ └── schema.json │ ├── type-alias-or │ │ ├── main.ts │ │ └── schema.json │ ├── type-alias-schema-override │ │ ├── main.ts │ │ └── schema.json │ ├── type-alias-single-annotated │ │ ├── main.ts │ │ └── schema.json │ ├── type-alias-single │ │ ├── main.ts │ │ └── schema.json │ ├── type-alias-undefined │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-alias-ref-topref │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-alias-ref │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-anonymous │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-fixed-size-array │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-local-namespace │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-local-namsepace │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-mixed │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-multitype-array │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-object │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-partial │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-primitive │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-recursive-alias-topref │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-recursive-anonymous │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-recursive-export │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-recursive-object-topref │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-tuple-of-variable-length │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-tuple-with-names │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-tuple-with-rest-element │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-tuple │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-union-namespace │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases-union │ │ ├── main.ts │ │ └── schema.json │ ├── type-aliases │ │ ├── main.ts │ │ └── schema.json │ ├── type-anonymous │ │ ├── main.ts │ │ └── schema.json │ ├── type-default-number-as-integer │ │ ├── main.ts │ │ └── schema.json │ ├── type-function │ │ ├── main.ts │ │ └── schema.json │ ├── type-globalThis │ │ ├── main.ts │ │ └── schema.json │ ├── type-intersection-recursive-no-additional │ │ ├── main.ts │ │ └── schema.json │ ├── type-intersection-recursive │ │ ├── main.ts │ │ └── schema.json │ ├── type-intersection │ │ ├── main.ts │ │ └── schema.json │ ├── type-literals │ │ ├── main.ts │ │ └── schema.json │ ├── type-mapped-types │ │ ├── main.ts │ │ └── schema.json │ ├── type-no-aliases-recursive-topref │ │ ├── main.ts │ │ └── schema.json │ ├── type-nullable │ │ ├── main.ts │ │ └── schema.json │ ├── type-primitives │ │ ├── main.ts │ │ └── schema.json │ ├── type-recursive │ │ ├── main.ts │ │ └── schema.json │ ├── type-union-strict-null-keep-description │ │ ├── main.ts │ │ └── schema.json │ ├── type-union-tagged │ │ ├── main.ts │ │ └── schema.json │ ├── type-union │ │ ├── main.ts │ │ └── schema.json │ ├── typeof-keyword │ │ ├── main.ts │ │ └── schema.json │ ├── undefined-property │ │ ├── main.ts │ │ └── schema.json │ ├── unique-names-multiple-subdefinitions │ │ ├── main.ts │ │ ├── other.ts │ │ └── schema.json │ ├── unique-names │ │ ├── main.ts │ │ ├── other.ts │ │ ├── schema.MyObject.f2191116.json │ │ └── schema.MyObject.fded1213.json │ ├── user-symbols │ │ ├── main.ts │ │ └── schema.json │ └── user-validation-keywords │ │ ├── main.ts │ │ └── schema.json ├── require.test.ts └── schema.test.ts ├── tsconfig.json ├── tslint.json ├── typescript-json-schema-cli.ts ├── typescript-json-schema.ts ├── update-docs.js └── yarn.lock /.gitattributes: -------------------------------------------------------------------------------- 1 | bin/typescript-json-schema text eol=lf -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please: 2 | - [ ] Make your pull request atomic, fixing one issue at a time unless there are many relevant issues that cannot be decoupled. 3 | - [ ] Provide a test case & update the documentation in the `Readme.md` 4 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v3 14 | - uses: actions/setup-node@v3 15 | - run: yarn --frozen-lockfile 16 | - name: Lint 17 | run: yarn lint 18 | - name: Test 19 | run: yarn test 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | test/*.js 3 | test/*.d.ts 4 | test/*.map 5 | .vscode/*.* 6 | npm-debug.log 7 | yarn-error.log 8 | .idea 9 | *~ 10 | .#* 11 | dist/ 12 | .DS_Store 13 | .history/ -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .gitattributes 3 | .github 4 | .travis.yml 5 | .vscode/*.* 6 | example/ 7 | node_modules 8 | test/ 9 | typings/ 10 | yarn-error.log 11 | dist/test/* 12 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 120, 3 | "tabWidth": 4, 4 | "overrides": [ 5 | { 6 | "files": "*.json", 7 | "options": { 8 | "tabWidth": 2 9 | } 10 | }, 11 | { 12 | "files": "*.yml", 13 | "options": { 14 | "tabWidth": 2 15 | } 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, typescript-json-schema contributors 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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) ![Test](https://github.com/YousefED/typescript-json-schema/workflows/Test/badge.svg) 4 | 5 | Generate json-schemas from your Typescript sources. This library is lightweight and more or less in maintenance mode. For a more complete JSON schema generator, take a look at [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator). 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 [api doc](https://github.com/YousefED/typescript-json-schema/tree/master/api.md) or 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 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 | --esModuleInterop Use esModuleInterop when loading typescript modules. [boolean] [default: false] 42 | --skipLibCheck Use skipLibCheck when loading typescript modules. [boolean] [default: false] 43 | --useTypeOfKeyword Use `typeOf` keyword (https://goo.gl/DC6sni) for functions. [boolean] [default: false] 44 | --out, -o The output file, defaults to using stdout 45 | --validationKeywords Provide additional validation keywords to include [array] [default: []] 46 | --include Further limit tsconfig to include only matching files [array] [default: []] 47 | --ignoreErrors Generate even if the program has errors. [boolean] [default: false] 48 | --excludePrivate Exclude private members from the schema [boolean] [default: false] 49 | --uniqueNames Use unique names for type symbols. [boolean] [default: false] 50 | --rejectDateType Rejects Date fields in type definitions. [boolean] [default: false] 51 | --id Set schema id. [string] [default: ""] 52 | --defaultNumberType Default number type. [choices: "number", "integer"] [default: "number"] 53 | --tsNodeRegister Use ts-node/register (needed for require typescript files). [boolean] [default: false] 54 | --constAsEnum Use enums with a single value when declaring constants. [boolean] [default: false] 55 | --experimentalDecorators Use experimentalDecorators when loading typescript modules. 56 | [boolean] [default: true] 57 | ``` 58 | 59 | ### Programmatic use 60 | 61 | ```ts 62 | import { resolve } from "path"; 63 | 64 | import * as TJS from "typescript-json-schema"; 65 | 66 | // optionally pass argument to schema generator 67 | const settings: TJS.PartialArgs = { 68 | required: true, 69 | }; 70 | 71 | // optionally pass ts compiler options 72 | const compilerOptions: TJS.CompilerOptions = { 73 | strictNullChecks: true, 74 | }; 75 | 76 | // optionally pass a base path 77 | const basePath = "./my-dir"; 78 | 79 | const program = TJS.getProgramFromFiles( 80 | [resolve("my-file.ts")], 81 | compilerOptions, 82 | basePath 83 | ); 84 | 85 | // We can either get the schema for one file and one type... 86 | const schema = TJS.generateSchema(program, "MyType", settings); 87 | 88 | // ... or a generator that lets us incrementally get more schemas 89 | 90 | const generator = TJS.buildGenerator(program, settings); 91 | 92 | // generator can be also reused to speed up generating the schema if usecase allows: 93 | const schemaWithReusedGenerator = TJS.generateSchema(program, "MyType", settings, [], generator); 94 | 95 | // all symbols 96 | const symbols = generator.getUserSymbols(); 97 | 98 | // Get symbols for different types from generator. 99 | generator.getSchemaForSymbol("MyType"); 100 | generator.getSchemaForSymbol("AnotherType"); 101 | ``` 102 | 103 | ```ts 104 | // In larger projects type names may not be unique, 105 | // while unique names may be enabled. 106 | const settings: TJS.PartialArgs = { 107 | uniqueNames: true, 108 | }; 109 | 110 | const generator = TJS.buildGenerator(program, settings); 111 | 112 | // A list of all types of a given name can then be retrieved. 113 | const symbolList = generator.getSymbols("MyType"); 114 | 115 | // Choose the appropriate type, and continue with the symbol's unique name. 116 | generator.getSchemaForSymbol(symbolList[1].name); 117 | 118 | // Also it is possible to get a list of all symbols. 119 | const fullSymbolList = generator.getSymbols(); 120 | ``` 121 | 122 | `getSymbols('')` and `getSymbols()` return an array of `SymbolRef`, which is of the following format: 123 | 124 | ```ts 125 | type SymbolRef = { 126 | name: string; 127 | typeName: string; 128 | fullyQualifiedName: string; 129 | symbol: ts.Symbol; 130 | }; 131 | ``` 132 | 133 | `getUserSymbols` and `getMainFileSymbols` return an array of `string`. 134 | 135 | ### Annotations 136 | 137 | The schema generator converts annotations to JSON schema properties. 138 | 139 | For example 140 | 141 | ```ts 142 | export interface Shape { 143 | /** 144 | * The size of the shape. 145 | * 146 | * @minimum 0 147 | * @TJS-type integer 148 | */ 149 | size: number; 150 | } 151 | ``` 152 | 153 | will be translated to 154 | 155 | ```json 156 | { 157 | "$ref": "#/definitions/Shape", 158 | "$schema": "http://json-schema.org/draft-07/schema#", 159 | "definitions": { 160 | "Shape": { 161 | "properties": { 162 | "size": { 163 | "description": "The size of the shape.", 164 | "minimum": 0, 165 | "type": "integer" 166 | } 167 | }, 168 | "type": "object" 169 | } 170 | } 171 | } 172 | ``` 173 | 174 | 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). 175 | 176 | You can also override the type of array items, either listing each field in its own annotation or one 177 | annotation with the full JSON of the spec (for special cases). This replaces the item types that would 178 | have been inferred from the TypeScript type of the array elements. 179 | 180 | Example: 181 | 182 | ```ts 183 | export interface ShapesData { 184 | /** 185 | * Specify individual fields in items. 186 | * 187 | * @items.type integer 188 | * @items.minimum 0 189 | */ 190 | sizes: number[]; 191 | 192 | /** 193 | * Or specify a JSON spec: 194 | * 195 | * @items {"type":"string","format":"email"} 196 | */ 197 | emails: string[]; 198 | } 199 | ``` 200 | 201 | Translation: 202 | 203 | ```json 204 | { 205 | "$ref": "#/definitions/ShapesData", 206 | "$schema": "http://json-schema.org/draft-07/schema#", 207 | "definitions": { 208 | "Shape": { 209 | "properties": { 210 | "sizes": { 211 | "description": "Specify individual fields in items.", 212 | "items": { 213 | "minimum": 0, 214 | "type": "integer" 215 | }, 216 | "type": "array" 217 | }, 218 | "emails": { 219 | "description": "Or specify a JSON spec:", 220 | "items": { 221 | "format": "email", 222 | "type": "string" 223 | }, 224 | "type": "array" 225 | } 226 | }, 227 | "type": "object" 228 | } 229 | } 230 | } 231 | ``` 232 | 233 | This same syntax can be used for `contains` and `additionalProperties`. 234 | 235 | ### `integer` type alias 236 | 237 | If you create a type alias `integer` for `number` it will be mapped to the `integer` type in the generated JSON schema. 238 | 239 | Example: 240 | 241 | ```typescript 242 | type integer = number; 243 | interface MyObject { 244 | n: integer; 245 | } 246 | ``` 247 | 248 | Note: this feature doesn't work for generic types & array types, it mainly works in very simple cases. 249 | 250 | ### `require` a variable from a file 251 | 252 | (for requiring typescript files is needed to set argument `tsNodeRegister` to true) 253 | 254 | When you want to import for example an object or an array into your property defined in annotation, you can use `require`. 255 | 256 | Example: 257 | 258 | ```ts 259 | export interface InnerData { 260 | age: number; 261 | name: string; 262 | free: boolean; 263 | } 264 | 265 | export interface UserData { 266 | /** 267 | * Specify required object 268 | * 269 | * @examples require("./example.ts").example 270 | */ 271 | data: InnerData; 272 | } 273 | ``` 274 | 275 | file `example.ts` 276 | 277 | ```ts 278 | export const example: InnerData[] = [{ 279 | age: 30, 280 | name: "Ben", 281 | free: false 282 | }] 283 | ``` 284 | 285 | Translation: 286 | 287 | ```json 288 | { 289 | "$schema": "http://json-schema.org/draft-07/schema#", 290 | "properties": { 291 | "data": { 292 | "description": "Specify required object", 293 | "examples": [ 294 | { 295 | "age": 30, 296 | "name": "Ben", 297 | "free": false 298 | } 299 | ], 300 | "type": "object", 301 | "properties": { 302 | "age": { "type": "number" }, 303 | "name": { "type": "string" }, 304 | "free": { "type": "boolean" } 305 | }, 306 | "required": ["age", "free", "name"] 307 | } 308 | }, 309 | "required": ["data"], 310 | "type": "object" 311 | } 312 | ``` 313 | 314 | 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'. 315 | 316 | Note: For `examples` a required variable must be an array. 317 | 318 | ## Background 319 | 320 | 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). 321 | 322 | ## Debugging 323 | 324 | `npm run debug -- test/programs/type-alias-single/main.ts --aliasRefs true MyString` 325 | 326 | And connect via the debugger protocol. 327 | -------------------------------------------------------------------------------- /bin/typescript-json-schema: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var app = require('../dist/typescript-json-schema-cli.js'); 3 | app.run(); 4 | -------------------------------------------------------------------------------- /example/invoice/line.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | /** 5 | * Represents the document sent to the customer for payment. 6 | */ 7 | interface Invoice { 8 | /** 9 | * Who will pay? 10 | * Not me! 11 | */ 12 | customer: string; 13 | 14 | /** 15 | * Invoice content 16 | * @minItems 1 17 | * @maxItems 50 18 | */ 19 | lines: InvoiceLine[]; 20 | 21 | dimension: Dimension; // Total dimension of the order 22 | 23 | blob: any; // Additional stuff 24 | } 25 | 26 | interface InvoiceLine { 27 | 28 | product: Product; 29 | 30 | /** 31 | * @minimum 0 32 | * @exclusiveMinimum true 33 | * @maximum 10 34 | * @exclusiveMaximum false 35 | * @multipleOf 2 36 | */ 37 | quantity: number; 38 | } 39 | -------------------------------------------------------------------------------- /example/misc/dimension.ts: -------------------------------------------------------------------------------- 1 | // Typescript-JSON-Schema can handle defaults in classes as illustrated below 2 | class Dimension { 3 | /** Width in cm */ 4 | width: number = 10; 5 | 6 | /** Height in cm */ 7 | height: number = 10; 8 | 9 | /** Length in cm */ 10 | length: number = 10; 11 | } 12 | -------------------------------------------------------------------------------- /example/product.ts: -------------------------------------------------------------------------------- 1 | /// 2 | 3 | interface Product { 4 | /** 5 | * Uniquely defines the product 6 | * @pattern [A-Z][a-z][0-9]_ 7 | */ 8 | name: string; 9 | 10 | /** How big it is */ 11 | dimension?: Dimension; 12 | 13 | /** Classification */ 14 | category: Category; 15 | } 16 | 17 | interface WeightedProduct extends Product { 18 | weight: number; 19 | } 20 | 21 | interface Category { 22 | /** Uniquely identifies the category */ 23 | name: string | number; 24 | 25 | /** Classification level from 1 to 5 (highest) 26 | * @type integer 27 | */ 28 | level: number; 29 | } 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-json-schema", 3 | "version": "0.65.1", 4 | "description": "typescript-json-schema generates JSON Schema files from your Typescript sources", 5 | "main": "dist/typescript-json-schema.js", 6 | "typings": "dist/typescript-json-schema.d.ts", 7 | "bin": { 8 | "typescript-json-schema": "./bin/typescript-json-schema" 9 | }, 10 | "author": "Yousef El-Dardiry and Dominik Moritz", 11 | "contributors": [ 12 | { 13 | "name": "Yousef El-Dardiry", 14 | "email": "yousef@tweetbeam.com", 15 | "url": "http://www.twitter.com/yousefed" 16 | }, 17 | { 18 | "name": "Dominik Moritz", 19 | "email": "domoritz@gmail.com", 20 | "url": "https://www.domoritz.de/" 21 | }, 22 | { 23 | "name": "Vladimir Krivosheev", 24 | "email": "develar@gmail.com" 25 | }, 26 | { 27 | "name": "Fabian Pirklbauer", 28 | "email": "hi@fabiandev.io" 29 | } 30 | ], 31 | "repository": { 32 | "type": "git", 33 | "url": "git@github.com:YousefED/typescript-json-schema.git" 34 | }, 35 | "license": "BSD-3-Clause", 36 | "keywords": [ 37 | "typescript", 38 | "json", 39 | "forms", 40 | "jsonschema", 41 | "schema" 42 | ], 43 | "dependencies": { 44 | "@types/json-schema": "^7.0.9", 45 | "@types/node": "^18.11.9", 46 | "glob": "^7.1.7", 47 | "path-equal": "^1.2.5", 48 | "safe-stable-stringify": "^2.2.0", 49 | "ts-node": "^10.9.1", 50 | "typescript": "~5.5.0", 51 | "yargs": "^17.1.1" 52 | }, 53 | "devDependencies": { 54 | "@types/chai": "^4.2.21", 55 | "@types/glob": "^7.1.4", 56 | "@types/mocha": "^9.0.0", 57 | "ajv": "^8.6.3", 58 | "ajv-formats": "^2.1.1", 59 | "chai": "^4.3.4", 60 | "mocha": "^9.1.3", 61 | "prettier": "^2.4.1", 62 | "source-map-support": "^0.5.20", 63 | "tslint": "^6.1.3" 64 | }, 65 | "scripts": { 66 | "prepare": "tsc && yarn docs", 67 | "test": "tsc && mocha -t 5000 --require source-map-support/register dist/test", 68 | "debug": "node --inspect=19248 --inspect-brk -r ts-node/register typescript-json-schema-cli.ts", 69 | "docs": "./update-docs.js", 70 | "run": "ts-node typescript-json-schema-cli.ts", 71 | "build": "tsc", 72 | "lint": "tslint --project tsconfig.json -c tslint.json --exclude '**/*.d.ts'", 73 | "style": "prettier --write *.js *.ts test/*.ts", 74 | "dev": "tsc -w", 75 | "test:dev": "mocha -t 5000 --watch --require source-map-support/register dist/test" 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /test/error.test.ts: -------------------------------------------------------------------------------- 1 | import { assert } from "chai"; 2 | import { exec, getDefaultArgs } from "../typescript-json-schema"; 3 | 4 | describe("error", () => { 5 | it("error-check", async () => { 6 | try { 7 | await exec("test/programs/dates/", "MyObject", getDefaultArgs()); 8 | assert.fail("Expected exec to fail"); 9 | } catch (err) { 10 | assert.instanceOf(err, Error); 11 | assert.equal(err.message, "No output definition. Probably caused by errors prior to this?"); 12 | } 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /test/out-option.test.ts: -------------------------------------------------------------------------------- 1 | import { assert } from "chai"; 2 | import { exec, getDefaultArgs } from "../typescript-json-schema"; 3 | 4 | describe("out option", () => { 5 | beforeEach( 6 | () => 7 | new Promise((resolve, reject) => { 8 | require("fs").rm("./dist/test/doesnotexist", { recursive: true, force: true }, (err: Error) => 9 | err ? reject(err) : resolve(null) 10 | ); 11 | }) 12 | ); 13 | it("should create parent directory when necessary", async () => { 14 | try { 15 | await exec("test/programs/interface-single/main.ts", "MyObject", { 16 | ...getDefaultArgs(), 17 | out: "./dist/test/doesnotexist/schema.json", 18 | }); 19 | } catch (err) { 20 | assert.fail(`Execution should not have failed: ${err.stack}`); 21 | } 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/programs/abstract-class/main.ts: -------------------------------------------------------------------------------- 1 | export abstract class AbstractBase { 2 | propA:number; 3 | propB:string; 4 | 5 | abstract doNotInclude(): void; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /test/programs/abstract-class/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "propA": { 6 | "type": "number" 7 | }, 8 | "propB": { 9 | "type": "string" 10 | } 11 | }, 12 | "required": [ 13 | "propA", 14 | "propB" 15 | ], 16 | "type": "object" 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/abstract-extends/main.ts: -------------------------------------------------------------------------------- 1 | import { AbstractBase } from "../abstract-class/main"; 2 | 3 | class MyObjectFromAbstract extends AbstractBase { 4 | doNotInclude(): void { } 5 | 6 | propB:string; 7 | propC:number; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/abstract-extends/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "propA": { 6 | "type": "number" 7 | }, 8 | "propB": { 9 | "type": "string" 10 | }, 11 | "propC": { 12 | "type": "number" 13 | } 14 | }, 15 | "required": [ 16 | "propA", 17 | "propB", 18 | "propC" 19 | ], 20 | "type": "object" 21 | } 22 | 23 | -------------------------------------------------------------------------------- /test/programs/annotation-default/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | /** 3 | * @default true 4 | */ 5 | varBoolean: boolean; 6 | /** 7 | * @default 123 8 | */ 9 | varInteger: number; 10 | /** 11 | * @default 3.21 12 | */ 13 | varFloat: number; 14 | /** 15 | * @default "foo" 16 | */ 17 | varString: string; 18 | /** 19 | * @default [true, false, true] 20 | */ 21 | varBooleanArray: boolean[]; 22 | /** 23 | * @default [1, 2, 3, 4, 5] 24 | */ 25 | varIntegerArray: number[]; 26 | /** 27 | * @default [1.23, 65.21, -123.40, 0, 1000000.0001] 28 | */ 29 | varFloatArray: number[]; 30 | /** 31 | * @default ["a", "b", "c", "..."] 32 | */ 33 | varStringArray: string[]; 34 | /** 35 | * @default [true, 123, 3.21, "foo"] 36 | */ 37 | varMixedArray: any[]; 38 | } 39 | -------------------------------------------------------------------------------- /test/programs/annotation-default/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "varBoolean": { 6 | "default": true, 7 | "type": "boolean" 8 | }, 9 | "varBooleanArray": { 10 | "default": [ 11 | true, 12 | false, 13 | true 14 | ], 15 | "items": { 16 | "type": "boolean" 17 | }, 18 | "type": "array" 19 | }, 20 | "varFloat": { 21 | "default": 3.21, 22 | "type": "number" 23 | }, 24 | "varFloatArray": { 25 | "default": [ 26 | 1.23, 27 | 65.21, 28 | -123.4, 29 | 0, 30 | 1000000.0001 31 | ], 32 | "items": { 33 | "type": "number" 34 | }, 35 | "type": "array" 36 | }, 37 | "varInteger": { 38 | "default": 123, 39 | "type": "number" 40 | }, 41 | "varIntegerArray": { 42 | "default": [ 43 | 1, 44 | 2, 45 | 3, 46 | 4, 47 | 5 48 | ], 49 | "items": { 50 | "type": "number" 51 | }, 52 | "type": "array" 53 | }, 54 | "varMixedArray": { 55 | "default": [ 56 | true, 57 | 123, 58 | 3.21, 59 | "foo" 60 | ], 61 | "items": { 62 | }, 63 | "type": "array" 64 | }, 65 | "varString": { 66 | "default": "foo", 67 | "type": "string" 68 | }, 69 | "varStringArray": { 70 | "default": [ 71 | "a", 72 | "b", 73 | "c", 74 | "..." 75 | ], 76 | "items": { 77 | "type": "string" 78 | }, 79 | "type": "array" 80 | } 81 | }, 82 | "required": [ 83 | "varBoolean", 84 | "varBooleanArray", 85 | "varFloat", 86 | "varFloatArray", 87 | "varInteger", 88 | "varIntegerArray", 89 | "varMixedArray", 90 | "varString", 91 | "varStringArray" 92 | ], 93 | "type": "object" 94 | } 95 | 96 | -------------------------------------------------------------------------------- /test/programs/annotation-id/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @$id filled# 3 | */ 4 | interface MySubObject { 5 | a: boolean; 6 | } 7 | 8 | interface MyObject { 9 | /** 10 | * @$id empty# 11 | */ 12 | empty; 13 | 14 | filled: MySubObject; 15 | } 16 | -------------------------------------------------------------------------------- /test/programs/annotation-id/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MySubObject": { 6 | "$id": "filled#", 7 | "type": "object", 8 | "additionalProperties": false, 9 | "properties": { 10 | "a": { "type": "boolean" } 11 | }, 12 | "required": ["a"] 13 | } 14 | }, 15 | "properties": { 16 | "empty": { 17 | "$id": "empty#" 18 | }, 19 | "filled": { 20 | "$ref": "#/definitions/MySubObject" 21 | } 22 | }, 23 | "required": [ 24 | "empty", 25 | "filled" 26 | ], 27 | "type": "object" 28 | } 29 | 30 | -------------------------------------------------------------------------------- /test/programs/annotation-items/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | /** 3 | * @items {"type":"integer"} 4 | */ 5 | a: number[]; 6 | 7 | /** 8 | * @items {"type":"integer", "minimum":0} 9 | */ 10 | b: number[]; 11 | 12 | /** 13 | * @items.type integer 14 | * @items.minimum 0 15 | */ 16 | c: number[]; 17 | 18 | /** 19 | * @items.type integer 20 | */ 21 | d: number[]; 22 | 23 | /** 24 | * @items {"type":"string", "format":"email"} 25 | */ 26 | emails: string[]; 27 | 28 | /** 29 | * @items.type string 30 | * @items.format email 31 | */ 32 | emails2: string[]; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /test/programs/annotation-items/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "a": { 6 | "items": { 7 | "type": "integer" 8 | }, 9 | "type": "array" 10 | }, 11 | "b": { 12 | "items": { 13 | "minimum": 0, 14 | "type": "integer" 15 | }, 16 | "type": "array" 17 | }, 18 | "c": { 19 | "items": { 20 | "minimum": 0, 21 | "type": "integer" 22 | }, 23 | "type": "array" 24 | }, 25 | "d": { 26 | "items": { 27 | "type": "integer" 28 | }, 29 | "type": "array" 30 | }, 31 | "emails": { 32 | "items": { 33 | "type": "string", 34 | "format": "email" 35 | }, 36 | "type": "array" 37 | }, 38 | "emails2": { 39 | "items": { 40 | "type": "string", 41 | "format": "email" 42 | }, 43 | "type": "array" 44 | } 45 | }, 46 | "required": [ 47 | "a", 48 | "b", 49 | "c", 50 | "d", 51 | "emails", 52 | "emails2" 53 | ], 54 | "type": "object" 55 | } 56 | -------------------------------------------------------------------------------- /test/programs/annotation-ref/main.ts: -------------------------------------------------------------------------------- 1 | interface MySubObject {} 2 | 3 | interface MyObject { 4 | /** 5 | * @$ref http://my-schema.org 6 | */ 7 | externalRef; 8 | 9 | /** 10 | * @$ref http://my-schema.org 11 | */ 12 | externalRefOverride: MySubObject; 13 | } -------------------------------------------------------------------------------- /test/programs/annotation-ref/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MySubObject": { 5 | "additionalProperties": false, 6 | "type": "object" 7 | } 8 | }, 9 | "additionalProperties": false, 10 | "properties": { 11 | "externalRef": { 12 | "$ref": "http://my-schema.org" 13 | }, 14 | "externalRefOverride": { 15 | "$ref": "http://my-schema.org" 16 | } 17 | }, 18 | "required": [ 19 | "externalRef", 20 | "externalRefOverride" 21 | ], 22 | "type": "object" 23 | } 24 | 25 | -------------------------------------------------------------------------------- /test/programs/annotation-required/examples.ts: -------------------------------------------------------------------------------- 1 | import { MyDefaultObject, MySubObject2 } from "./main"; 2 | 3 | export const mySubObject2Example: MySubObject2[] = [{ 4 | bool: true, 5 | string: "string", 6 | object: { prop: 1 } 7 | }]; 8 | 9 | const myDefaultExample: MyDefaultObject[] = [{ 10 | age: 30, 11 | name: "me", 12 | free: true 13 | }] 14 | 15 | export default myDefaultExample; -------------------------------------------------------------------------------- /test/programs/annotation-required/main.ts: -------------------------------------------------------------------------------- 1 | interface MySubObject { 2 | bool: boolean; 3 | string: string; 4 | object: object | null; 5 | /** 6 | * @examples require('./examples.ts').mySubObject2Example 7 | */ 8 | subObject?: MySubObject2; 9 | } 10 | 11 | export interface MySubObject2 { 12 | bool: boolean; 13 | string: string; 14 | object: object; 15 | } 16 | 17 | export interface MyDefaultObject { 18 | age: number; 19 | name: string; 20 | free?: boolean; 21 | } 22 | 23 | export interface MyObject { 24 | /** 25 | * @examples require(".").innerExample 26 | */ 27 | filled: MySubObject; 28 | /** 29 | * @examples require('./examples.ts') 30 | */ 31 | defaultObject: MyDefaultObject; 32 | } 33 | 34 | export const innerExample: MySubObject[] = [ 35 | { 36 | bool: true, 37 | string: "string", 38 | object: {} 39 | }, 40 | ]; 41 | -------------------------------------------------------------------------------- /test/programs/annotation-required/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyDefaultObject": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "age": { 9 | "type": "number" 10 | }, 11 | "free": { 12 | "type": "boolean" 13 | }, 14 | "name": { 15 | "type": "string" 16 | } 17 | }, 18 | "required": ["age", "name"], 19 | "type": "object" 20 | }, 21 | "MySubObject": { 22 | "additionalProperties": false, 23 | "properties": { 24 | "bool": { 25 | "type": "boolean" 26 | }, 27 | "object": { 28 | "additionalProperties": true, 29 | "properties": {}, 30 | "type": "object" 31 | }, 32 | "string": { 33 | "type": "string" 34 | }, 35 | "subObject": { 36 | "$ref": "#/definitions/MySubObject2", 37 | "examples": [ 38 | { 39 | "bool": true, 40 | "object": { 41 | "prop": 1 42 | }, 43 | "string": "string" 44 | } 45 | ] 46 | } 47 | }, 48 | "required": ["bool", "object", "string"], 49 | "type": "object" 50 | }, 51 | "MySubObject2": { 52 | "additionalProperties": false, 53 | "properties": { 54 | "bool": { 55 | "type": "boolean" 56 | }, 57 | "object": { 58 | "additionalProperties": true, 59 | "properties": {}, 60 | "type": "object" 61 | }, 62 | "string": { 63 | "type": "string" 64 | } 65 | }, 66 | "required": ["bool", "object", "string"], 67 | "type": "object" 68 | } 69 | }, 70 | "properties": { 71 | "defaultObject": { 72 | "$ref": "#/definitions/MyDefaultObject", 73 | "examples": [ 74 | { 75 | "age": 30, 76 | "free": true, 77 | "name": "me" 78 | } 79 | ] 80 | }, 81 | "filled": { 82 | "$ref": "#/definitions/MySubObject", 83 | "examples": [ 84 | { 85 | "bool": true, 86 | "object": {}, 87 | "string": "string" 88 | } 89 | ] 90 | } 91 | }, 92 | "required": ["defaultObject", "filled"], 93 | "type": "object" 94 | } 95 | -------------------------------------------------------------------------------- /test/programs/annotation-title/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @title filled# 3 | */ 4 | interface MySubObject { 5 | a: boolean; 6 | } 7 | 8 | interface AnotherSubObject { 9 | b: boolean; 10 | } 11 | 12 | interface MyObject { 13 | /** 14 | * @title empty# 15 | */ 16 | empty; 17 | /** 18 | * @title filled 19 | */ 20 | filled: MySubObject; 21 | nonTitled: AnotherSubObject; 22 | } 23 | -------------------------------------------------------------------------------- /test/programs/annotation-title/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "AnotherSubObject": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "b": { 9 | "type": "boolean" 10 | } 11 | }, 12 | "required": ["b"], 13 | "type": "object" 14 | }, 15 | "MySubObject": { 16 | "additionalProperties": false, 17 | "title": "filled#", 18 | "type": "object", 19 | "properties": { 20 | "a": { "type": "boolean" } 21 | }, 22 | "required": ["a"] 23 | } 24 | }, 25 | "properties": { 26 | "empty": { 27 | "title": "empty#" 28 | }, 29 | "filled": { 30 | "$ref": "#/definitions/MySubObject", 31 | "title": "filled" 32 | }, 33 | "nonTitled": { 34 | "$ref": "#/definitions/AnotherSubObject" 35 | } 36 | }, 37 | "required": ["empty", "filled", "nonTitled"], 38 | "type": "object" 39 | } 40 | -------------------------------------------------------------------------------- /test/programs/annotation-tjs/main.ts: -------------------------------------------------------------------------------- 1 | // All of these formats are defined in this specification: http://json-schema.org/latest/json-schema-validation.html#rfc.section.8.3 2 | 3 | interface MyRef {} 4 | 5 | interface MyObject { 6 | /** 7 | * @TJS-format date-time 8 | */ 9 | dateTime: string; 10 | 11 | /** 12 | * @TJS-format email 13 | */ 14 | email: string; 15 | 16 | /** 17 | * @TJS-format hostname 18 | */ 19 | hostname: string; 20 | 21 | /** 22 | * @TJS-format ipv4 23 | */ 24 | ipv4: string; 25 | 26 | /** 27 | * @TJS-format ipv6 28 | */ 29 | ipv6: string; 30 | 31 | /** 32 | * @TJS-format uri 33 | */ 34 | uri: string; 35 | 36 | /** 37 | * @TJS-format uri-reference 38 | */ 39 | uriReference: string; 40 | 41 | /** 42 | * @TJS-format uri-template 43 | */ 44 | uriTemplate: string; 45 | 46 | /** 47 | * @TJS-format json-pointer 48 | */ 49 | jsonPointer: string; 50 | 51 | /** 52 | * @TJS-pattern ^[a-zA-Z0-9]{4}-abc_123$ 53 | */ 54 | regexPattern: string; 55 | 56 | /** 57 | * @TJS-pattern ^[a-zA-Z0-9]{4}-abc_123$ 58 | */ 59 | regexPatternWithWhitespace: string; 60 | 61 | /** 62 | * @TJS-minimum 5 63 | */ 64 | oneCharacter: number; 65 | 66 | /** 67 | * @TJS-examples ["foo", 1] 68 | */ 69 | examples: string; 70 | 71 | /** 72 | * @TJS-hide 73 | */ 74 | booleanAnnotationDefaultValue: string; 75 | 76 | /** 77 | * @TJS-hide true 78 | */ 79 | booleanAnnotationWithTrue: string; 80 | 81 | /** 82 | * @TJS-hide false 83 | */ 84 | booleanAnnotationWithFalse: string; 85 | 86 | /** 87 | * @TJS-ignore 88 | */ 89 | complexWithRefIgnored: MyRef; 90 | } 91 | -------------------------------------------------------------------------------- /test/programs/annotation-tjs/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "dateTime": { 6 | "format": "date-time", 7 | "type": "string" 8 | }, 9 | "email": { 10 | "format": "email", 11 | "type": "string" 12 | }, 13 | "examples": { 14 | "examples": [ 15 | "foo", 16 | 1 17 | ], 18 | "type": "string" 19 | }, 20 | "hostname": { 21 | "format": "hostname", 22 | "type": "string" 23 | }, 24 | "ipv4": { 25 | "format": "ipv4", 26 | "type": "string" 27 | }, 28 | "ipv6": { 29 | "format": "ipv6", 30 | "type": "string" 31 | }, 32 | "jsonPointer": { 33 | "format": "json-pointer", 34 | "type": "string" 35 | }, 36 | "oneCharacter": { 37 | "minimum": 5, 38 | "type": "number" 39 | }, 40 | "regexPattern": { 41 | "pattern": "^[a-zA-Z0-9]{4}-abc_123$", 42 | "type": "string" 43 | }, 44 | "regexPatternWithWhitespace": { 45 | "pattern": "^[a-zA-Z0-9]{4}-abc_123$", 46 | "type": "string" 47 | }, 48 | "uri": { 49 | "format": "uri", 50 | "type": "string" 51 | }, 52 | "uriReference": { 53 | "format": "uri-reference", 54 | "type": "string" 55 | }, 56 | "uriTemplate": { 57 | "format": "uri-template", 58 | "type": "string" 59 | }, 60 | "booleanAnnotationDefaultValue": { 61 | "hide": true, 62 | "type": "string" 63 | }, 64 | "booleanAnnotationWithTrue": { 65 | "hide": true, 66 | "type": "string" 67 | }, 68 | "booleanAnnotationWithFalse": { 69 | "hide": false, 70 | "type": "string" 71 | } 72 | }, 73 | "required": [ 74 | "booleanAnnotationDefaultValue", 75 | "booleanAnnotationWithFalse", 76 | "booleanAnnotationWithTrue", 77 | "dateTime", 78 | "email", 79 | "examples", 80 | "hostname", 81 | "ipv4", 82 | "ipv6", 83 | "jsonPointer", 84 | "oneCharacter", 85 | "regexPattern", 86 | "regexPatternWithWhitespace", 87 | "uri", 88 | "uriReference", 89 | "uriTemplate" 90 | ], 91 | "type": "object" 92 | } 93 | 94 | -------------------------------------------------------------------------------- /test/programs/any-unknown/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | a: any; 3 | b: unknown; 4 | } -------------------------------------------------------------------------------- /test/programs/any-unknown/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "object", 3 | "additionalProperties": false, 4 | "properties": { 5 | "a": {}, 6 | "b": {} 7 | }, 8 | "required": [ 9 | "a", 10 | "b" 11 | ], 12 | "$schema": "http://json-schema.org/draft-07/schema#" 13 | } 14 | 15 | -------------------------------------------------------------------------------- /test/programs/argument-id/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | someProp: string; 3 | referenceType: ReferenceType; 4 | } 5 | 6 | interface ReferenceType { 7 | reference: true; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/argument-id/schema.MyObject.json: -------------------------------------------------------------------------------- 1 | { 2 | "$id": "someSchemaId", 3 | "$schema": "http://json-schema.org/draft-07/schema#", 4 | "additionalProperties": false, 5 | "definitions": { 6 | "ReferenceType": { 7 | "additionalProperties": false, 8 | "properties": { 9 | "reference": { 10 | "const": true, 11 | "type": "boolean" 12 | } 13 | }, 14 | "required": [ 15 | "reference" 16 | ], 17 | "type": "object" 18 | } 19 | }, 20 | "properties": { 21 | "referenceType": { 22 | "$ref": "someSchemaId#/definitions/ReferenceType" 23 | }, 24 | "someProp": { 25 | "type": "string" 26 | } 27 | }, 28 | "required": [ 29 | "referenceType", 30 | "someProp" 31 | ], 32 | "type": "object" 33 | } -------------------------------------------------------------------------------- /test/programs/array-and-description/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | /** 3 | * A name 4 | */ 5 | name?: string; 6 | description?: string; 7 | test: any[]; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/array-and-description/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "description": { 6 | "type": "string" 7 | }, 8 | "name": { 9 | "description": "A name", 10 | "type": "string" 11 | }, 12 | "test": { 13 | "items": { 14 | }, 15 | "type": "array" 16 | } 17 | }, 18 | "required": [ 19 | "test" 20 | ], 21 | "type": "object" 22 | } 23 | 24 | -------------------------------------------------------------------------------- /test/programs/array-empty/main.ts: -------------------------------------------------------------------------------- 1 | type MyEmptyArray = []; 2 | -------------------------------------------------------------------------------- /test/programs/array-empty/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "type": "array", 4 | "minItems": 0, 5 | "maxItems": 0 6 | } 7 | 8 | -------------------------------------------------------------------------------- /test/programs/array-readonly/main.ts: -------------------------------------------------------------------------------- 1 | export type MyReadOnlyArray = ReadonlyArray; 2 | -------------------------------------------------------------------------------- /test/programs/array-readonly/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "items": { 4 | "type": "number" 5 | }, 6 | "type": "array" 7 | } 8 | 9 | -------------------------------------------------------------------------------- /test/programs/array-types/main.ts: -------------------------------------------------------------------------------- 1 | type MyArray = Array; 2 | -------------------------------------------------------------------------------- /test/programs/array-types/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "items": { 4 | "type": [ 5 | "string", 6 | "number" 7 | ] 8 | }, 9 | "type": "array" 10 | } 11 | 12 | -------------------------------------------------------------------------------- /test/programs/builtin-names/main.ts: -------------------------------------------------------------------------------- 1 | declare namespace Ext { 2 | export class Array { 3 | } 4 | 5 | export class Foo { 6 | bar: Ext.Array; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/builtin-names/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Ext.Array": { 6 | "additionalProperties": false, 7 | "type": "object" 8 | } 9 | }, 10 | "properties": { 11 | "bar": { 12 | "$ref": "#/definitions/Ext.Array" 13 | } 14 | }, 15 | "required": [ 16 | "bar" 17 | ], 18 | "type": "object" 19 | } 20 | -------------------------------------------------------------------------------- /test/programs/class-extends/main.ts: -------------------------------------------------------------------------------- 1 | class Base { 2 | propA:number; 3 | } 4 | 5 | class MyObject extends Base { 6 | propB:number; 7 | } 8 | -------------------------------------------------------------------------------- /test/programs/class-extends/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "propA": { 6 | "type": "number" 7 | }, 8 | "propB": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "propA", 14 | "propB" 15 | ], 16 | "type": "object" 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/class-single/main.ts: -------------------------------------------------------------------------------- 1 | class MyObject { 2 | constructor() {} 3 | propA:number; 4 | propB:number; 5 | doNotInclude(): void {} 6 | } 7 | -------------------------------------------------------------------------------- /test/programs/class-single/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "propA": { 6 | "type": "number" 7 | }, 8 | "propB": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "propA", 14 | "propB" 15 | ], 16 | "type": "object" 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/comments-comment/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @$comment Object comment 3 | */ 4 | interface MyObject { 5 | /** 6 | * @$comment Property comment 7 | */ 8 | text: string; 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/comments-comment/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "$comment": "Object comment", 5 | "properties": { 6 | "text": { 7 | "$comment": "Property comment", 8 | "type": "string" 9 | } 10 | }, 11 | "required": [ 12 | "text" 13 | ], 14 | "type": "object" 15 | } 16 | 17 | -------------------------------------------------------------------------------- /test/programs/comments-from-lib/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Use this comment 3 | */ 4 | export type MyObject = Pick; 5 | 6 | /** 7 | * Not this comment though 8 | */ 9 | interface BigThing { 10 | prop1: string; 11 | prop2: string; 12 | }; 13 | -------------------------------------------------------------------------------- /test/programs/comments-from-lib/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "Use this comment", 3 | "additionalProperties": false, 4 | "type": "object", 5 | "properties": { 6 | "prop1": { 7 | "type": "string" 8 | } 9 | }, 10 | "required": [ 11 | "prop1" 12 | ], 13 | "$schema": "http://json-schema.org/draft-07/schema#" 14 | } 15 | -------------------------------------------------------------------------------- /test/programs/comments-imports/color.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Description of Color. 3 | * 4 | * @pattern ^[0-9a-f]{6}$ 5 | */ 6 | export type Color = string; 7 | -------------------------------------------------------------------------------- /test/programs/comments-imports/main.ts: -------------------------------------------------------------------------------- 1 | import { Color } from "./color"; 2 | import { Text } from "./text"; 3 | 4 | /** Description of MyObject */ 5 | export interface MyObject { 6 | /** Description of MyObject color property. */ 7 | color: Color; 8 | 9 | /** Description of MyObject text property. */ 10 | text: Text; 11 | } 12 | -------------------------------------------------------------------------------- /test/programs/comments-imports/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Color": { 6 | "description": "Description of Color.", 7 | "pattern": "^[0-9a-f]{6}$", 8 | "type": "string" 9 | }, 10 | "Text": { 11 | "additionalProperties": false, 12 | "description": "Description of Text interface.", 13 | "properties": { 14 | "color": { 15 | "$ref": "#/definitions/Color", 16 | "description": "Description of text color property." 17 | }, 18 | "text": { 19 | "description": "Description of text property.", 20 | "type": "string" 21 | } 22 | }, 23 | "required": [ 24 | "color", 25 | "text" 26 | ], 27 | "type": "object" 28 | } 29 | }, 30 | "description": "Description of MyObject", 31 | "properties": { 32 | "color": { 33 | "$ref": "#/definitions/Color", 34 | "description": "Description of MyObject color property." 35 | }, 36 | "text": { 37 | "$ref": "#/definitions/Text", 38 | "description": "Description of MyObject text property." 39 | } 40 | }, 41 | "required": [ 42 | "color", 43 | "text" 44 | ], 45 | "type": "object" 46 | } -------------------------------------------------------------------------------- /test/programs/comments-imports/text.ts: -------------------------------------------------------------------------------- 1 | import { Color } from "./color"; 2 | 3 | /** 4 | * Description of Text interface. 5 | */ 6 | export interface Text { 7 | /** Description of text property. */ 8 | text: string; 9 | 10 | /** Description of text color property. */ 11 | color: Color; 12 | } 13 | -------------------------------------------------------------------------------- /test/programs/comments-inline-tags/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This is MyOtherObject 3 | */ 4 | interface MyOtherObject { 5 | prop1: string; 6 | } 7 | 8 | /** 9 | * This is MyObject. It extends {@link MyOtherObject} and {@link SomeOtherObject}. 10 | */ 11 | interface MyObject extends MyOtherObject { 12 | prop2: string; 13 | } 14 | -------------------------------------------------------------------------------- /test/programs/comments-inline-tags/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "description": "This is MyObject. It extends {@link MyOtherObject} and {@link SomeOtherObject}.", 5 | "properties": { 6 | "prop1": { 7 | "type": "string" 8 | }, 9 | "prop2": { 10 | "type": "string" 11 | } 12 | }, 13 | "required": [ 14 | "prop1", 15 | "prop2" 16 | ], 17 | "type": "object" 18 | } 19 | -------------------------------------------------------------------------------- /test/programs/comments-override/main.ts: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Type-level description 4 | * @additionalProperties true 5 | */ 6 | export interface MySubObject { 7 | value: string; 8 | } 9 | 10 | export interface MyObject { 11 | list: MySubObject[]; 12 | 13 | sub1: MySubObject; 14 | 15 | /** 16 | * Property-level description 17 | * @additionalProperties false 18 | */ 19 | sub2: MySubObject; 20 | 21 | /** 22 | * Date property description 23 | */ 24 | date: Date; 25 | } 26 | -------------------------------------------------------------------------------- /test/programs/comments-override/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MySubObject": { 6 | "additionalProperties": true, 7 | "description": "Type-level description", 8 | "properties": { 9 | "value": { 10 | "type": "string" 11 | } 12 | }, 13 | "required": [ 14 | "value" 15 | ], 16 | "type": "object" 17 | } 18 | }, 19 | "properties": { 20 | "date": { 21 | "description": "Date property description", 22 | "format": "date-time", 23 | "type": "string" 24 | }, 25 | "list": { 26 | "items": { 27 | "$ref": "#/definitions/MySubObject" 28 | }, 29 | "type": "array" 30 | }, 31 | "sub1": { 32 | "$ref": "#/definitions/MySubObject" 33 | }, 34 | "sub2": { 35 | "$ref": "#/definitions/MySubObject", 36 | "description": "Property-level description" 37 | } 38 | }, 39 | "required": [ 40 | "date", 41 | "list", 42 | "sub1", 43 | "sub2" 44 | ], 45 | "type": "object" 46 | } 47 | 48 | -------------------------------------------------------------------------------- /test/programs/comments/main.ts: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Description of Vector3D, a type alias to a array of integers with length 3 4 | * If run without useTypeAliasRef, this comment should be ignored but 5 | * the other annotations should be inherited 6 | * @minItems 3 7 | * @maxItems 3 8 | */ 9 | type Vector3D = number[]; 10 | 11 | /** 12 | * Description of MyObject, a top level object, 13 | * which also has a comment that spans 14 | * multiple lines 15 | * 16 | * @additionalProperties false 17 | * @unsupportedAnnotationThatShouldBeIgnored 18 | */ 19 | interface MyObject { 20 | 21 | /** 22 | * Description of opacity, a field with min/max values 23 | * @minimum 0 24 | * @maximum 100 25 | */ 26 | opacity: number; 27 | 28 | /** 29 | * Description of field position, of aliased type Vector3D, which should inherit its annotations 30 | */ 31 | position: Vector3D; 32 | 33 | /** 34 | * Description of rotation, a field with an anonymous type 35 | */ 36 | rotation: { 37 | /** 38 | * Description of the value yaw inside an anonymous type, with min/max annotations 39 | * @minimum -90 40 | * @maximum 90 41 | */ 42 | yaw: number; 43 | }; 44 | } 45 | -------------------------------------------------------------------------------- /test/programs/comments/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "description": "Description of MyObject, a top level object,\nwhich also has a comment that spans\nmultiple lines", 5 | "properties": { 6 | "opacity": { 7 | "description": "Description of opacity, a field with min/max values", 8 | "maximum": 100, 9 | "minimum": 0, 10 | "type": "number" 11 | }, 12 | "position": { 13 | "description": "Description of field position, of aliased type Vector3D, which should inherit its annotations", 14 | "items": { 15 | "type": "number" 16 | }, 17 | "maxItems": 3, 18 | "minItems": 3, 19 | "type": "array" 20 | }, 21 | "rotation": { 22 | "additionalProperties": false, 23 | "description": "Description of rotation, a field with an anonymous type", 24 | "properties": { 25 | "yaw": { 26 | "description": "Description of the value yaw inside an anonymous type, with min/max annotations", 27 | "maximum": 90, 28 | "minimum": -90, 29 | "type": "number" 30 | } 31 | }, 32 | "required": [ 33 | "yaw" 34 | ], 35 | "type": "object" 36 | } 37 | }, 38 | "required": [ 39 | "opacity", 40 | "position", 41 | "rotation" 42 | ], 43 | "type": "object" 44 | } 45 | 46 | -------------------------------------------------------------------------------- /test/programs/const-as-enum/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | reference: true; 3 | } 4 | -------------------------------------------------------------------------------- /test/programs/const-as-enum/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "reference": { 6 | "enum": [ 7 | true 8 | ], 9 | "type": "boolean" 10 | } 11 | }, 12 | "required": [ 13 | "reference" 14 | ], 15 | "type": "object" 16 | } 17 | -------------------------------------------------------------------------------- /test/programs/const-keyword/main.ts: -------------------------------------------------------------------------------- 1 | const fn = (value: T) => 2 | ({ value }); 3 | 4 | export type Object = ReturnType>; 5 | -------------------------------------------------------------------------------- /test/programs/const-keyword/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "value": { 6 | "const": "value", 7 | "type": "string" 8 | } 9 | }, 10 | "required": ["value"], 11 | "type": "object" 12 | } 13 | 14 | -------------------------------------------------------------------------------- /test/programs/custom-dates/main.ts: -------------------------------------------------------------------------------- 1 | namespace foo { 2 | export interface Date { 3 | day?: number; 4 | month?: number; 5 | year?: number; 6 | } 7 | 8 | export interface Bar { 9 | date: Date; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/programs/custom-dates/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "foo.Date": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "day": { 9 | "type": "number" 10 | }, 11 | "month": { 12 | "type": "number" 13 | }, 14 | "year": { 15 | "type": "number" 16 | } 17 | }, 18 | "type": "object" 19 | } 20 | }, 21 | "properties": { 22 | "date": { 23 | "$ref": "#/definitions/foo.Date" 24 | } 25 | }, 26 | "required": [ 27 | "date" 28 | ], 29 | "type": "object" 30 | } -------------------------------------------------------------------------------- /test/programs/dates/main.ts: -------------------------------------------------------------------------------- 1 | type DateAlias = Date; 2 | 3 | interface MyObject { 4 | var1: Date; 5 | var2: DateAlias; 6 | /** 7 | * @format date 8 | */ 9 | var3: Date; 10 | /** 11 | * @format date 12 | */ 13 | var4: DateAlias; 14 | } 15 | -------------------------------------------------------------------------------- /test/programs/dates/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "var1": { 6 | "format": "date-time", 7 | "type": "string" 8 | }, 9 | "var2": { 10 | "format": "date-time", 11 | "type": "string" 12 | }, 13 | "var3": { 14 | "format": "date", 15 | "type": "string" 16 | }, 17 | "var4": { 18 | "format": "date", 19 | "type": "string" 20 | } 21 | }, 22 | "required": [ 23 | "var1", 24 | "var2", 25 | "var3", 26 | "var4" 27 | ], 28 | "type": "object" 29 | } 30 | 31 | -------------------------------------------------------------------------------- /test/programs/default-properties/main.ts: -------------------------------------------------------------------------------- 1 | type Foo = "a" | "b" | "c" | boolean | number; 2 | 3 | class MyObject { 4 | varBoolean: Foo = false; 5 | varInteger: Foo = 123; 6 | varString: Foo = "123"; 7 | } 8 | -------------------------------------------------------------------------------- /test/programs/default-properties/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Foo": { 6 | "anyOf": [ 7 | { 8 | "enum": [ 9 | "a", 10 | "b", 11 | "c", 12 | false, 13 | true 14 | ] 15 | }, 16 | { 17 | "type": "number" 18 | } 19 | ] 20 | } 21 | }, 22 | "properties": { 23 | "varBoolean": { 24 | "$ref": "#/definitions/Foo", 25 | "default": false 26 | }, 27 | "varInteger": { 28 | "$ref": "#/definitions/Foo", 29 | "default": 123 30 | }, 31 | "varString": { 32 | "$ref": "#/definitions/Foo", 33 | "default": "123" 34 | } 35 | }, 36 | "required": [ 37 | "varBoolean", 38 | "varInteger", 39 | "varString" 40 | ], 41 | "type": "object" 42 | } 43 | -------------------------------------------------------------------------------- /test/programs/enums-compiled-compute/main.ts: -------------------------------------------------------------------------------- 1 | enum Enum { 2 | X = 1 << 1, 3 | Y = 1 << 2, 4 | Z = X | Y, 5 | A = 1, 6 | } 7 | -------------------------------------------------------------------------------- /test/programs/enums-compiled-compute/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "enum": [ 4 | 2, 5 | 4, 6 | 6, 7 | 1 8 | ], 9 | "type": "number" 10 | } 11 | 12 | -------------------------------------------------------------------------------- /test/programs/enums-mixed/main.ts: -------------------------------------------------------------------------------- 1 | enum Enum { 2 | A, // = 0 3 | B = 1, 4 | C = true as any, 5 | D = "str" as any, 6 | E = null 7 | } 8 | 9 | interface MyObject { 10 | foo: Enum; 11 | } 12 | -------------------------------------------------------------------------------- /test/programs/enums-mixed/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Enum": { 6 | "enum": [ 7 | 0, 8 | 1, 9 | true, 10 | "str", 11 | null 12 | ], 13 | "type": [ 14 | "number", 15 | "boolean", 16 | "string", 17 | "null" 18 | ] 19 | } 20 | }, 21 | "properties": { 22 | "foo": { 23 | "$ref": "#/definitions/Enum" 24 | } 25 | }, 26 | "required": [ 27 | "foo" 28 | ], 29 | "type": "object" 30 | } 31 | 32 | -------------------------------------------------------------------------------- /test/programs/enums-number-initialized/main.ts: -------------------------------------------------------------------------------- 1 | enum Enum { 2 | X = 10, 3 | Y, 4 | Z, 5 | A = 1, 6 | } 7 | -------------------------------------------------------------------------------- /test/programs/enums-number-initialized/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "enum": [ 4 | 10, 5 | 11, 6 | 12, 7 | 1 8 | ], 9 | "type": "number" 10 | } 11 | 12 | -------------------------------------------------------------------------------- /test/programs/enums-number/main.ts: -------------------------------------------------------------------------------- 1 | enum Enum { 2 | X = 1, 3 | Y = 2 4 | } 5 | 6 | interface MyObject { 7 | foo: Enum; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/enums-number/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Enum": { 6 | "enum": [ 7 | 1, 8 | 2 9 | ], 10 | "type": "number" 11 | } 12 | }, 13 | "properties": { 14 | "foo": { 15 | "$ref": "#/definitions/Enum" 16 | } 17 | }, 18 | "required": [ 19 | "foo" 20 | ], 21 | "type": "object" 22 | } 23 | 24 | -------------------------------------------------------------------------------- /test/programs/enums-string/main.ts: -------------------------------------------------------------------------------- 1 | enum Enum { 2 | X = "x" as any, 3 | Y = "y" as any, 4 | Z = "123" as any 5 | } 6 | 7 | interface MyObject { 8 | foo: Enum; 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/enums-string/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Enum": { 6 | "enum": [ 7 | "x", 8 | "y", 9 | "123" 10 | ], 11 | "type": "string" 12 | } 13 | }, 14 | "properties": { 15 | "foo": { 16 | "$ref": "#/definitions/Enum" 17 | } 18 | }, 19 | "required": [ 20 | "foo" 21 | ], 22 | "type": "object" 23 | } 24 | 25 | -------------------------------------------------------------------------------- /test/programs/enums-value-in-interface/main.ts: -------------------------------------------------------------------------------- 1 | export enum A { 2 | B, 3 | C, 4 | D, 5 | }; 6 | 7 | export interface MyObject { 8 | code: A.B; 9 | }; 10 | -------------------------------------------------------------------------------- /test/programs/enums-value-in-interface/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "A.B": { 6 | "const": 0, 7 | "type": "number" 8 | } 9 | }, 10 | "properties": { 11 | "code": { 12 | "$ref": "#/definitions/A.B" 13 | } 14 | }, 15 | "required": [ 16 | "code" 17 | ], 18 | "type": "object" 19 | } 20 | 21 | -------------------------------------------------------------------------------- /test/programs/extra-properties/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | required: string; 3 | optional?: number; 4 | [name: string]: string|number; 5 | } 6 | -------------------------------------------------------------------------------- /test/programs/extra-properties/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": { 4 | "type": [ 5 | "string", 6 | "number" 7 | ] 8 | }, 9 | "properties": { 10 | "optional": { 11 | "type": "number" 12 | }, 13 | "required": { 14 | "type": "string" 15 | } 16 | }, 17 | "required": [ 18 | "required" 19 | ], 20 | "type": "object" 21 | } 22 | 23 | -------------------------------------------------------------------------------- /test/programs/force-type-imported/main.ts: -------------------------------------------------------------------------------- 1 | import { Widget } from "./widget"; 2 | 3 | export interface MyObject { 4 | name: string; 5 | 6 | mainWidget: Widget; 7 | otherWidgets: Widget[]; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/force-type-imported/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Widget": { 6 | "type": "number" 7 | } 8 | }, 9 | "properties": { 10 | "mainWidget": { 11 | "$ref": "#/definitions/Widget" 12 | }, 13 | "name": { 14 | "type": "string" 15 | }, 16 | "otherWidgets": { 17 | "items": { 18 | "$ref": "#/definitions/Widget" 19 | }, 20 | "type": "array" 21 | } 22 | }, 23 | "required": [ 24 | "mainWidget", 25 | "name", 26 | "otherWidgets" 27 | ], 28 | "type": "object" 29 | } 30 | 31 | -------------------------------------------------------------------------------- /test/programs/force-type-imported/widget.ts: -------------------------------------------------------------------------------- 1 | /** @TJS-type number */ 2 | export class Widget {} 3 | -------------------------------------------------------------------------------- /test/programs/force-type/main.ts: -------------------------------------------------------------------------------- 1 | /** @TJS-type number */ 2 | export class Widget {} 3 | 4 | export interface MyObject { 5 | name: string; 6 | 7 | mainWidget: Widget; 8 | otherWidgets: Widget[]; 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/force-type/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Widget": { 6 | "type": "number" 7 | } 8 | }, 9 | "properties": { 10 | "mainWidget": { 11 | "$ref": "#/definitions/Widget" 12 | }, 13 | "name": { 14 | "type": "string" 15 | }, 16 | "otherWidgets": { 17 | "items": { 18 | "$ref": "#/definitions/Widget" 19 | }, 20 | "type": "array" 21 | } 22 | }, 23 | "required": [ 24 | "mainWidget", 25 | "name", 26 | "otherWidgets" 27 | ], 28 | "type": "object" 29 | } 30 | 31 | -------------------------------------------------------------------------------- /test/programs/generate-all-types/main.ts: -------------------------------------------------------------------------------- 1 | 2 | 3 | interface MyInterface { 4 | 5 | } 6 | 7 | class MyObject { 8 | 9 | } 10 | 11 | enum MyEnum { 12 | Value = 0 13 | } 14 | -------------------------------------------------------------------------------- /test/programs/generate-all-types/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyEnum": { 5 | "const": 0, 6 | "type": "number" 7 | }, 8 | "MyInterface": { 9 | "additionalProperties": false, 10 | "type": "object" 11 | }, 12 | "MyObject": { 13 | "additionalProperties": false, 14 | "type": "object" 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/generic-anonymous/main.ts: -------------------------------------------------------------------------------- 1 | interface MyGeneric { 2 | a: A; 3 | b: B; 4 | } 5 | 6 | export interface MyObject { 7 | value1: MyGeneric; 8 | value2: MyGeneric; 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/generic-anonymous/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyGeneric": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "a": { 9 | "type": "number" 10 | }, 11 | "b": { 12 | "type": "string" 13 | } 14 | }, 15 | "required": [ 16 | "a", 17 | "b" 18 | ], 19 | "type": "object" 20 | }, 21 | "MyGeneric": { 22 | "additionalProperties": false, 23 | "properties": { 24 | "a": { 25 | "type": "string" 26 | }, 27 | "b": { 28 | "type": "number" 29 | } 30 | }, 31 | "required": [ 32 | "a", 33 | "b" 34 | ], 35 | "type": "object" 36 | } 37 | }, 38 | "properties": { 39 | "value1": { 40 | "$ref": "#/definitions/MyGeneric" 41 | }, 42 | "value2": { 43 | "$ref": "#/definitions/MyGeneric" 44 | } 45 | }, 46 | "required": [ 47 | "value1", 48 | "value2" 49 | ], 50 | "type": "object" 51 | } 52 | 53 | -------------------------------------------------------------------------------- /test/programs/generic-arrays/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | numberArray: Array; 3 | stringArray: ReadonlyArray; 4 | } 5 | -------------------------------------------------------------------------------- /test/programs/generic-arrays/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "numberArray": { 6 | "items": { 7 | "type": "number" 8 | }, 9 | "type": "array" 10 | }, 11 | "stringArray": { 12 | "items": { 13 | "type": "string" 14 | }, 15 | "type": "array" 16 | } 17 | }, 18 | "required": [ 19 | "numberArray", 20 | "stringArray" 21 | ], 22 | "type": "object" 23 | } 24 | 25 | -------------------------------------------------------------------------------- /test/programs/generic-hell/main.ts: -------------------------------------------------------------------------------- 1 | export interface GenericA { 2 | a: A; 3 | } 4 | export interface B { 5 | b: number; 6 | } 7 | export interface GenericC { 8 | c: C; 9 | } 10 | 11 | export type SomeAlias = SomeGeneric; 12 | export interface SomeGeneric { 13 | a: A; 14 | b: B; 15 | c: GenericA; 16 | d: GenericC; 17 | } 18 | 19 | export interface MyObject extends GenericC>>>, B { 20 | someGeneric: SomeGeneric<1, 2>; 21 | someAlias: SomeAlias<"alias">; 22 | } 23 | -------------------------------------------------------------------------------- /test/programs/generic-hell/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "GenericA<\"alias\">": { 5 | "properties": { 6 | "a": { 7 | "enum": [ 8 | "alias" 9 | ], 10 | "type": "string" 11 | } 12 | }, 13 | "required": [ 14 | "a" 15 | ], 16 | "type": "object" 17 | }, 18 | "GenericA<1>": { 19 | "properties": { 20 | "a": { 21 | "enum": [ 22 | 1 23 | ], 24 | "type": "number" 25 | } 26 | }, 27 | "required": [ 28 | "a" 29 | ], 30 | "type": "object" 31 | }, 32 | "GenericA": { 33 | "properties": { 34 | "a": { 35 | "type": "string" 36 | } 37 | }, 38 | "required": [ 39 | "a" 40 | ], 41 | "type": "object" 42 | }, 43 | "GenericC<\"alias\">": { 44 | "properties": { 45 | "c": { 46 | "enum": [ 47 | "alias" 48 | ], 49 | "type": "string" 50 | } 51 | }, 52 | "required": [ 53 | "c" 54 | ], 55 | "type": "object" 56 | }, 57 | "GenericC<2>": { 58 | "properties": { 59 | "c": { 60 | "enum": [ 61 | 2 62 | ], 63 | "type": "number" 64 | } 65 | }, 66 | "required": [ 67 | "c" 68 | ], 69 | "type": "object" 70 | }, 71 | "GenericC>": { 72 | "properties": { 73 | "c": { 74 | "$ref": "#/definitions/GenericA" 75 | } 76 | }, 77 | "required": [ 78 | "c" 79 | ], 80 | "type": "object" 81 | }, 82 | "GenericC>>": { 83 | "properties": { 84 | "c": { 85 | "$ref": "#/definitions/GenericC>" 86 | } 87 | }, 88 | "required": [ 89 | "c" 90 | ], 91 | "type": "object" 92 | }, 93 | "SomeGeneric<\"alias\",\"alias\">": { 94 | "properties": { 95 | "a": { 96 | "enum": [ 97 | "alias" 98 | ], 99 | "type": "string" 100 | }, 101 | "b": { 102 | "enum": [ 103 | "alias" 104 | ], 105 | "type": "string" 106 | }, 107 | "c": { 108 | "$ref": "#/definitions/GenericA<\"alias\">" 109 | }, 110 | "d": { 111 | "$ref": "#/definitions/GenericC<\"alias\">" 112 | } 113 | }, 114 | "required": [ 115 | "a", 116 | "b", 117 | "c", 118 | "d" 119 | ], 120 | "type": "object" 121 | }, 122 | "SomeGeneric<1,2>": { 123 | "properties": { 124 | "a": { 125 | "enum": [ 126 | 1 127 | ], 128 | "type": "number" 129 | }, 130 | "b": { 131 | "enum": [ 132 | 2 133 | ], 134 | "type": "number" 135 | }, 136 | "c": { 137 | "$ref": "#/definitions/GenericA<1>" 138 | }, 139 | "d": { 140 | "$ref": "#/definitions/GenericC<2>" 141 | } 142 | }, 143 | "required": [ 144 | "a", 145 | "b", 146 | "c", 147 | "d" 148 | ], 149 | "type": "object" 150 | } 151 | }, 152 | "properties": { 153 | "b": { 154 | "type": "number" 155 | }, 156 | "c": { 157 | "$ref": "#/definitions/GenericC>>" 158 | }, 159 | "someAlias": { 160 | "$ref": "#/definitions/SomeGeneric<\"alias\",\"alias\">" 161 | }, 162 | "someGeneric": { 163 | "$ref": "#/definitions/SomeGeneric<1,2>" 164 | } 165 | }, 166 | "required": [ 167 | "b", 168 | "c", 169 | "someAlias", 170 | "someGeneric" 171 | ], 172 | "type": "object" 173 | } 174 | 175 | -------------------------------------------------------------------------------- /test/programs/generic-multiargs/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyGeneric { 2 | a: A; 3 | b: B; 4 | } 5 | 6 | export interface MyObject { 7 | value1: MyGeneric; 8 | value2: MyGeneric; 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/generic-multiargs/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyGeneric": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "a": { 9 | "type": "number" 10 | }, 11 | "b": { 12 | "type": "string" 13 | } 14 | }, 15 | "required": [ 16 | "a", 17 | "b" 18 | ], 19 | "type": "object" 20 | }, 21 | "MyGeneric": { 22 | "additionalProperties": false, 23 | "properties": { 24 | "a": { 25 | "type": "string" 26 | }, 27 | "b": { 28 | "type": "number" 29 | } 30 | }, 31 | "required": [ 32 | "a", 33 | "b" 34 | ], 35 | "type": "object" 36 | } 37 | }, 38 | "properties": { 39 | "value1": { 40 | "$ref": "#/definitions/MyGeneric" 41 | }, 42 | "value2": { 43 | "$ref": "#/definitions/MyGeneric" 44 | } 45 | }, 46 | "required": [ 47 | "value1", 48 | "value2" 49 | ], 50 | "type": "object" 51 | } 52 | 53 | -------------------------------------------------------------------------------- /test/programs/generic-multiple/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyGeneric { 2 | field: T; 3 | } 4 | 5 | export interface MyObject { 6 | value1: MyGeneric; 7 | value2: MyGeneric; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/generic-multiple/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyGeneric": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "field": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "field" 14 | ], 15 | "type": "object" 16 | }, 17 | "MyGeneric": { 18 | "additionalProperties": false, 19 | "properties": { 20 | "field": { 21 | "type": "string" 22 | } 23 | }, 24 | "required": [ 25 | "field" 26 | ], 27 | "type": "object" 28 | } 29 | }, 30 | "properties": { 31 | "value1": { 32 | "$ref": "#/definitions/MyGeneric" 33 | }, 34 | "value2": { 35 | "$ref": "#/definitions/MyGeneric" 36 | } 37 | }, 38 | "required": [ 39 | "value1", 40 | "value2" 41 | ], 42 | "type": "object" 43 | } 44 | 45 | -------------------------------------------------------------------------------- /test/programs/generic-recursive/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyGeneric { 2 | field: MyGeneric; 3 | } 4 | 5 | export interface MyObject { 6 | value: MyGeneric; 7 | } 8 | -------------------------------------------------------------------------------- /test/programs/generic-recursive/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$ref": "#/definitions/MyObject", 3 | "$schema": "http://json-schema.org/draft-07/schema#", 4 | "definitions": { 5 | "MyGeneric": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "field": { 9 | "$ref": "#/definitions/MyGeneric" 10 | } 11 | }, 12 | "required": [ 13 | "field" 14 | ], 15 | "type": "object" 16 | }, 17 | "MyGeneric": { 18 | "additionalProperties": false, 19 | "properties": { 20 | "field": { 21 | "$ref": "#/definitions/MyGeneric" 22 | } 23 | }, 24 | "required": [ 25 | "field" 26 | ], 27 | "type": "object" 28 | }, 29 | "MyObject": { 30 | "additionalProperties": false, 31 | "properties": { 32 | "value": { 33 | "$ref": "#/definitions/MyGeneric" 34 | } 35 | }, 36 | "required": [ 37 | "value" 38 | ], 39 | "type": "object" 40 | } 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /test/programs/generic-simple/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyGeneric { 2 | field: T; 3 | } 4 | 5 | export interface MyObject { 6 | value: MyGeneric; 7 | } 8 | -------------------------------------------------------------------------------- /test/programs/generic-simple/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyGeneric": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "field": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "field" 14 | ], 15 | "type": "object" 16 | } 17 | }, 18 | "properties": { 19 | "value": { 20 | "$ref": "#/definitions/MyGeneric" 21 | } 22 | }, 23 | "required": [ 24 | "value" 25 | ], 26 | "type": "object" 27 | } 28 | 29 | -------------------------------------------------------------------------------- /test/programs/ignored-required/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | /** 3 | * @ignore 4 | */ 5 | ignored: boolean; 6 | 7 | /** 8 | * @ignore 9 | */ 10 | ignoredOptional?: boolean; 11 | 12 | required: boolean; 13 | optional?: boolean; 14 | } 15 | -------------------------------------------------------------------------------- /test/programs/ignored-required/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "required": { 6 | "type": "boolean" 7 | }, 8 | "optional": { 9 | "type": "boolean" 10 | } 11 | }, 12 | "required": [ 13 | "required" 14 | ], 15 | "type": "object" 16 | } 17 | -------------------------------------------------------------------------------- /test/programs/imports/main.ts: -------------------------------------------------------------------------------- 1 | 2 | // This file imports "MyInterface" from the other 2 files 3 | // while also declaring a MyInterface type 4 | 5 | import { MyInterface as module1_MyInterface } from "./module1"; 6 | import * as module2 from "./module2"; 7 | 8 | class MyInterface { 9 | fieldInMain: number; 10 | } 11 | 12 | class MyObject { 13 | a: MyInterface; 14 | b: module1_MyInterface; 15 | c: module2.MyInterface; 16 | } 17 | -------------------------------------------------------------------------------- /test/programs/imports/module1.ts: -------------------------------------------------------------------------------- 1 | 2 | export class MyInterface { 3 | fieldInModule1: string; 4 | } 5 | 6 | -------------------------------------------------------------------------------- /test/programs/imports/module2.ts: -------------------------------------------------------------------------------- 1 | 2 | export class MyInterface { 3 | fieldInModule2: number; 4 | } 5 | 6 | -------------------------------------------------------------------------------- /test/programs/imports/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyInterface": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "fieldInMain": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "fieldInMain" 14 | ], 15 | "type": "object" 16 | }, 17 | "MyInterface_1": { 18 | "additionalProperties": false, 19 | "properties": { 20 | "fieldInModule1": { 21 | "type": "string" 22 | } 23 | }, 24 | "required": [ 25 | "fieldInModule1" 26 | ], 27 | "type": "object" 28 | }, 29 | "MyInterface_2": { 30 | "additionalProperties": false, 31 | "properties": { 32 | "fieldInModule2": { 33 | "type": "number" 34 | } 35 | }, 36 | "required": [ 37 | "fieldInModule2" 38 | ], 39 | "type": "object" 40 | } 41 | }, 42 | "properties": { 43 | "a": { 44 | "$ref": "#/definitions/MyInterface" 45 | }, 46 | "b": { 47 | "$ref": "#/definitions/MyInterface_1" 48 | }, 49 | "c": { 50 | "$ref": "#/definitions/MyInterface_2" 51 | } 52 | }, 53 | "required": [ 54 | "a", 55 | "b", 56 | "c" 57 | ], 58 | "type": "object" 59 | } 60 | 61 | -------------------------------------------------------------------------------- /test/programs/interface-extends/main.ts: -------------------------------------------------------------------------------- 1 | interface Base { 2 | propA: number; 3 | } 4 | 5 | export interface MyObject extends Base { 6 | propB: number; 7 | } 8 | -------------------------------------------------------------------------------- /test/programs/interface-extends/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "propA": { 6 | "type": "number" 7 | }, 8 | "propB": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "propA", 14 | "propB" 15 | ], 16 | "type": "object" 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/interface-extra-props/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | required: string; 3 | optional?: number; 4 | [name: string]: string|number; 5 | } 6 | -------------------------------------------------------------------------------- /test/programs/interface-extra-props/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyObject": { 5 | "type": "object", 6 | "properties": { 7 | "required": { 8 | "type": "string" 9 | }, 10 | "optional": { 11 | "type": "number" 12 | } 13 | }, 14 | "required": [ 15 | "required" 16 | ], 17 | "additionalProperties": { 18 | "type": [ 19 | "string", 20 | "number" 21 | ] 22 | } 23 | } 24 | }, 25 | "$ref": "#/definitions/MyObject" 26 | } 27 | -------------------------------------------------------------------------------- /test/programs/interface-multi/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | subA: MySubObject; 3 | subB: MySubObject; 4 | } 5 | interface MySubObject { 6 | propA: number; 7 | propB: number; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/interface-multi/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MySubObject": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "propA": { 9 | "type": "number" 10 | }, 11 | "propB": { 12 | "type": "number" 13 | } 14 | }, 15 | "required": [ 16 | "propA", 17 | "propB" 18 | ], 19 | "type": "object" 20 | } 21 | }, 22 | "properties": { 23 | "subA": { 24 | "$ref": "#/definitions/MySubObject" 25 | }, 26 | "subB": { 27 | "$ref": "#/definitions/MySubObject" 28 | } 29 | }, 30 | "required": [ 31 | "subA", 32 | "subB" 33 | ], 34 | "type": "object" 35 | } 36 | 37 | -------------------------------------------------------------------------------- /test/programs/interface-recursion/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | propA: number; 3 | propB: MyObject; 4 | } 5 | -------------------------------------------------------------------------------- /test/programs/interface-recursion/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$ref": "#/definitions/MyObject", 3 | "$schema": "http://json-schema.org/draft-07/schema#", 4 | "definitions": { 5 | "MyObject": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "propA": { 9 | "type": "number" 10 | }, 11 | "propB": { 12 | "$ref": "#/definitions/MyObject" 13 | } 14 | }, 15 | "required": [ 16 | "propA", 17 | "propB" 18 | ], 19 | "type": "object" 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /test/programs/interface-single/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | propA: number; 3 | propB: number; 4 | } 5 | -------------------------------------------------------------------------------- /test/programs/interface-single/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "propA": { 6 | "type": "number" 7 | }, 8 | "propB": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "propA", 14 | "propB" 15 | ], 16 | "type": "object" 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/key-in-key-of-multi-underscores/main.ts: -------------------------------------------------------------------------------- 1 | type Util = { 2 | __2Underscores: { 3 | utilDeepKey2: string; 4 | }; 5 | ___3Underscores: { 6 | utilDeepKey3: string; 7 | }; 8 | ____4Underscores: { 9 | utilDeepKey4: string; 10 | }; 11 | }; 12 | 13 | export type Main = { 14 | [Key in keyof Util]: { 15 | [key: string]: Util[Key]; 16 | }; 17 | }; 18 | -------------------------------------------------------------------------------- /test/programs/key-in-key-of-multi-underscores/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "__2Underscores": { 6 | "additionalProperties": { 7 | "additionalProperties": false, 8 | "properties": { 9 | "utilDeepKey2": { 10 | "type": "string" 11 | } 12 | }, 13 | "required": [ 14 | "utilDeepKey2" 15 | ], 16 | "type": "object" 17 | }, 18 | "type": "object" 19 | }, 20 | "___3Underscores": { 21 | "additionalProperties": { 22 | "additionalProperties": false, 23 | "properties": { 24 | "utilDeepKey3": { 25 | "type": "string" 26 | } 27 | }, 28 | "required": [ 29 | "utilDeepKey3" 30 | ], 31 | "type": "object" 32 | }, 33 | "type": "object" 34 | }, 35 | "____4Underscores": { 36 | "additionalProperties": { 37 | "additionalProperties": false, 38 | "properties": { 39 | "utilDeepKey4": { 40 | "type": "string" 41 | } 42 | }, 43 | "required": [ 44 | "utilDeepKey4" 45 | ], 46 | "type": "object" 47 | }, 48 | "type": "object" 49 | } 50 | }, 51 | "required": [ 52 | "__2Underscores", 53 | "___3Underscores", 54 | "____4Underscores" 55 | ], 56 | "type": "object" 57 | } 58 | -------------------------------------------------------------------------------- /test/programs/key-in-key-of-multi/main.ts: -------------------------------------------------------------------------------- 1 | type Util = { 2 | utilKey1: { 3 | utilDeepKey11: string; 4 | utilDeepKey12: number; 5 | }; 6 | utilKey2: { 7 | utilDeepKey21: boolean; 8 | utilDeepKey22: null; 9 | }; 10 | }; 11 | 12 | export type Main = { 13 | [Key in keyof Util]: { 14 | [key: string]: Util[Key]; 15 | }; 16 | }; 17 | -------------------------------------------------------------------------------- /test/programs/key-in-key-of-multi/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "utilKey1": { 6 | "additionalProperties": { 7 | "additionalProperties": false, 8 | "properties": { 9 | "utilDeepKey11": { 10 | "type": "string" 11 | }, 12 | "utilDeepKey12": { 13 | "type": "number" 14 | } 15 | }, 16 | "required": [ 17 | "utilDeepKey11", 18 | "utilDeepKey12" 19 | ], 20 | "type": "object" 21 | }, 22 | "type": "object" 23 | }, 24 | "utilKey2": { 25 | "additionalProperties": { 26 | "additionalProperties": false, 27 | "properties": { 28 | "utilDeepKey21": { 29 | "type": "boolean" 30 | }, 31 | "utilDeepKey22": { 32 | "type": "null" 33 | } 34 | }, 35 | "required": [ 36 | "utilDeepKey21", 37 | "utilDeepKey22" 38 | ], 39 | "type": "object" 40 | }, 41 | "type": "object" 42 | } 43 | }, 44 | "required": [ 45 | "utilKey1", 46 | "utilKey2" 47 | ], 48 | "type": "object" 49 | } 50 | 51 | -------------------------------------------------------------------------------- /test/programs/key-in-key-of-single/main.ts: -------------------------------------------------------------------------------- 1 | type Util = { 2 | utilKey: { 3 | utilDeepKey: string; 4 | }; 5 | }; 6 | 7 | export type Main = { 8 | [Key in keyof Util]: { 9 | [key: string]: Util[Key]; 10 | }; 11 | }; 12 | -------------------------------------------------------------------------------- /test/programs/key-in-key-of-single/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "type": "object", 5 | "properties": { 6 | "utilKey": { 7 | "type": "object", 8 | "additionalProperties": { 9 | "additionalProperties": false, 10 | "type": "object", 11 | "properties": { 12 | "utilDeepKey": { 13 | "type": "string" 14 | } 15 | }, 16 | "required": [ 17 | "utilDeepKey" 18 | ] 19 | } 20 | } 21 | }, 22 | "required": [ 23 | "utilKey" 24 | ] 25 | } -------------------------------------------------------------------------------- /test/programs/map-types/main.ts: -------------------------------------------------------------------------------- 1 | 2 | interface MyType {} 3 | 4 | interface MyMap1 { 5 | [id: string]: MyType; 6 | } 7 | 8 | /** 9 | * The additionalProperties annotation should be ignored 10 | * @additionalProperties false 11 | */ 12 | interface MyMap2 { 13 | [id: string]: (string | number); 14 | } 15 | 16 | type MyMap3 = Readonly; 17 | 18 | interface MyObject { 19 | map1: MyMap1; 20 | map2: MyMap2; 21 | map3: MyMap3; 22 | } 23 | -------------------------------------------------------------------------------- /test/programs/map-types/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyMap1": { 6 | "additionalProperties": { 7 | "$ref": "#/definitions/MyType" 8 | }, 9 | "type": "object" 10 | }, 11 | "MyMap2": { 12 | "additionalProperties": { 13 | "type": [ 14 | "string", 15 | "number" 16 | ] 17 | }, 18 | "description": "The additionalProperties annotation should be ignored", 19 | "type": "object" 20 | }, 21 | "MyType": { 22 | "additionalProperties": false, 23 | "type": "object" 24 | } 25 | }, 26 | "properties": { 27 | "map1": { 28 | "$ref": "#/definitions/MyMap1" 29 | }, 30 | "map2": { 31 | "$ref": "#/definitions/MyMap2" 32 | }, 33 | "map3": { 34 | "$ref": "#/definitions/MyMap2" 35 | } 36 | }, 37 | "required": [ 38 | "map1", 39 | "map2", 40 | "map3" 41 | ], 42 | "type": "object" 43 | } 44 | 45 | -------------------------------------------------------------------------------- /test/programs/module-interface-deep/main.ts: -------------------------------------------------------------------------------- 1 | module MyModule { 2 | export interface Def { 3 | nest: Def; 4 | prev: MyModule.Def; 5 | propA: SubModule.HelperA; 6 | propB: SubModule.HelperB; 7 | } 8 | export module SubModule { 9 | export interface HelperA { 10 | propA: number; 11 | propB: HelperB; 12 | } 13 | export interface HelperB { 14 | propA: SubModule.HelperA; 15 | propB: Def; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test/programs/module-interface-deep/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyModule.Def": { 5 | "id": "MyModule.Def", 6 | "type": "object", 7 | "properties": { 8 | "nest": { 9 | "$ref": "#/definitions/MyModule.Def" 10 | }, 11 | "prev": { 12 | "$ref": "#/definitions/MyModule.Def" 13 | }, 14 | "propA": { 15 | "$ref": "#/definitions/MyModule.SubModule.HelperA" 16 | }, 17 | "propB": { 18 | "$ref": "#/definitions/MyModule.SubModule.HelperB" 19 | } 20 | }, 21 | "required": [ 22 | "nest", 23 | "prev", 24 | "propA", 25 | "propB" 26 | ], 27 | "additionalProperties": false 28 | }, 29 | "MyModule.SubModule.HelperA": { 30 | "id": "MyModule.SubModule.HelperA", 31 | "type": "object", 32 | "properties": { 33 | "propA": { 34 | "type": "number" 35 | }, 36 | "propB": { 37 | "$ref": "#/definitions/MyModule.SubModule.HelperB" 38 | } 39 | }, 40 | "required": [ 41 | "propA", 42 | "propB" 43 | ], 44 | "additionalProperties": false 45 | }, 46 | "MyModule.SubModule.HelperB": { 47 | "id": "MyModule.SubModule.HelperB", 48 | "type": "object", 49 | "properties": { 50 | "propA": { 51 | "$ref": "#/definitions/MyModule.SubModule.HelperA" 52 | }, 53 | "propB": { 54 | "$ref": "#/definitions/MyModule.Def" 55 | } 56 | }, 57 | "required": [ 58 | "propA", 59 | "propB" 60 | ], 61 | "additionalProperties": false 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /test/programs/module-interface-single/main.ts: -------------------------------------------------------------------------------- 1 | module MyModule { 2 | interface MyObject { 3 | propA: number; 4 | propB: number; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /test/programs/module-interface-single/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "propA": { 6 | "type": "number" 7 | }, 8 | "propB": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "propA", 14 | "propB" 15 | ], 16 | "type": "object" 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/namespace-deep-1/main.ts: -------------------------------------------------------------------------------- 1 | namespace RootNamespace { 2 | export interface Def { 3 | nest: Def; 4 | prev: RootNamespace.Def; 5 | 6 | propA: SubNamespace.HelperA; 7 | propB: SubNamespace.HelperB; 8 | } 9 | 10 | export namespace SubNamespace { 11 | export interface HelperA { 12 | propA: number; 13 | propB: HelperB; 14 | } 15 | export interface HelperB { 16 | propA: SubNamespace.HelperA; 17 | propB: Def; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/programs/namespace-deep-1/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "$ref": "#/definitions/RootNamespace.Def", 4 | "definitions": { 5 | "RootNamespace.Def": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "nest": { 9 | "$ref": "#/definitions/RootNamespace.Def" 10 | }, 11 | "prev": { 12 | "$ref": "#/definitions/RootNamespace.Def" 13 | }, 14 | "propA": { 15 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA" 16 | }, 17 | "propB": { 18 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" 19 | } 20 | }, 21 | "required": [ 22 | "nest", 23 | "prev", 24 | "propA", 25 | "propB" 26 | ], 27 | "type": "object" 28 | }, 29 | "RootNamespace.SubNamespace.HelperA": { 30 | "additionalProperties": false, 31 | "properties": { 32 | "propA": { 33 | "type": "number" 34 | }, 35 | "propB": { 36 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" 37 | } 38 | }, 39 | "required": [ 40 | "propA", 41 | "propB" 42 | ], 43 | "type": "object" 44 | }, 45 | "RootNamespace.SubNamespace.HelperB": { 46 | "additionalProperties": false, 47 | "properties": { 48 | "propA": { 49 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA" 50 | }, 51 | "propB": { 52 | "$ref": "#/definitions/RootNamespace.Def" 53 | } 54 | }, 55 | "required": [ 56 | "propA", 57 | "propB" 58 | ], 59 | "type": "object" 60 | } 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /test/programs/namespace-deep-2/main.ts: -------------------------------------------------------------------------------- 1 | namespace RootNamespace { 2 | export interface Def { 3 | nest: Def; 4 | prev: RootNamespace.Def; 5 | 6 | propA: SubNamespace.HelperA; 7 | propB: SubNamespace.HelperB; 8 | } 9 | 10 | export namespace SubNamespace { 11 | export interface HelperA { 12 | propA: number; 13 | propB: HelperB; 14 | } 15 | export interface HelperB { 16 | propA: SubNamespace.HelperA; 17 | propB: Def; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/programs/namespace-deep-2/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA", 4 | "definitions": { 5 | "RootNamespace.Def": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "nest": { 9 | "$ref": "#/definitions/RootNamespace.Def" 10 | }, 11 | "prev": { 12 | "$ref": "#/definitions/RootNamespace.Def" 13 | }, 14 | "propA": { 15 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA" 16 | }, 17 | "propB": { 18 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" 19 | } 20 | }, 21 | "required": [ 22 | "nest", 23 | "prev", 24 | "propA", 25 | "propB" 26 | ], 27 | "type": "object" 28 | }, 29 | "RootNamespace.SubNamespace.HelperA": { 30 | "additionalProperties": false, 31 | "properties": { 32 | "propA": { 33 | "type": "number" 34 | }, 35 | "propB": { 36 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperB" 37 | } 38 | }, 39 | "required": [ 40 | "propA", 41 | "propB" 42 | ], 43 | "type": "object" 44 | }, 45 | "RootNamespace.SubNamespace.HelperB": { 46 | "additionalProperties": false, 47 | "properties": { 48 | "propA": { 49 | "$ref": "#/definitions/RootNamespace.SubNamespace.HelperA" 50 | }, 51 | "propB": { 52 | "$ref": "#/definitions/RootNamespace.Def" 53 | } 54 | }, 55 | "required": [ 56 | "propA", 57 | "propB" 58 | ], 59 | "type": "object" 60 | } 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /test/programs/namespace/main.ts: -------------------------------------------------------------------------------- 1 | export namespace Types { 2 | export const X: "x" = "x"; 3 | export const Y: "y" = "y"; 4 | } 5 | 6 | export type Type = typeof Types.X | typeof Types.Y; 7 | -------------------------------------------------------------------------------- /test/programs/namespace/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "enum": [ 4 | "x", 5 | "y" 6 | ], 7 | "type": "string" 8 | } 9 | 10 | -------------------------------------------------------------------------------- /test/programs/never/main.ts: -------------------------------------------------------------------------------- 1 | export interface Never { 2 | neverProp: never; 3 | propA: string; 4 | } 5 | -------------------------------------------------------------------------------- /test/programs/never/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "propA": { 6 | "type": "string" 7 | } 8 | }, 9 | "required": [ 10 | "propA" 11 | ], 12 | "type": "object" 13 | } 14 | -------------------------------------------------------------------------------- /test/programs/no-ref/main.ts: -------------------------------------------------------------------------------- 1 | type MySubType = { 2 | id: string; 3 | }; 4 | 5 | export type MyModule = { 6 | address: MySubType & { extraProp: number }; 7 | address2: MySubType; 8 | address3: MySubType; 9 | }; 10 | -------------------------------------------------------------------------------- /test/programs/no-ref/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "object", 3 | "properties": { 4 | "address": { 5 | "additionalProperties": false, 6 | "type": "object", 7 | "properties": { 8 | "id": { 9 | "type": "string" 10 | }, 11 | "extraProp": { 12 | "type": "number" 13 | } 14 | }, 15 | "required": ["extraProp", "id"] 16 | }, 17 | "address2": { 18 | "additionalProperties": false, 19 | "type": "object", 20 | "properties": { 21 | "id": { 22 | "type": "string" 23 | } 24 | }, 25 | "required": ["id"] 26 | }, 27 | "address3": { 28 | "additionalProperties": false, 29 | "type": "object", 30 | "properties": { 31 | "id": { 32 | "type": "string" 33 | } 34 | }, 35 | "required": ["id"] 36 | } 37 | }, 38 | "additionalProperties": false, 39 | "required": ["address", "address2", "address3"], 40 | "$schema": "http://json-schema.org/draft-07/schema#" 41 | } 42 | -------------------------------------------------------------------------------- /test/programs/no-unrelated-definitions/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | sub: SomeDefinition; 3 | } 4 | 5 | interface SomeDefinition { 6 | is: string; 7 | } 8 | 9 | export interface MyOtherObject { 10 | sub: SomeOtherDefinition; 11 | } 12 | 13 | interface SomeOtherDefinition { 14 | is: string; 15 | } 16 | -------------------------------------------------------------------------------- /test/programs/no-unrelated-definitions/schema.MyObject.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "SomeDefinition": { 5 | "type": "object", 6 | "properties": { 7 | "is": { 8 | "type": "string" 9 | } 10 | } 11 | } 12 | }, 13 | "properties": { 14 | "sub": { 15 | "$ref": "#/definitions/SomeDefinition" 16 | } 17 | }, 18 | "type": "object" 19 | } 20 | -------------------------------------------------------------------------------- /test/programs/no-unrelated-definitions/schema.MyOtherObject.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "SomeOtherDefinition": { 5 | "type": "object", 6 | "properties": { 7 | "is": { 8 | "type": "string" 9 | } 10 | } 11 | } 12 | }, 13 | "properties": { 14 | "sub": { 15 | "$ref": "#/definitions/SomeOtherDefinition" 16 | } 17 | }, 18 | "type": "object" 19 | } 20 | -------------------------------------------------------------------------------- /test/programs/no-unrelated-definitions/schema.MyOtherObjectWithOverride.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "SomeOtherDefinition": { 5 | "type": "string" 6 | } 7 | }, 8 | "properties": { 9 | "sub": { 10 | "$ref": "#/definitions/SomeOtherDefinition" 11 | } 12 | }, 13 | "type": "object" 14 | } 15 | -------------------------------------------------------------------------------- /test/programs/no-unrelated-definitions/schema.program.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyObject": { 5 | "properties": { 6 | "sub": { 7 | "$ref": "#/definitions/SomeDefinition" 8 | } 9 | }, 10 | "type": "object" 11 | }, 12 | "MyOtherObject": { 13 | "properties": { 14 | "sub": { 15 | "$ref": "#/definitions/SomeOtherDefinition" 16 | } 17 | }, 18 | "type": "object" 19 | }, 20 | "SomeDefinition": { 21 | "properties": { 22 | "is": { 23 | "type": "string" 24 | } 25 | }, 26 | "type": "object" 27 | }, 28 | "SomeOtherDefinition": { 29 | "properties": { 30 | "is": { 31 | "type": "string" 32 | } 33 | }, 34 | "type": "object" 35 | }, 36 | "UnrelatedDefinition": { 37 | "type": "string" 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/programs/no-unrelated-definitions/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | }, 5 | "include": ["main.ts"] 6 | } 7 | -------------------------------------------------------------------------------- /test/programs/numeric-keys-and-others/main.ts: -------------------------------------------------------------------------------- 1 | interface NumericKeysAndOthers { 2 | [key: number]: number; 3 | a: string; 4 | b: boolean; 5 | } 6 | -------------------------------------------------------------------------------- /test/programs/numeric-keys-and-others/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "patternProperties": { 5 | "^[0-9]+$": { 6 | "type": "number" 7 | } 8 | }, 9 | "properties": { 10 | "a": { 11 | "type": "string" 12 | }, 13 | "b": { 14 | "type": "boolean" 15 | } 16 | }, 17 | "required": [ 18 | "a", 19 | "b" 20 | ], 21 | "type": "object" 22 | } -------------------------------------------------------------------------------- /test/programs/object-numeric-index-as-property/main.ts: -------------------------------------------------------------------------------- 1 | interface Target { 2 | objAnonymous: { 3 | [index: number]: number; 4 | }; 5 | objInterface: IndexInterface; 6 | indexInType: { [index in number]?: number }; 7 | indexInInline: { [index in number]: number }; 8 | indexInPartialType: IndexInPartial; 9 | indexInPartialInline: { [index in number]?: number }; 10 | } 11 | interface IndexInterface { 12 | [index: number]: number; 13 | } 14 | 15 | type IndexIn = { [index in number]: number }; 16 | type IndexInPartial = { [index in number]?: number }; 17 | -------------------------------------------------------------------------------- /test/programs/object-numeric-index-as-property/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "objAnonymous": { 6 | "additionalProperties": false, 7 | "patternProperties": { "^[0-9]+$": { "type": "number" } }, 8 | "type": "object" 9 | }, 10 | "objInterface": { 11 | "additionalProperties": false, 12 | "patternProperties": { "^[0-9]+$": { "type": "number" } }, 13 | "type": "object" 14 | }, 15 | "indexInType": { 16 | "additionalProperties": false, 17 | "patternProperties": { "^[0-9]+$": { "type": "number" } }, 18 | "type": "object" 19 | }, 20 | "indexInInline": { 21 | "additionalProperties": false, 22 | "patternProperties": { "^[0-9]+$": { "type": "number" } }, 23 | "type": "object" 24 | }, 25 | "indexInPartialType": { 26 | "additionalProperties": false, 27 | "patternProperties": { "^[0-9]+$": { "type": "number" } }, 28 | "type": "object" 29 | }, 30 | "indexInPartialInline": { 31 | "additionalProperties": false, 32 | "patternProperties": { "^[0-9]+$": { "type": "number" } }, 33 | "type": "object" 34 | } 35 | }, 36 | "type": "object" 37 | } 38 | -------------------------------------------------------------------------------- /test/programs/object-numeric-index/main.ts: -------------------------------------------------------------------------------- 1 | interface IndexInterface { 2 | [index: number]: number; 3 | } 4 | -------------------------------------------------------------------------------- /test/programs/object-numeric-index/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "patternProperties": { 5 | "^[0-9]+$": { 6 | "type": "number" 7 | } 8 | }, 9 | "type": "object" 10 | } 11 | -------------------------------------------------------------------------------- /test/programs/optionals-derived/main.ts: -------------------------------------------------------------------------------- 1 | interface MyBase { 2 | baseRequired : number; 3 | baseOptional?: number; 4 | } 5 | 6 | interface MyDerived extends MyBase { 7 | derivedRequired : number; 8 | derivedOptional?: number; 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/optionals-derived/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "type": "object", 5 | "required": [ "baseRequired", "derivedRequired" ], 6 | "properties": { 7 | "baseRequired": { 8 | "type": "number" 9 | }, 10 | "baseOptional": { 11 | "type": "number" 12 | }, 13 | "derivedRequired": { 14 | "type": "number" 15 | }, 16 | "derivedOptional": { 17 | "type": "number" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/programs/optionals/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | required:number; 3 | optional?:number; 4 | } 5 | -------------------------------------------------------------------------------- /test/programs/optionals/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "optional": { 6 | "type": "number" 7 | }, 8 | "required": { 9 | "type": "number" 10 | } 11 | }, 12 | "required": [ 13 | "required" 14 | ], 15 | "type": "object" 16 | } 17 | 18 | -------------------------------------------------------------------------------- /test/programs/private-members/main.ts: -------------------------------------------------------------------------------- 1 | export class MyObject { 2 | publicMember: string; 3 | private privateMember: string; 4 | } 5 | -------------------------------------------------------------------------------- /test/programs/private-members/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "publicMember": { 6 | "type": "string" 7 | } 8 | }, 9 | "required": [ 10 | "publicMember" 11 | ], 12 | "type": "object" 13 | } 14 | 15 | -------------------------------------------------------------------------------- /test/programs/prop-override/main.ts: -------------------------------------------------------------------------------- 1 | import type { ObjectId } from './third-party' 2 | 3 | export type MyObject = { 4 | /** 5 | * @TJS-type string 6 | * @description Overrides aliased type definition with this JSDoc if at least TJS-type annotation is present 7 | */ 8 | _id: ObjectId 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/prop-override/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "_id": { 6 | "description": "Overrides aliased type definition with this JSDoc if at least TJS-type annotation is present", 7 | "type": "string" 8 | } 9 | }, 10 | "required": [ 11 | "_id" 12 | ], 13 | "type": "object" 14 | } 15 | 16 | -------------------------------------------------------------------------------- /test/programs/prop-override/third-party.ts: -------------------------------------------------------------------------------- 1 | // cannot modify with JSDoc because third-party sources 2 | export class ObjectId {} -------------------------------------------------------------------------------- /test/programs/satisfies-keyword/main.ts: -------------------------------------------------------------------------------- 1 | interface Basic { 2 | a: string; 3 | b: number; 4 | c: boolean; 5 | } 6 | 7 | const myObject = { 8 | a: "a" as const, 9 | b: 1 as const, 10 | c: true as const, 11 | // tslint:disable-next-line:variable-name 12 | } satisfies Basic; 13 | 14 | export type Specific = typeof myObject; 15 | -------------------------------------------------------------------------------- /test/programs/satisfies-keyword/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "a": { 6 | "const": "a", 7 | "type": "string" 8 | }, 9 | "b": { 10 | "const": 1, 11 | "type": "number" 12 | }, 13 | "c": { 14 | "const": true, 15 | "type": "boolean" 16 | } 17 | }, 18 | "required": [ 19 | "a", 20 | "b", 21 | "c" 22 | ], 23 | "type": "object" 24 | } 25 | -------------------------------------------------------------------------------- /test/programs/strict-null-checks/main.ts: -------------------------------------------------------------------------------- 1 | 2 | class MyObject { 3 | val: number; 4 | valNullable: number | null; 5 | valUndef: number | undefined; 6 | valOpt?: number; 7 | valVoid: number | void; 8 | 9 | valTrueOpt?: true; 10 | valTrueOrNull: true|null; 11 | valTrue: true|true; // twice to check that it will appear only once 12 | } 13 | -------------------------------------------------------------------------------- /test/programs/strict-null-checks/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "val": { 6 | "type": "number" 7 | }, 8 | "valNullable": { 9 | "type": [ 10 | "null", 11 | "number" 12 | ] 13 | }, 14 | "valOpt": { 15 | "type": "number" 16 | }, 17 | "valTrue": { 18 | "const": true, 19 | "type": "boolean" 20 | }, 21 | "valTrueOpt": { 22 | "const": true, 23 | "type": "boolean" 24 | }, 25 | "valTrueOrNull": { 26 | "anyOf": [ 27 | { 28 | "const": true, 29 | "type": "boolean" 30 | }, 31 | { 32 | "type": "null" 33 | } 34 | ] 35 | }, 36 | "valUndef": { 37 | "type": "number" 38 | }, 39 | "valVoid": { 40 | "type": "number" 41 | } 42 | }, 43 | "required": [ 44 | "val", 45 | "valNullable", 46 | "valTrue", 47 | "valTrueOrNull" 48 | ], 49 | "type": "object" 50 | } 51 | 52 | -------------------------------------------------------------------------------- /test/programs/string-literals-inline/main.ts: -------------------------------------------------------------------------------- 1 | class MyObject { 2 | foo: "ok" | "fail" | "abort" | ""; 3 | bar: "ok" | "fail" | "abort" | string; 4 | } 5 | -------------------------------------------------------------------------------- /test/programs/string-literals-inline/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "bar": { 6 | "type": "string" 7 | }, 8 | "foo": { 9 | "enum": [ 10 | "", 11 | "abort", 12 | "fail", 13 | "ok" 14 | ], 15 | "type": "string" 16 | } 17 | }, 18 | "required": [ 19 | "bar", 20 | "foo" 21 | ], 22 | "type": "object" 23 | } 24 | 25 | -------------------------------------------------------------------------------- /test/programs/string-literals/main.ts: -------------------------------------------------------------------------------- 1 | type result = "ok" | "fail" | "abort" | ""; 2 | 3 | class MyObject { 4 | foo: result; 5 | bar: result | string; 6 | } 7 | -------------------------------------------------------------------------------- /test/programs/string-literals/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "result": { 6 | "enum": [ 7 | "", 8 | "abort", 9 | "fail", 10 | "ok" 11 | ], 12 | "type": "string" 13 | } 14 | }, 15 | "properties": { 16 | "bar": { 17 | "type": "string" 18 | }, 19 | "foo": { 20 | "$ref": "#/definitions/result" 21 | } 22 | }, 23 | "required": [ 24 | "bar", 25 | "foo" 26 | ], 27 | "type": "object" 28 | } 29 | 30 | -------------------------------------------------------------------------------- /test/programs/string-template-literal/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | a: `@${string}`, 3 | b: `@${number}`, 4 | c: `@${bigint}`, 5 | d: `@${boolean}`, 6 | e: `@${undefined}`, 7 | f: `@${null}`, 8 | g: `${string}@`, 9 | h: `${number}@`, 10 | i: `${string}@${number}`, 11 | j: `${string}.${string}`, 12 | } -------------------------------------------------------------------------------- /test/programs/string-template-literal/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "object", 3 | "properties": { 4 | "a": { 5 | "type": "string", 6 | "pattern": "^@.*$" 7 | }, 8 | "b": { 9 | "type": "string", 10 | "pattern": "^@[0-9]*$" 11 | }, 12 | "c": { 13 | "type": "string", 14 | "pattern": "^@[0-9]*$" 15 | }, 16 | "d": { 17 | "enum": [ 18 | "@false", 19 | "@true" 20 | ], 21 | "type": "string" 22 | }, 23 | "e": { 24 | "type": "string", 25 | "const": "@undefined" 26 | }, 27 | "f": { 28 | "type": "string", 29 | "const": "@null" 30 | }, 31 | "g": { 32 | "type": "string", 33 | "pattern": "^.*@$" 34 | }, 35 | "h": { 36 | "type": "string", 37 | "pattern": "^[0-9]*@$" 38 | }, 39 | "i": { 40 | "type": "string", 41 | "pattern": "^.*@[0-9]*$" 42 | }, 43 | "j": { 44 | "type": "string", 45 | "pattern": "^.*\\..*$" 46 | } 47 | }, 48 | "additionalProperties": false, 49 | "required": [ 50 | "a", 51 | "b", 52 | "c", 53 | "d", 54 | "e", 55 | "f", 56 | "g", 57 | "h", 58 | "i", 59 | "j" 60 | ], 61 | "$schema": "http://json-schema.org/draft-07/schema#" 62 | } -------------------------------------------------------------------------------- /test/programs/symbol/main.ts: -------------------------------------------------------------------------------- 1 | export type MyObject = { 2 | a: symbol; 3 | }; 4 | -------------------------------------------------------------------------------- /test/programs/symbol/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "type": "object", 5 | "properties": { 6 | "a": { 7 | "type": "object" 8 | } 9 | }, 10 | "required": [ 11 | "a" 12 | ] 13 | } -------------------------------------------------------------------------------- /test/programs/tsconfig/exclude.ts: -------------------------------------------------------------------------------- 1 | // This file is ignored. 2 | 3 | export interface Excluded { 4 | a: string; 5 | } 6 | -------------------------------------------------------------------------------- /test/programs/tsconfig/includedAlways.ts: -------------------------------------------------------------------------------- 1 | // This file is included by tsconfig.json and --include. 2 | 3 | export interface IncludedAlways { 4 | a: string; 5 | }; 6 | -------------------------------------------------------------------------------- /test/programs/tsconfig/includedByConfig.ts: -------------------------------------------------------------------------------- 1 | // This file is included by tsconfig.json. 2 | 3 | export interface IncludedOnlyByTsConfig { 4 | a: string; 5 | }; 6 | -------------------------------------------------------------------------------- /test/programs/tsconfig/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | }, 5 | "include": [ 6 | "**/include*.ts", 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/type-alias-never/main.ts: -------------------------------------------------------------------------------- 1 | export type MyNever = never; 2 | -------------------------------------------------------------------------------- /test/programs/type-alias-never/schema.json: -------------------------------------------------------------------------------- 1 | "Creating a schema for alias of never will fail" -------------------------------------------------------------------------------- /test/programs/type-alias-or/main.ts: -------------------------------------------------------------------------------- 1 | interface A {} 2 | interface B {} 3 | 4 | type C = A | B; 5 | 6 | interface MyObject { 7 | c: C; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/type-alias-or/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "A": { 6 | "additionalProperties": false, 7 | "type": "object" 8 | }, 9 | "B": { 10 | "additionalProperties": false, 11 | "type": "object" 12 | }, 13 | "C": { 14 | "anyOf": [ 15 | { 16 | "$ref": "#/definitions/A" 17 | }, 18 | { 19 | "$ref": "#/definitions/B" 20 | } 21 | ] 22 | } 23 | }, 24 | "properties": { 25 | "c": { 26 | "$ref": "#/definitions/C" 27 | } 28 | }, 29 | "required": [ 30 | "c" 31 | ], 32 | "type": "object" 33 | } 34 | -------------------------------------------------------------------------------- /test/programs/type-alias-schema-override/main.ts: -------------------------------------------------------------------------------- 1 | interface All {} 2 | 3 | type Some = Partial; 4 | 5 | interface MyObject { 6 | some?: Some; 7 | } 8 | -------------------------------------------------------------------------------- /test/programs/type-alias-schema-override/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "Some": { 5 | "type": "string" 6 | } 7 | }, 8 | "properties": { 9 | "some": { 10 | "$ref": "#/definitions/Some" 11 | } 12 | }, 13 | "type": "object" 14 | } 15 | -------------------------------------------------------------------------------- /test/programs/type-alias-single-annotated/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This is a description 3 | * @pattern ^mystring-[a-zA-Z0-9]+$ 4 | * @minLength 10 5 | * @maxLength 24 6 | */ 7 | type MyString = string; 8 | -------------------------------------------------------------------------------- /test/programs/type-alias-single-annotated/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "description": "This is a description", 4 | "type": "string", 5 | "pattern": "^mystring-[a-zA-Z0-9]+$", 6 | "minLength": 10, 7 | "maxLength": 24 8 | } 9 | 10 | -------------------------------------------------------------------------------- /test/programs/type-alias-single/main.ts: -------------------------------------------------------------------------------- 1 | type MyString = string; 2 | -------------------------------------------------------------------------------- /test/programs/type-alias-single/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "type": "string" 4 | } 5 | 6 | -------------------------------------------------------------------------------- /test/programs/type-alias-undefined/main.ts: -------------------------------------------------------------------------------- 1 | export type MyUndefined = undefined; 2 | -------------------------------------------------------------------------------- /test/programs/type-alias-undefined/schema.json: -------------------------------------------------------------------------------- 1 | "Creating a schema for alias of undefined will fail" -------------------------------------------------------------------------------- /test/programs/type-aliases-alias-ref-topref/main.ts: -------------------------------------------------------------------------------- 1 | 2 | interface MyObject { 3 | prop: number; 4 | } 5 | 6 | type MyAlias = MyObject; 7 | -------------------------------------------------------------------------------- /test/programs/type-aliases-alias-ref-topref/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyObject": { 5 | "type": "object", 6 | "properties": { 7 | "prop": { 8 | "type": "number" 9 | } 10 | }, 11 | "required": [ 12 | "prop" 13 | ] 14 | }, 15 | "MyAlias": { 16 | "$ref": "#/definitions/MyObject" 17 | } 18 | }, 19 | "$ref": "#/definitions/MyAlias" 20 | } 21 | -------------------------------------------------------------------------------- /test/programs/type-aliases-alias-ref/main.ts: -------------------------------------------------------------------------------- 1 | 2 | interface MyObject { 3 | prop: number; 4 | } 5 | 6 | type MyAlias = MyObject; 7 | -------------------------------------------------------------------------------- /test/programs/type-aliases-alias-ref/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "prop": { 6 | "type": "number" 7 | } 8 | }, 9 | "required": [ 10 | "prop" 11 | ], 12 | "type": "object" 13 | } 14 | 15 | -------------------------------------------------------------------------------- /test/programs/type-aliases-anonymous/main.ts: -------------------------------------------------------------------------------- 1 | export type MyExportString = string; 2 | type MyPrivateString = string; 3 | 4 | export interface MyObject { 5 | export: MyExportString; 6 | private: MyPrivateString; 7 | } 8 | -------------------------------------------------------------------------------- /test/programs/type-aliases-anonymous/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyObject": { 5 | "type": "object", 6 | "properties": { 7 | "export": { 8 | "$ref": "#/definitions/MyExportString" 9 | }, 10 | "private": { 11 | "type": "string" 12 | } 13 | }, 14 | "required": [ 15 | "export", 16 | "private" 17 | ], 18 | "additionalProperties": false 19 | }, 20 | "MyExportString": { 21 | "type": "string" 22 | } 23 | }, 24 | "$ref": "#/definitions/MyObject" 25 | } 26 | -------------------------------------------------------------------------------- /test/programs/type-aliases-fixed-size-array/main.ts: -------------------------------------------------------------------------------- 1 | type MyFixedSizeArray = [string, number]; 2 | 3 | -------------------------------------------------------------------------------- /test/programs/type-aliases-fixed-size-array/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "items": [ 4 | { 5 | "type": "string" 6 | }, 7 | { 8 | "type": "number" 9 | } 10 | ], 11 | "maxItems": 2, 12 | "minItems": 2, 13 | "type": "array" 14 | } 15 | 16 | -------------------------------------------------------------------------------- /test/programs/type-aliases-local-namespace/main.ts: -------------------------------------------------------------------------------- 1 | namespace A { 2 | export interface A { a: any; } 3 | } 4 | namespace B { 5 | export interface B { b: any; } 6 | } 7 | namespace C { 8 | import A = B.B; 9 | export interface C { c: A; } 10 | } 11 | namespace D { 12 | import A = C.C; 13 | export interface D { d: A; } 14 | } 15 | 16 | export interface MyObject extends D.D {} 17 | -------------------------------------------------------------------------------- /test/programs/type-aliases-local-namespace/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "B.B": { 5 | "properties": { 6 | "b": { 7 | } 8 | }, 9 | "required": [ 10 | "b" 11 | ], 12 | "additionalProperties": false, 13 | "type": "object" 14 | }, 15 | "C.C": { 16 | "properties": { 17 | "c": { 18 | "$ref": "#/definitions/B.B" 19 | } 20 | }, 21 | "required": [ 22 | "c" 23 | ], 24 | "additionalProperties": false, 25 | "type": "object" 26 | }, 27 | "D.D": { 28 | "properties": { 29 | "d": { 30 | "$ref": "#/definitions/C.C" 31 | } 32 | }, 33 | "required": [ 34 | "d" 35 | ], 36 | "additionalProperties": false, 37 | "type": "object" 38 | }, 39 | "MyObject": { 40 | "$ref": "#/definitions/D.D" 41 | } 42 | }, 43 | "$ref": "#/definitions/MyObject" 44 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-local-namsepace/main.ts: -------------------------------------------------------------------------------- 1 | namespace A { 2 | export interface A {a: any;} 3 | } 4 | namespace B { 5 | export interface B {b: any;} 6 | } 7 | namespace C { 8 | import A = B.B; 9 | export interface C {c: A;} 10 | } 11 | namespace D { 12 | import A = C.C; 13 | export interface D {d: A;} 14 | } 15 | 16 | interface MyObject extends D.D {} 17 | -------------------------------------------------------------------------------- /test/programs/type-aliases-local-namsepace/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "B.B": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "b": { 9 | } 10 | }, 11 | "required": [ 12 | "b" 13 | ], 14 | "type": "object" 15 | }, 16 | "C.C": { 17 | "additionalProperties": false, 18 | "properties": { 19 | "c": { 20 | "$ref": "#/definitions/B.B" 21 | } 22 | }, 23 | "required": [ 24 | "c" 25 | ], 26 | "type": "object" 27 | } 28 | }, 29 | "properties": { 30 | "d": { 31 | "$ref": "#/definitions/C.C" 32 | } 33 | }, 34 | "required": [ 35 | "d" 36 | ], 37 | "type": "object" 38 | } 39 | 40 | -------------------------------------------------------------------------------- /test/programs/type-aliases-mixed/main.ts: -------------------------------------------------------------------------------- 1 | export type MyString = string; 2 | 3 | export interface MySubObject { 4 | propA: number; 5 | propB: number; 6 | } 7 | 8 | export interface MyObject { 9 | primitive: MyString; 10 | object: MySubObject; 11 | } 12 | -------------------------------------------------------------------------------- /test/programs/type-aliases-mixed/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyObject": { 5 | "properties": { 6 | "object": { 7 | "$ref": "#/definitions/MySubObject" 8 | }, 9 | "primitive": { 10 | "$ref": "#/definitions/MyString" 11 | } 12 | }, 13 | "required": [ 14 | "primitive", 15 | "object" 16 | ], 17 | "additionalProperties": false, 18 | "type": "object" 19 | }, 20 | "MyString": { 21 | "type": "string" 22 | }, 23 | "MySubObject": { 24 | "properties": { 25 | "propA": { 26 | "type": "number" 27 | }, 28 | "propB": { 29 | "type": "number" 30 | } 31 | }, 32 | "required": [ 33 | "propA", 34 | "propB" 35 | ], 36 | "additionalProperties": false, 37 | "type": "object" 38 | } 39 | }, 40 | "$ref": "#/definitions/MyObject" 41 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-multitype-array/main.ts: -------------------------------------------------------------------------------- 1 | 2 | type BasicArray = (string | number)[]; 3 | 4 | interface MyObject { 5 | array: BasicArray; 6 | } 7 | 8 | type MyArray = (string | MyObject)[]; 9 | -------------------------------------------------------------------------------- /test/programs/type-aliases-multitype-array/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyObject": { 5 | "additionalProperties": false, 6 | "properties": { 7 | "array": { 8 | "items": { 9 | "type": [ 10 | "string", 11 | "number" 12 | ] 13 | }, 14 | "type": "array" 15 | } 16 | }, 17 | "required": [ 18 | "array" 19 | ], 20 | "type": "object" 21 | } 22 | }, 23 | "items": { 24 | "anyOf": [ 25 | { 26 | "$ref": "#/definitions/MyObject" 27 | }, 28 | { 29 | "type": "string" 30 | } 31 | ] 32 | }, 33 | "type": "array" 34 | } 35 | 36 | -------------------------------------------------------------------------------- /test/programs/type-aliases-object/main.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface MyObject { 3 | number: number; 4 | string: string; 5 | } 6 | 7 | export type MyAlias = MyObject; 8 | -------------------------------------------------------------------------------- /test/programs/type-aliases-object/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyAlias": { 5 | "$ref": "#/definitions/MyObject" 6 | }, 7 | "MyObject": { 8 | "properties": { 9 | "number": { 10 | "type": "number" 11 | }, 12 | "string": { 13 | "type": "string" 14 | } 15 | }, 16 | "required": [ 17 | "number", 18 | "string" 19 | ], 20 | "additionalProperties": false, 21 | "type": "object" 22 | } 23 | }, 24 | "$ref": "#/definitions/MyAlias" 25 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-partial/main.ts: -------------------------------------------------------------------------------- 1 | export interface Foo { 2 | x: number; 3 | y: number; 4 | } 5 | 6 | export interface Bar { 7 | a: number; 8 | b: number; 9 | } 10 | 11 | export interface MyObject { 12 | foo: Partial; 13 | bar: Partial; 14 | } 15 | -------------------------------------------------------------------------------- /test/programs/type-aliases-partial/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "object", 3 | "additionalProperties": false, 4 | "properties": { 5 | "foo": { 6 | "$ref": "#/definitions/Partial" 7 | }, 8 | "bar": { 9 | "$ref": "#/definitions/Partial_1" 10 | } 11 | }, 12 | "required": [ 13 | "bar", 14 | "foo" 15 | ], 16 | "definitions": { 17 | "__type": { 18 | "additionalProperties": false, 19 | "type": "object", 20 | "properties": { 21 | "x": { 22 | "type": "number" 23 | }, 24 | "y": { 25 | "type": "number" 26 | } 27 | } 28 | }, 29 | "Partial": { 30 | "$ref": "#/definitions/__type" 31 | }, 32 | "__type_1": { 33 | "type": "object", 34 | "additionalProperties": false, 35 | "properties": { 36 | "a": { 37 | "type": "number" 38 | }, 39 | "b": { 40 | "type": "number" 41 | } 42 | } 43 | }, 44 | "Partial_1": { 45 | "$ref": "#/definitions/__type_1" 46 | } 47 | }, 48 | "$schema": "http://json-schema.org/draft-07/schema#" 49 | } 50 | -------------------------------------------------------------------------------- /test/programs/type-aliases-primitive/main.ts: -------------------------------------------------------------------------------- 1 | export type MyString = string; 2 | -------------------------------------------------------------------------------- /test/programs/type-aliases-primitive/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyString": { 5 | "type": "string" 6 | } 7 | }, 8 | "$ref": "#/definitions/MyString" 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/type-aliases-recursive-alias-topref/main.ts: -------------------------------------------------------------------------------- 1 | 2 | interface MyObject { 3 | alias: MyAlias; 4 | self: MyObject; 5 | } 6 | 7 | type MyAlias = MyObject; 8 | -------------------------------------------------------------------------------- /test/programs/type-aliases-recursive-alias-topref/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyAlias": { 5 | "$ref": "#/definitions/MyObject" 6 | }, 7 | "MyObject": { 8 | "properties": { 9 | "alias": { 10 | "$ref": "#/definitions/MyAlias" 11 | }, 12 | "self": { 13 | "$ref": "#/definitions/MyObject" 14 | } 15 | }, 16 | "required": [ 17 | "alias", 18 | "self" 19 | ], 20 | "type": "object" 21 | } 22 | }, 23 | "$ref": "#/definitions/MyAlias" 24 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-recursive-anonymous/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | alias: MyAlias; 3 | self: MyObject; 4 | } 5 | 6 | export type MyAlias = MyObject; 7 | -------------------------------------------------------------------------------- /test/programs/type-aliases-recursive-anonymous/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyAlias": { 5 | "$ref": "#/definitions/interface-0" 6 | }, 7 | "interface-0": { 8 | "type": "object", 9 | "properties": { 10 | "alias": { 11 | "$ref": "#/definitions/MyAlias" 12 | }, 13 | "self": { 14 | "$ref": "#/definitions/interface-0" 15 | } 16 | }, 17 | "required": [ 18 | "alias", 19 | "self" 20 | ], 21 | "additionalProperties": false 22 | } 23 | }, 24 | "$ref": "#/definitions/MyAlias" 25 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-recursive-export/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | alias: MyAlias; 3 | self: MyObject; 4 | } 5 | 6 | export type MyAlias = MyObject; 7 | -------------------------------------------------------------------------------- /test/programs/type-aliases-recursive-export/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyAlias": { 5 | "$ref": "#/definitions/MyObject" 6 | }, 7 | "MyObject": { 8 | "type": "object", 9 | "properties": { 10 | "alias": { 11 | "$ref": "#/definitions/MyAlias" 12 | }, 13 | "self": { 14 | "$ref": "#/definitions/MyObject" 15 | } 16 | }, 17 | "required": [ 18 | "alias", 19 | "self" 20 | ], 21 | "additionalProperties": false 22 | } 23 | }, 24 | "$ref": "#/definitions/MyObject" 25 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-recursive-object-topref/main.ts: -------------------------------------------------------------------------------- 1 | 2 | interface MyObject { 3 | alias: MyAlias; 4 | self: MyObject; 5 | } 6 | 7 | type MyAlias = MyObject; 8 | -------------------------------------------------------------------------------- /test/programs/type-aliases-recursive-object-topref/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$ref": "#/definitions/MyObject", 3 | "$schema": "http://json-schema.org/draft-07/schema#", 4 | "definitions": { 5 | "MyAlias": { 6 | "$ref": "#/definitions/MyObject" 7 | }, 8 | "MyObject": { 9 | "additionalProperties": false, 10 | "properties": { 11 | "alias": { 12 | "$ref": "#/definitions/MyAlias" 13 | }, 14 | "self": { 15 | "$ref": "#/definitions/MyObject" 16 | } 17 | }, 18 | "required": [ 19 | "alias", 20 | "self" 21 | ], 22 | "type": "object" 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /test/programs/type-aliases-tuple-of-variable-length/main.ts: -------------------------------------------------------------------------------- 1 | export type MyTuple = [string, number, boolean?]; 2 | -------------------------------------------------------------------------------- /test/programs/type-aliases-tuple-of-variable-length/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "type": "array", 4 | "items": [ 5 | { 6 | "type": "string" 7 | }, 8 | { 9 | "type": "number" 10 | }, 11 | { 12 | "type": "boolean" 13 | } 14 | ], 15 | "maxItems": 3, 16 | "minItems": 2 17 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-tuple-with-names/main.ts: -------------------------------------------------------------------------------- 1 | export type MyTuple = [a: string, b: 123, c?: boolean, ...d: number[]]; 2 | -------------------------------------------------------------------------------- /test/programs/type-aliases-tuple-with-names/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "type": "array", 4 | "items": [ 5 | { 6 | "title": "a", 7 | "type": "string" 8 | }, 9 | { 10 | "title": "b", 11 | "type": "number", 12 | "const": 123 13 | }, 14 | { 15 | "title": "c", 16 | "type": "boolean" 17 | } 18 | ], 19 | "minItems": 2, 20 | "additionalItems": { 21 | "title": "d", 22 | "type": "number" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /test/programs/type-aliases-tuple-with-rest-element/main.ts: -------------------------------------------------------------------------------- 1 | export type MyTuple = [string, ...number[]]; 2 | -------------------------------------------------------------------------------- /test/programs/type-aliases-tuple-with-rest-element/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "type": "array", 4 | "items": [ 5 | { 6 | "type": "string" 7 | } 8 | ], 9 | "additionalItems": { 10 | "type": "number" 11 | }, 12 | "minItems": 1 13 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-tuple/main.ts: -------------------------------------------------------------------------------- 1 | export type MyTuple = [string, number]; 2 | -------------------------------------------------------------------------------- /test/programs/type-aliases-tuple/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyTuple": { 5 | "type": "array", 6 | "items": [ 7 | { 8 | "type": "string" 9 | }, 10 | { 11 | "type": "number" 12 | } 13 | ], 14 | "additionalItems": { 15 | "anyOf": [ 16 | { 17 | "type": "string" 18 | }, 19 | { 20 | "type": "number" 21 | } 22 | ] 23 | }, 24 | "minItems": 2 25 | } 26 | }, 27 | "$ref": "#/definitions/MyTuple" 28 | } -------------------------------------------------------------------------------- /test/programs/type-aliases-union-namespace/main.ts: -------------------------------------------------------------------------------- 1 | 2 | export namespace Cardinal { 3 | export const NORTH: "north" = "north"; 4 | export const SOUTH: "south" = "south"; 5 | export const EAST: "east" = "east"; 6 | export const WEST: "west" = "west"; 7 | } 8 | 9 | export type Cardinal = typeof Cardinal.NORTH | typeof Cardinal.SOUTH | typeof Cardinal.EAST | typeof Cardinal.WEST; 10 | 11 | export interface MyModel { 12 | direction: Cardinal; 13 | } 14 | -------------------------------------------------------------------------------- /test/programs/type-aliases-union-namespace/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "Cardinal": { 6 | "enum": [ 7 | "east", 8 | "north", 9 | "south", 10 | "west" 11 | ], 12 | "type": "string" 13 | } 14 | }, 15 | "properties": { 16 | "direction": { 17 | "$ref": "#/definitions/Cardinal" 18 | } 19 | }, 20 | "required": [ 21 | "direction" 22 | ], 23 | "type": "object" 24 | } 25 | 26 | -------------------------------------------------------------------------------- /test/programs/type-aliases-union/main.ts: -------------------------------------------------------------------------------- 1 | 2 | type BasicArray = (string | number)[]; 3 | 4 | export interface MyObject { 5 | array: BasicArray; 6 | } 7 | 8 | export type MyUnion = (string | MyObject)[]; 9 | -------------------------------------------------------------------------------- /test/programs/type-aliases-union/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyUnion": { 5 | "type": "array", 6 | "items": { 7 | "anyOf": [ 8 | { 9 | "type": "string" 10 | }, 11 | { 12 | "$ref": "#/definitions/MyObject" 13 | } 14 | ] 15 | } 16 | }, 17 | "MyObject": { 18 | "type": "object", 19 | "properties": { 20 | "array": { 21 | "type": "array", 22 | "items": { 23 | "type": [ 24 | "string", 25 | "number" 26 | ] 27 | } 28 | } 29 | }, 30 | "required": [ 31 | "array" 32 | ], 33 | "additionalProperties": false 34 | } 35 | }, 36 | "$ref": "#/definitions/MyUnion" 37 | } -------------------------------------------------------------------------------- /test/programs/type-aliases/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * My string 3 | */ 4 | type MyString = string; 5 | 6 | /** 7 | * My type alias 8 | */ 9 | type MyAlias = MySubObject; 10 | 11 | /** 12 | * My sub object 13 | */ 14 | interface MySubObject { 15 | propA: number; 16 | propB: number; 17 | } 18 | 19 | /** 20 | * My Object 21 | */ 22 | interface MyObject { 23 | primitive: MyString; 24 | object: MySubObject; 25 | alias: MyAlias; 26 | } 27 | -------------------------------------------------------------------------------- /test/programs/type-aliases/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyAlias": { 6 | "$ref": "#/definitions/MySubObject", 7 | "description": "My sub object" 8 | }, 9 | "MyString": { 10 | "description": "My string", 11 | "type": "string" 12 | }, 13 | "MySubObject": { 14 | "additionalProperties": false, 15 | "description": "My sub object", 16 | "properties": { 17 | "propA": { 18 | "type": "number" 19 | }, 20 | "propB": { 21 | "type": "number" 22 | } 23 | }, 24 | "required": [ 25 | "propA", 26 | "propB" 27 | ], 28 | "type": "object" 29 | } 30 | }, 31 | "description": "My Object", 32 | "properties": { 33 | "alias": { 34 | "$ref": "#/definitions/MyAlias" 35 | }, 36 | "object": { 37 | "$ref": "#/definitions/MySubObject" 38 | }, 39 | "primitive": { 40 | "$ref": "#/definitions/MyString" 41 | } 42 | }, 43 | "required": [ 44 | "alias", 45 | "object", 46 | "primitive" 47 | ], 48 | "type": "object" 49 | } 50 | 51 | -------------------------------------------------------------------------------- /test/programs/type-anonymous/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | FieldWithAnonType: { 3 | SubfieldA: number; 4 | SubfieldB: (string | number); 5 | SubfieldC: { 6 | SubsubfieldA: number[]; 7 | } 8 | }; 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/type-anonymous/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "FieldWithAnonType": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "SubfieldA": { 9 | "type": "number" 10 | }, 11 | "SubfieldB": { 12 | "type": [ 13 | "string", 14 | "number" 15 | ] 16 | }, 17 | "SubfieldC": { 18 | "additionalProperties": false, 19 | "properties": { 20 | "SubsubfieldA": { 21 | "items": { 22 | "type": "number" 23 | }, 24 | "type": "array" 25 | } 26 | }, 27 | "required": [ 28 | "SubsubfieldA" 29 | ], 30 | "type": "object" 31 | } 32 | }, 33 | "required": [ 34 | "SubfieldA", 35 | "SubfieldB", 36 | "SubfieldC" 37 | ], 38 | "type": "object" 39 | } 40 | }, 41 | "required": [ 42 | "FieldWithAnonType" 43 | ], 44 | "type": "object" 45 | } 46 | 47 | -------------------------------------------------------------------------------- /test/programs/type-default-number-as-integer/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | as_integer: number; 3 | 4 | /** @TJS-type number */ 5 | as_number: number; 6 | } 7 | -------------------------------------------------------------------------------- /test/programs/type-default-number-as-integer/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "MyObject": { 5 | "properties": { 6 | "as_integer": { 7 | "type": "integer" 8 | }, 9 | "as_number": { 10 | "type": "number" 11 | } 12 | }, 13 | "type": "object" 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/programs/type-function/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | myFunction: Function; 3 | } -------------------------------------------------------------------------------- /test/programs/type-function/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "Function": { 5 | "description": "Creates a new function.", 6 | "properties": { 7 | "apply": { 8 | "description": "Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function.", 9 | "type": "object" 10 | }, 11 | "arguments": {}, 12 | "bind": { 13 | "description": "For a given function, creates a bound function that has the same body as the original function.\nThe this object of the bound function is associated with the specified object, and has the specified initial parameters.", 14 | "type": "object" 15 | }, 16 | "call": { 17 | "description": "Calls a method of an object, substituting another object for the current object.", 18 | "type": "object" 19 | }, 20 | "caller": { 21 | "$ref": "#/definitions/Function" 22 | }, 23 | "length": { 24 | "type": "number" 25 | }, 26 | "prototype": {}, 27 | "toString": { 28 | "description": "Returns a string representation of a function.", 29 | "type": "object" 30 | } 31 | }, 32 | "required": [ 33 | "apply", 34 | "arguments", 35 | "bind", 36 | "call", 37 | "caller", 38 | "length", 39 | "prototype", 40 | "toString" 41 | ], 42 | "type": "object" 43 | } 44 | }, 45 | "properties": { 46 | "myFunction": { 47 | "$ref": "#/definitions/Function" 48 | } 49 | }, 50 | "required": [ 51 | "myFunction" 52 | ], 53 | "type": "object" 54 | } -------------------------------------------------------------------------------- /test/programs/type-globalThis/main.ts: -------------------------------------------------------------------------------- 1 | export type Test = typeof globalThis; 2 | -------------------------------------------------------------------------------- /test/programs/type-globalThis/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "type": "object" 4 | } 5 | -------------------------------------------------------------------------------- /test/programs/type-intersection-recursive-no-additional/main.ts: -------------------------------------------------------------------------------- 1 | type MyRecursiveNode = { 2 | next?: MyNode; 3 | } 4 | 5 | type MyNode = { 6 | val: string; 7 | } & MyRecursiveNode; 8 | 9 | type MyLinkedList = MyNode; -------------------------------------------------------------------------------- /test/programs/type-intersection-recursive-no-additional/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$ref": "#/definitions/MyNode", 3 | "$schema": "http://json-schema.org/draft-07/schema#", 4 | "definitions": { 5 | "MyNode": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "next": { 9 | "$ref": "#/definitions/MyNode" 10 | }, 11 | "val": { 12 | "type": "string" 13 | } 14 | }, 15 | "required": [ 16 | "val" 17 | ], 18 | "type": "object" 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /test/programs/type-intersection-recursive/main.ts: -------------------------------------------------------------------------------- 1 | interface ChildFoo { 2 | } 3 | 4 | interface Foo { 5 | readonly childFoos: Foo | ChildFoo; 6 | } 7 | -------------------------------------------------------------------------------- /test/programs/type-intersection-recursive/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$ref": "#/definitions/Foo", 3 | "$schema": "http://json-schema.org/draft-07/schema#", 4 | "definitions": { 5 | "ChildFoo": { 6 | "additionalProperties": false, 7 | "type": "object" 8 | }, 9 | "Foo": { 10 | "additionalProperties": false, 11 | "properties": { 12 | "childFoos": { 13 | "anyOf": [ 14 | { 15 | "$ref": "#/definitions/ChildFoo" 16 | }, 17 | { 18 | "$ref": "#/definitions/Foo" 19 | } 20 | ] 21 | } 22 | }, 23 | "required": [ 24 | "childFoos" 25 | ], 26 | "type": "object" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/programs/type-intersection/main.ts: -------------------------------------------------------------------------------- 1 | interface Type1 { 2 | value1: string; 3 | value2: number; 4 | } 5 | interface Type2 { 6 | value2: number; 7 | value3: boolean; 8 | } 9 | 10 | interface MyObject { 11 | value: Type1 & Type2; 12 | } 13 | -------------------------------------------------------------------------------- /test/programs/type-intersection/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "value": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "value1": { 9 | "type": "string" 10 | }, 11 | "value2": { 12 | "type": "number" 13 | }, 14 | "value3": { 15 | "type": "boolean" 16 | } 17 | }, 18 | "required": [ 19 | "value1", 20 | "value2", 21 | "value3" 22 | ], 23 | "type": "object" 24 | } 25 | }, 26 | "required": [ 27 | "value" 28 | ], 29 | "type": "object" 30 | } 31 | 32 | -------------------------------------------------------------------------------- /test/programs/type-literals/main.ts: -------------------------------------------------------------------------------- 1 | type MyObject = { 2 | param1: "1" | "2" | "3"; 3 | param2: "1" | "2" | 3 | true; 4 | /** @enum {string} */ 5 | param3: "1" | "2" | "3"; 6 | /** @enum {unknown} */ 7 | param4: "1" | "2" | 3 | true; 8 | }; 9 | -------------------------------------------------------------------------------- /test/programs/type-literals/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "param1": { 6 | "enum": ["1", "2", "3"], 7 | "type": "string" 8 | }, 9 | "param2": { 10 | "enum": ["1", "2", 3, true] 11 | }, 12 | "param3": { 13 | "enum": ["1", "2", "3"], 14 | "type": "string" 15 | }, 16 | "param4": { 17 | "enum": ["1", "2", 3, true] 18 | } 19 | }, 20 | "required": ["param1", "param2", "param3", "param4"], 21 | "type": "object" 22 | } 23 | -------------------------------------------------------------------------------- /test/programs/type-mapped-types/main.ts: -------------------------------------------------------------------------------- 1 | type Keys = "str1" | "str2"; 2 | 3 | type MyMappedType = { 4 | [key in Keys]: string; 5 | }; 6 | -------------------------------------------------------------------------------- /test/programs/type-mapped-types/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "str1": { 6 | "type": "string" 7 | }, 8 | "str2": { 9 | "type": "string" 10 | } 11 | }, 12 | "required": [ 13 | "str1", 14 | "str2" 15 | ], 16 | "type": "object" 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/type-no-aliases-recursive-topref/main.ts: -------------------------------------------------------------------------------- 1 | 2 | interface MyObject { 3 | alias: MyAlias; 4 | self: MyObject; 5 | } 6 | 7 | type MyAlias = MyObject; 8 | -------------------------------------------------------------------------------- /test/programs/type-no-aliases-recursive-topref/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$ref": "#/definitions/MyObject", 3 | "$schema": "http://json-schema.org/draft-07/schema#", 4 | "definitions": { 5 | "MyObject": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "alias": { 9 | "$ref": "#/definitions/MyObject" 10 | }, 11 | "self": { 12 | "$ref": "#/definitions/MyObject" 13 | } 14 | }, 15 | "required": [ 16 | "alias", 17 | "self" 18 | ], 19 | "type": "object" 20 | } 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /test/programs/type-nullable/main.ts: -------------------------------------------------------------------------------- 1 | 2 | /** @nullable */ 3 | type MyType1 = string; 4 | 5 | /** @nullable */ 6 | type MyType2 = string | number; 7 | 8 | /** @nullable */ 9 | type MyType3 = string | number[]; 10 | 11 | /** @nullable */ 12 | type MyType4 = number[]; 13 | 14 | type Ref = { foo: number }; 15 | 16 | /** @nullable */ 17 | type MyType5 = Ref; 18 | 19 | interface MyType6 {}; 20 | 21 | interface MyObject { 22 | var1: MyType1; 23 | var2: MyType2; 24 | var3: MyType3; 25 | var4: MyType4; 26 | var5: MyType5; 27 | 28 | /** 29 | * @nullable 30 | */ 31 | var6: MyType6; 32 | var7: MyType6; 33 | } 34 | -------------------------------------------------------------------------------- /test/programs/type-nullable/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyType2": { 6 | "type": [ 7 | "string", 8 | "number" 9 | ] 10 | }, 11 | "MyType3": { 12 | "anyOf": [ 13 | { 14 | "items": { 15 | "type": "number" 16 | }, 17 | "type": "array" 18 | }, 19 | { 20 | "type": "string" 21 | } 22 | ] 23 | }, 24 | "MyType6": { 25 | "additionalProperties": false, 26 | "type": "object" 27 | } 28 | }, 29 | "properties": { 30 | "var1": { 31 | "type": [ 32 | "string", 33 | "null" 34 | ] 35 | }, 36 | "var2": { 37 | "anyOf": [ 38 | { 39 | "$ref": "#/definitions/MyType2" 40 | }, 41 | { 42 | "type": "null" 43 | } 44 | ] 45 | }, 46 | "var3": { 47 | "anyOf": [ 48 | { 49 | "$ref": "#/definitions/MyType3" 50 | }, 51 | { 52 | "type": "null" 53 | } 54 | ] 55 | }, 56 | "var4": { 57 | "anyOf": [ 58 | { 59 | "items": { 60 | "type": "number" 61 | }, 62 | "type": "array" 63 | }, 64 | { 65 | "type": "null" 66 | } 67 | ] 68 | }, 69 | "var5": { 70 | "anyOf": [ 71 | { 72 | "properties": { 73 | "foo": { 74 | "type": "number" 75 | } 76 | }, 77 | "additionalProperties": false, 78 | "required": [ 79 | "foo" 80 | ], 81 | "type": "object" 82 | }, 83 | { 84 | "type": "null" 85 | } 86 | ] 87 | }, 88 | "var6": { 89 | "anyOf": [ 90 | { 91 | "$ref": "#/definitions/MyType6" 92 | }, 93 | { 94 | "type": "null" 95 | } 96 | ] 97 | }, 98 | "var7": { 99 | "$ref": "#/definitions/MyType6" 100 | } 101 | }, 102 | "required": [ 103 | "var1", 104 | "var2", 105 | "var3", 106 | "var4", 107 | "var5", 108 | "var6", 109 | "var7" 110 | ], 111 | "type": "object" 112 | } 113 | -------------------------------------------------------------------------------- /test/programs/type-primitives/main.ts: -------------------------------------------------------------------------------- 1 | /* tslint:disable:no-inferrable-types */ 2 | 3 | // Special type, should not appear in the schema 4 | type integer = number; 5 | 6 | class MyObject { 7 | 8 | boolean1: boolean = true; 9 | 10 | number1: number = 1; 11 | 12 | /** @TJS-type integer */ 13 | integer1: number = 1; 14 | integer2: integer = 1; 15 | 16 | string1: string = "defaultValue"; 17 | 18 | array1: Array = null; 19 | array2: Array = null; 20 | 21 | object1: any = null; 22 | object2: {} = null; 23 | object3: object = null; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /test/programs/type-primitives/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "array1": { 6 | "default": null, 7 | "items": { 8 | }, 9 | "type": "array" 10 | }, 11 | "array2": { 12 | "default": null, 13 | "items": { 14 | "type": "number" 15 | }, 16 | "type": "array" 17 | }, 18 | "boolean1": { 19 | "default": true, 20 | "type": "boolean" 21 | }, 22 | "integer1": { 23 | "default": 1, 24 | "type": "integer" 25 | }, 26 | "integer2": { 27 | "default": 1, 28 | "type": "integer" 29 | }, 30 | "number1": { 31 | "default": 1, 32 | "type": "number" 33 | }, 34 | "object1": { 35 | "default": null 36 | }, 37 | "object2": { 38 | "default": null, 39 | "properties": { 40 | }, 41 | "type": "object" 42 | }, 43 | "object3": { 44 | "default": null, 45 | "properties": { 46 | }, 47 | "additionalProperties": true, 48 | "type": "object" 49 | }, 50 | "string1": { 51 | "default": "defaultValue", 52 | "type": "string" 53 | } 54 | }, 55 | "required": [ 56 | "array1", 57 | "array2", 58 | "boolean1", 59 | "integer1", 60 | "integer2", 61 | "number1", 62 | "object1", 63 | "object2", 64 | "object3", 65 | "string1" 66 | ], 67 | "type": "object" 68 | } 69 | 70 | -------------------------------------------------------------------------------- /test/programs/type-recursive/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * A recursive type 3 | */ 4 | export type TestChildren = TestChild | Array; 5 | 6 | interface TestChild { 7 | type: string; 8 | } 9 | -------------------------------------------------------------------------------- /test/programs/type-recursive/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "$ref": "#/definitions/TestChildren", 4 | "definitions": { 5 | "TestChild": { 6 | "additionalProperties": false, 7 | "properties": { 8 | "type": { 9 | "type": "string" 10 | } 11 | }, 12 | "required": ["type"], 13 | "type": "object" 14 | }, 15 | "TestChildren": { 16 | "anyOf": [ 17 | { 18 | "$ref": "#/definitions/TestChild" 19 | }, 20 | { 21 | "items": { 22 | "$ref": "#/definitions/TestChildren" 23 | }, 24 | "type": "array" 25 | } 26 | ], 27 | "description": "A recursive type" 28 | } 29 | }, 30 | "description": "A recursive type" 31 | } 32 | -------------------------------------------------------------------------------- /test/programs/type-union-strict-null-keep-description/main.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Description of InnerObject. 3 | */ 4 | type InnerObject = { 5 | /** 6 | * Description of foo. 7 | */ 8 | foo: string; 9 | }; 10 | 11 | /** 12 | * Description of MyObject. 13 | */ 14 | type MyObject = { 15 | 16 | inner1?: InnerObject; 17 | 18 | /** 19 | * Override description. 20 | */ 21 | inner2?: InnerObject; 22 | }; 23 | -------------------------------------------------------------------------------- /test/programs/type-union-strict-null-keep-description/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "description": "Description of MyObject.", 5 | "properties": { 6 | "inner1": { 7 | "additionalProperties": false, 8 | "description": "Description of InnerObject.", 9 | "properties": { 10 | "foo": { 11 | "description": "Description of foo.", 12 | "type": "string" 13 | } 14 | }, 15 | "required": [ 16 | "foo" 17 | ], 18 | "type": "object" 19 | }, 20 | "inner2": { 21 | "additionalProperties": false, 22 | "description": "Override description.", 23 | "properties": { 24 | "foo": { 25 | "description": "Description of foo.", 26 | "type": "string" 27 | } 28 | }, 29 | "required": [ 30 | "foo" 31 | ], 32 | "type": "object" 33 | } 34 | }, 35 | "type": "object" 36 | } 37 | 38 | -------------------------------------------------------------------------------- /test/programs/type-union-tagged/main.ts: -------------------------------------------------------------------------------- 1 | 2 | interface Square { 3 | kind: "square"; 4 | size: number; 5 | } 6 | 7 | interface Rectangle { 8 | kind: "rectangle"; 9 | width: number; 10 | height: number; 11 | } 12 | 13 | interface Circle { 14 | kind: "circle"; 15 | radius: number; 16 | } 17 | 18 | type Shape = Square | Rectangle | Circle; 19 | -------------------------------------------------------------------------------- /test/programs/type-union-tagged/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "anyOf": [ 4 | { 5 | "$ref": "#/definitions/Square" 6 | }, 7 | { 8 | "$ref": "#/definitions/Rectangle" 9 | }, 10 | { 11 | "$ref": "#/definitions/Circle" 12 | } 13 | ], 14 | "definitions": { 15 | "Circle": { 16 | "additionalProperties": false, 17 | "properties": { 18 | "kind": { 19 | "const": "circle", 20 | "type": "string" 21 | }, 22 | "radius": { 23 | "type": "number" 24 | } 25 | }, 26 | "required": [ 27 | "kind", 28 | "radius" 29 | ], 30 | "type": "object" 31 | }, 32 | "Rectangle": { 33 | "additionalProperties": false, 34 | "properties": { 35 | "height": { 36 | "type": "number" 37 | }, 38 | "kind": { 39 | "const": "rectangle", 40 | "type": "string" 41 | }, 42 | "width": { 43 | "type": "number" 44 | } 45 | }, 46 | "required": [ 47 | "height", 48 | "kind", 49 | "width" 50 | ], 51 | "type": "object" 52 | }, 53 | "Square": { 54 | "additionalProperties": false, 55 | "properties": { 56 | "kind": { 57 | "const": "square", 58 | "type": "string" 59 | }, 60 | "size": { 61 | "type": "number" 62 | } 63 | }, 64 | "required": [ 65 | "kind", 66 | "size" 67 | ], 68 | "type": "object" 69 | } 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /test/programs/type-union/main.ts: -------------------------------------------------------------------------------- 1 | 2 | // Simple union (generates "type": [...]) 3 | type MyType1 = string | number; 4 | 5 | // Non-simple union (generates a "oneOf"/"anyOf") 6 | type MyType2 = string | number[]; 7 | 8 | interface MyObject { 9 | var1: MyType1; 10 | var2: MyType2; 11 | } 12 | -------------------------------------------------------------------------------- /test/programs/type-union/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "definitions": { 5 | "MyType1": { 6 | "type": [ 7 | "string", 8 | "number" 9 | ] 10 | }, 11 | "MyType2": { 12 | "anyOf": [ 13 | { 14 | "type": "array", 15 | "items": { 16 | "type": "number" 17 | } 18 | }, 19 | { 20 | "type": "string" 21 | } 22 | ] 23 | } 24 | }, 25 | "properties": { 26 | "var1": { 27 | "$ref": "#/definitions/MyType1" 28 | }, 29 | "var2": { 30 | "$ref": "#/definitions/MyType2" 31 | } 32 | }, 33 | "required": [ 34 | "var1", 35 | "var2" 36 | ], 37 | "type": "object" 38 | } 39 | 40 | -------------------------------------------------------------------------------- /test/programs/typeof-keyword/main.ts: -------------------------------------------------------------------------------- 1 | interface MyObject { 2 | foo: () => string; 3 | } 4 | -------------------------------------------------------------------------------- /test/programs/typeof-keyword/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "foo": { 6 | "typeof": "function" 7 | } 8 | }, 9 | "required": [ 10 | "foo" 11 | ], 12 | "type": "object" 13 | } 14 | 15 | -------------------------------------------------------------------------------- /test/programs/undefined-property/main.ts: -------------------------------------------------------------------------------- 1 | export type MyObject = { 2 | a: string; 3 | b: undefined; 4 | }; 5 | -------------------------------------------------------------------------------- /test/programs/undefined-property/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "type": "object", 5 | "properties": { 6 | "a": { 7 | "type": "string" 8 | } 9 | }, 10 | "required": [ 11 | "a" 12 | ] 13 | } -------------------------------------------------------------------------------- /test/programs/unique-names-multiple-subdefinitions/main.ts: -------------------------------------------------------------------------------- 1 | import "./other"; 2 | 3 | class SubObject { 4 | is: "SubObject_1"; 5 | } 6 | 7 | class MyObject { 8 | sub: SubObject; 9 | } 10 | -------------------------------------------------------------------------------- /test/programs/unique-names-multiple-subdefinitions/other.ts: -------------------------------------------------------------------------------- 1 | class SubObject { 2 | is: "SubObject_2"; 3 | } 4 | -------------------------------------------------------------------------------- /test/programs/unique-names-multiple-subdefinitions/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "sub": { 6 | "$ref": "#/definitions/SubObject.0eb4e9af" 7 | } 8 | }, 9 | "type": "object", 10 | "required": [ 11 | "sub" 12 | ], 13 | "definitions": { 14 | "SubObject.0eb4e9af": { 15 | "additionalProperties": false, 16 | "properties": { 17 | "is": { 18 | "const": "SubObject_1", 19 | "type": "string" 20 | } 21 | }, 22 | "required": [ 23 | "is" 24 | ], 25 | "type": "object" 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /test/programs/unique-names/main.ts: -------------------------------------------------------------------------------- 1 | import "./other"; 2 | 3 | class MyObject { 4 | is: "MyObject_1"; 5 | } 6 | -------------------------------------------------------------------------------- /test/programs/unique-names/other.ts: -------------------------------------------------------------------------------- 1 | class MyObject { 2 | is: "MyObject_2"; 3 | } 4 | -------------------------------------------------------------------------------- /test/programs/unique-names/schema.MyObject.f2191116.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "object", 3 | "additionalProperties": false, 4 | "properties": { 5 | "is": { 6 | "type": "string", 7 | "const": "MyObject_1" 8 | } 9 | }, 10 | "required": [ 11 | "is" 12 | ], 13 | "$schema": "http://json-schema.org/draft-07/schema#" 14 | } 15 | 16 | -------------------------------------------------------------------------------- /test/programs/unique-names/schema.MyObject.fded1213.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "object", 3 | "additionalProperties": false, 4 | "properties": { 5 | "is": { 6 | "type": "string", 7 | "const": "MyObject_2" 8 | } 9 | }, 10 | "required": [ 11 | "is" 12 | ], 13 | "$schema": "http://json-schema.org/draft-07/schema#" 14 | } 15 | 16 | -------------------------------------------------------------------------------- /test/programs/user-symbols/main.ts: -------------------------------------------------------------------------------- 1 | export interface Context { 2 | ip: string; 3 | } 4 | -------------------------------------------------------------------------------- /test/programs/user-symbols/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "definitions": { 4 | "Context": { 5 | "additionalProperties": false, 6 | "properties": { 7 | "ip": { 8 | "type": "string" 9 | } 10 | }, 11 | "required": [ 12 | "ip" 13 | ], 14 | "type": "object" 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /test/programs/user-validation-keywords/main.ts: -------------------------------------------------------------------------------- 1 | export interface MyObject { 2 | /** 3 | * Must be 'first' or 'last' 4 | * 5 | * @minLength 1 6 | * @chance { 7 | * "pickone": [ [ "first", "last" ] ] 8 | * } 9 | * @ignoreThis 2 10 | * @important 11 | */ 12 | name: string; 13 | } 14 | -------------------------------------------------------------------------------- /test/programs/user-validation-keywords/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-07/schema#", 3 | "additionalProperties": false, 4 | "properties": { 5 | "name": { 6 | "chance": { 7 | "pickone": [ 8 | [ 9 | "first", 10 | "last" 11 | ] 12 | ] 13 | }, 14 | "description": "Must be 'first' or 'last'", 15 | "important": "", 16 | "minLength": 1, 17 | "type": "string" 18 | } 19 | }, 20 | "required": [ 21 | "name" 22 | ], 23 | "type": "object" 24 | } 25 | 26 | -------------------------------------------------------------------------------- /test/require.test.ts: -------------------------------------------------------------------------------- 1 | import { assert } from "chai"; 2 | import { regexRequire } from "../typescript-json-schema"; 3 | 4 | const basicFilePath = "./file.ts"; 5 | const paths = [ 6 | basicFilePath, 7 | ".", 8 | "@some-module", 9 | "@some-module/my_123", 10 | "/some/absolute/path-to-file", 11 | "../relative-path", 12 | "../../../relative-path/to-file.ts", 13 | "./relative-path/myFile123.js", 14 | ]; 15 | 16 | const objName = "objectName"; 17 | const extendedObjName = "$object12_Name"; 18 | 19 | const getValues = (singleQuotation: boolean) => { 20 | const quot = singleQuotation ? "'" : '"'; 21 | return { 22 | path: `${quot}${basicFilePath}${quot}`, 23 | quot, 24 | quotName: singleQuotation ? "single" : "double", 25 | }; 26 | }; 27 | 28 | const matchSimple = ( 29 | match: RegExpExecArray | null, 30 | singleQuotation: boolean, 31 | filePath: string, 32 | propertyName?: string 33 | ) => { 34 | assert.isArray(match); 35 | const quotation = singleQuotation ? "'" : '"'; 36 | const expectedFileName = `${quotation}${filePath}${quotation}`; 37 | assert(match![2] === expectedFileName, `File doesn't match, got: ${match![2]}, expected: ${expectedFileName}`); 38 | assert(match![4] === propertyName, `Poperty has to be ${propertyName?.toString()}`); 39 | }; 40 | 41 | const commonTests = (singleQuotation: boolean) => { 42 | const { quotName, path } = getValues(singleQuotation); 43 | it(`will not match, (${quotName} quotation mark)`, () => { 44 | assert.isNull(regexRequire(`pre require(${path})`)); 45 | assert.isNull(regexRequire(` e require(${path})`)); 46 | assert.isNull(regexRequire(`require(${path})post`)); 47 | assert.isNull(regexRequire(`requir(${path})`)); 48 | assert.isNull(regexRequire(`require(${path}).e-r`)); 49 | assert.isNull(regexRequire(`require(${path}`)); 50 | assert.isNull(regexRequire(`require${path})`)); 51 | assert.isNull(regexRequire(`require[${path}]`)); 52 | assert.isNull(regexRequire(`REQUIRE[${path}]`)); 53 | }); 54 | }; 55 | 56 | const tests = (singleQuotation: boolean, objectName?: string) => { 57 | const { quotName, path, quot } = getValues(singleQuotation); 58 | const objNamePath = objectName ? `.${objectName}` : ""; 59 | it(`basic path (${quotName} quotation mark)`, () => { 60 | matchSimple(regexRequire(`require(${path})${objNamePath}`), singleQuotation, basicFilePath, objectName); 61 | }); 62 | it(`white spaces and basic path (${quotName} quotation mark)`, () => { 63 | matchSimple(regexRequire(` require(${path})${objNamePath}`), singleQuotation, basicFilePath, objectName); 64 | matchSimple(regexRequire(`require(${path})${objNamePath} `), singleQuotation, basicFilePath, objectName); 65 | matchSimple( 66 | regexRequire(` require(${path})${objNamePath} `), 67 | singleQuotation, 68 | basicFilePath, 69 | objectName 70 | ); 71 | matchSimple( 72 | regexRequire(` require(${path})${objNamePath} comment`), 73 | singleQuotation, 74 | basicFilePath, 75 | objectName 76 | ); 77 | matchSimple( 78 | regexRequire(` require(${path})${objNamePath} comment `), 79 | singleQuotation, 80 | basicFilePath, 81 | objectName 82 | ); 83 | }); 84 | it(`paths (${quotName} quotation mark)`, () => { 85 | paths.forEach((pathName) => { 86 | matchSimple( 87 | regexRequire(`require(${quot}${pathName}${quot})${objNamePath}`), 88 | singleQuotation, 89 | pathName, 90 | objectName 91 | ); 92 | }); 93 | }); 94 | }; 95 | 96 | describe("Require regex pattern", () => { 97 | tests(false); 98 | commonTests(false); 99 | tests(true); 100 | commonTests(true); 101 | tests(false, objName); 102 | tests(true, objName); 103 | tests(false, extendedObjName); 104 | tests(true, extendedObjName); 105 | }); 106 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "moduleResolution": "node", 6 | "outDir": "dist", 7 | "isolatedModules": false, 8 | "experimentalDecorators": true, 9 | "emitDecoratorMetadata": true, 10 | "declaration": true, 11 | "noImplicitAny": true, 12 | "strictNullChecks": true, 13 | "noImplicitReturns": true, 14 | "noFallthroughCasesInSwitch": true, 15 | "noUnusedLocals": true, 16 | "noUnusedParameters": true, 17 | "removeComments": true, 18 | "noLib": false, 19 | "preserveConstEnums": true, 20 | "sourceMap": true, 21 | "typeRoots" : ["node_modules/@types"], 22 | }, 23 | "include": [ 24 | "**/*.ts", 25 | "node_modules/@types/**/*.d.ts" 26 | ], 27 | "exclude": [ 28 | "node_modules", 29 | "example", 30 | "test/programs", 31 | "dist" 32 | ], 33 | "compileOnSave": true, 34 | "buildOnSave": false 35 | } 36 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "ban": true, 4 | "class-name": true, 5 | "eofline": true, 6 | "indent": [true, "spaces"], 7 | "comment-format": [true, "check-space"], 8 | "curly": true, 9 | "forin": true, 10 | "label-position": true, 11 | "no-arg": true, 12 | "no-any": false, 13 | "jsdoc-format": true, 14 | "semicolon": [true, "always"], 15 | "no-duplicate-variable": true, 16 | "no-consecutive-blank-lines": [false, 2], 17 | "no-console": [ 18 | true, 19 | "debug", 20 | "info", 21 | "time", 22 | "timeEnd", 23 | "trace" 24 | ], 25 | "no-debugger": true, 26 | "no-eval": true, 27 | "no-inferrable-types": [true, "ignore-params"], 28 | "no-shadowed-variable": true, 29 | "no-string-literal": false, 30 | "no-trailing-whitespace": true, 31 | "no-unused-expression": true, 32 | "one-line": [ 33 | true, 34 | "check-open-brace", 35 | "check-catch", 36 | "check-else", 37 | "check-whitespace" 38 | ], 39 | "typedef-whitespace": [ 40 | true, 41 | { 42 | "call-signature": "nospace", 43 | "index-signature": "nospace", 44 | "parameter": "nospace", 45 | "property-declaration": "nospace", 46 | "variable-declaration": "nospace" 47 | } 48 | ], 49 | "no-switch-case-fall-through": true, 50 | "quotemark": [true, "double", "avoid-escape"], 51 | "triple-equals": [true, "allow-null-check"], 52 | "variable-name": [true, "check-format", "allow-leading-underscore", "ban-keywords"], 53 | "object-literal-key-quotes": [true, "as-needed"] 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /typescript-json-schema-cli.ts: -------------------------------------------------------------------------------- 1 | import { exec, getDefaultArgs } from "./typescript-json-schema"; 2 | 3 | export function run() { 4 | var helpText = "Usage: typescript-json-schema "; 5 | const defaultArgs = getDefaultArgs(); 6 | 7 | // prettier-ignore 8 | var args = require("yargs") 9 | .usage(helpText) 10 | .demand(2) 11 | .boolean("refs").default("refs", defaultArgs.ref) 12 | .describe("refs", "Create shared ref definitions.") 13 | .boolean("aliasRefs").default("aliasRefs", defaultArgs.aliasRef) 14 | .describe("aliasRefs", "Create shared ref definitions for the type aliases.") 15 | .boolean("topRef").default("topRef", defaultArgs.topRef) 16 | .describe("topRef", "Create a top-level ref definition.") 17 | .boolean("titles").default("titles", defaultArgs.titles) 18 | .describe("titles", "Creates titles in the output schema.") 19 | .boolean("defaultProps").default("defaultProps", defaultArgs.defaultProps) 20 | .describe("defaultProps", "Create default properties definitions.") 21 | .boolean("noExtraProps").default("noExtraProps", defaultArgs.noExtraProps) 22 | .describe("noExtraProps", "Disable additional properties in objects by default.") 23 | .boolean("propOrder").default("propOrder", defaultArgs.propOrder) 24 | .describe("propOrder", "Create property order definitions.") 25 | .boolean("useTypeOfKeyword").default("useTypeOfKeyword", defaultArgs.typeOfKeyword) 26 | .describe("useTypeOfKeyword", "Use typeOf keyword (https://goo.gl/DC6sni) for functions.") 27 | .boolean("required").default("required", defaultArgs.required) 28 | .describe("required", "Create required array for non-optional properties.") 29 | .boolean("strictNullChecks").default("strictNullChecks", defaultArgs.strictNullChecks) 30 | .describe("strictNullChecks", "Make values non-nullable by default.") 31 | .boolean("esModuleInterop").default("esModuleInterop", defaultArgs.esModuleInterop) 32 | .describe("esModuleInterop", "Use esModuleInterop when loading typescript modules.") 33 | .boolean("skipLibCheck").default("skipLibCheck", defaultArgs.skipLibCheck) 34 | .describe("skipLibCheck", "Use skipLibCheck when loading typescript modules.") 35 | .boolean("experimentalDecorators").default("experimentalDecorators", defaultArgs.experimentalDecorators) 36 | .describe("skipLibCheck", "Use experimentalDecorators when loading typescript modules.") 37 | .boolean("ignoreErrors").default("ignoreErrors", defaultArgs.ignoreErrors) 38 | .describe("ignoreErrors", "Generate even if the program has errors.") 39 | .alias("out", "o") 40 | .describe("out", "The output file, defaults to using stdout") 41 | .array("validationKeywords").default("validationKeywords", defaultArgs.validationKeywords) 42 | .describe("validationKeywords", "Provide additional validation keywords to include.") 43 | .boolean("excludePrivate").default("excludePrivate", defaultArgs.excludePrivate) 44 | .describe("excludePrivate", "Exclude private members from the schema.") 45 | .boolean("uniqueNames").default("uniqueNames", defaultArgs.uniqueNames) 46 | .describe("uniqueNames", "Use unique names for type symbols.") 47 | .array("include").default("*", defaultArgs.include) 48 | .describe("include", "Further limit tsconfig to include only matching files.") 49 | .boolean("rejectDateType").default("rejectDateType", defaultArgs.rejectDateType) 50 | .describe("rejectDateType", "Rejects Date fields in type definitions.") 51 | .string("id").default("id", defaultArgs.id) 52 | .describe("id", "ID of schema.") 53 | .option("defaultNumberType").choices("defaultNumberType", ["number", "integer"]) 54 | .default("defaultNumberType", defaultArgs.defaultNumberType) 55 | .describe("defaultNumberType", "Default number type.") 56 | .boolean("tsNodeRegister").default("tsNodeRegister", defaultArgs.tsNodeRegister) 57 | .describe("tsNodeRegister", "Use ts-node/register (needed for requiring typescript files).") 58 | .boolean("constAsEnum").default("constAsEnum", defaultArgs.constAsEnum) 59 | .describe("constAsEnum", "Use enums with a single value when declaring constants. Needed for OpenAPI compatibility") 60 | .argv; 61 | 62 | exec(args._[0], args._[1], { 63 | ref: args.refs, 64 | aliasRef: args.aliasRefs, 65 | topRef: args.topRef, 66 | titles: args.titles, 67 | defaultProps: args.defaultProps, 68 | noExtraProps: args.noExtraProps, 69 | propOrder: args.propOrder, 70 | typeOfKeyword: args.useTypeOfKeyword, 71 | required: args.required, 72 | strictNullChecks: args.strictNullChecks, 73 | esModuleInterop: args.esModuleInterop, 74 | skipLibCheck: args.skipLibCheck, 75 | experimentalDecorators: args.experimentalDecorators, 76 | ignoreErrors: args.ignoreErrors, 77 | out: args.out, 78 | validationKeywords: args.validationKeywords, 79 | include: args.include, 80 | excludePrivate: args.excludePrivate, 81 | uniqueNames: args.uniqueNames, 82 | rejectDateType: args.rejectDateType, 83 | id: args.id, 84 | defaultNumberType: args.defaultNumberType, 85 | tsNodeRegister: args.tsNodeRegister, 86 | constAsEnum: args.constAsEnum, 87 | }); 88 | } 89 | 90 | if (typeof window === "undefined" && require.main === module) { 91 | run(); 92 | } 93 | -------------------------------------------------------------------------------- /update-docs.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const fs = require("fs"); 4 | const path = require("path"); 5 | 6 | function getFiles(base) { 7 | const directory = fs.readdirSync(base, { withFileTypes: true }); 8 | const files = directory.map((dir) => { 9 | const res = path.resolve(base, dir.name); 10 | return dir.isDirectory() ? getFiles(res) : res; 11 | }); 12 | return Array.prototype.concat(...files).filter((file) => file.indexOf(".ts") > 0); 13 | } 14 | 15 | const tests = getFiles("./test/programs"); 16 | const ensureTrailingLineBrake = (contents) => (contents[contents.length - 1] === "\n" ? contents : contents + "\n"); 17 | 18 | let source = 19 | "_Auto-generated file. Updated with NPM deploy. Update manually with 'yarn docs'._\n\n" + 20 | "# typescript-json-schema test examples\n\n"; 21 | tests.forEach((file) => { 22 | const contents = fs.readFileSync(file, "utf8"); 23 | const folder = file.split("/").slice(-2)[0]; 24 | source += `## [${folder}](./test/programs/${folder})\n\n\`\`\`ts\n${ensureTrailingLineBrake(contents)}\`\`\`\n\n\n`; 25 | }); 26 | 27 | fs.writeFileSync("./api.md", source); 28 | --------------------------------------------------------------------------------