typescript-json-schema
Advanced tools
Comparing version 0.42.0 to 0.43.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.run = void 0; | ||
var typescript_json_schema_1 = require("./typescript-json-schema"); | ||
@@ -4,0 +5,0 @@ function run() { |
@@ -77,2 +77,3 @@ import * as ts from "typescript"; | ||
private reffedDefinitions; | ||
private schemaOverrides; | ||
private userValidationKeywords; | ||
@@ -88,3 +89,3 @@ private typeNamesById; | ||
}, tc: ts.TypeChecker, args?: Args); | ||
readonly ReffedDefinitions: { | ||
get ReffedDefinitions(): { | ||
[key: string]: Definition; | ||
@@ -91,0 +92,0 @@ }; |
@@ -14,2 +14,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.exec = exports.programFromConfig = exports.generateSchema = exports.buildGenerator = exports.getProgramFromFiles = exports.JsonSchemaGenerator = exports.getDefaultArgs = void 0; | ||
var glob = require("glob"); | ||
@@ -228,2 +229,3 @@ var stringify = require("json-stable-stringify"); | ||
this.reffedDefinitions = {}; | ||
this.schemaOverrides = new Map(); | ||
this.typeNamesById = {}; | ||
@@ -238,3 +240,3 @@ this.typeIdsByName = {}; | ||
var _a; | ||
return (__assign({}, acc, (_a = {}, _a[word] = true, _a))); | ||
return (__assign(__assign({}, acc), (_a = {}, _a[word] = true, _a))); | ||
}, {}); | ||
@@ -246,3 +248,3 @@ } | ||
}, | ||
enumerable: true, | ||
enumerable: false, | ||
configurable: true | ||
@@ -285,3 +287,3 @@ }); | ||
var v = match[2]; | ||
definition[name] = __assign({}, definition[name], (_a = {}, _a[k] = v ? parseValue(v) : true, _a)); | ||
definition[name] = __assign(__assign({}, definition[name]), (_a = {}, _a[k] = v ? parseValue(v) : true, _a)); | ||
return; | ||
@@ -293,3 +295,3 @@ } | ||
if (parts.length === 2 && subDefinitions[parts[0]]) { | ||
definition[parts[0]] = __assign({}, definition[parts[0]], (_b = {}, _b[parts[1]] = text ? parseValue(text) : true, _b)); | ||
definition[parts[0]] = __assign(__assign({}, definition[parts[0]]), (_b = {}, _b[parts[1]] = text ? parseValue(text) : true, _b)); | ||
} | ||
@@ -311,3 +313,3 @@ } | ||
if (tupleType) { | ||
var elemTypes = tupleType.elementTypes || propertyType.typeArguments; | ||
var elemTypes = propertyType.typeArguments; | ||
var fixedTypes = elemTypes.map(function (elType) { return _this.getTypeDefinition(elType); }); | ||
@@ -798,2 +800,5 @@ definition.type = "array"; | ||
} | ||
else if (reffedType && this.schemaOverrides.has(reffedType.escapedName)) { | ||
fullTypeName = reffedType.escapedName; | ||
} | ||
else { | ||
@@ -881,2 +886,3 @@ fullTypeName = this.getTypeName(typ); | ||
this.reffedDefinitions[symbolName] = schema; | ||
this.schemaOverrides.set(symbolName, schema); | ||
}; | ||
@@ -914,3 +920,3 @@ JsonSchemaGenerator.prototype.getSchemaForSymbol = function (symbolName, includeReffedDefinitions) { | ||
if (this.args.ref && includeReffedDefinitions && Object.keys(this.reffedDefinitions).length > 0) { | ||
root.definitions = __assign({}, root.definitions, this.reffedDefinitions); | ||
root.definitions = __assign(__assign({}, root.definitions), this.reffedDefinitions); | ||
} | ||
@@ -917,0 +923,0 @@ return root; |
{ | ||
"name": "typescript-json-schema", | ||
"version": "0.42.0", | ||
"version": "0.43.0", | ||
"description": "typescript-json-schema generates JSON Schema files from your Typescript sources", | ||
@@ -48,20 +48,20 @@ "main": "dist/typescript-json-schema.js", | ||
"dependencies": { | ||
"@types/json-schema": "^7.0.3", | ||
"glob": "~7.1.4", | ||
"@types/json-schema": "^7.0.5", | ||
"glob": "~7.1.6", | ||
"json-stable-stringify": "^1.0.1", | ||
"typescript": "^3.5.3", | ||
"yargs": "^14.0.0" | ||
"typescript": "~4.0.2", | ||
"yargs": "^15.4.1" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.2.0", | ||
"@types/glob": "^7.1.1", | ||
"@types/chai": "^4.2.12", | ||
"@types/glob": "^7.1.3", | ||
"@types/json-stable-stringify": "^1.0.32", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^12.7.2", | ||
"ajv": "^6.10.2", | ||
"@types/mocha": "^8.0.3", | ||
"@types/node": "^14.6.0", | ||
"ajv": "^6.12.4", | ||
"chai": "^4.2.0", | ||
"mocha": "^6.2.0", | ||
"source-map-support": "^0.5.13", | ||
"ts-node": "^8.3.0", | ||
"tslint": "^5.19.0" | ||
"mocha": "^8.1.1", | ||
"source-map-support": "^0.5.19", | ||
"ts-node": "^9.0.0", | ||
"tslint": "^6.1.3" | ||
}, | ||
@@ -72,2 +72,3 @@ "scripts": { | ||
"debug": "ts-node --inspect=19248 --debug-brk typescript-json-schema-cli.ts", | ||
"docs": "./update-docs.js", | ||
"run": "ts-node typescript-json-schema-cli.ts", | ||
@@ -74,0 +75,0 @@ "build": "tsc -p .", |
139
README.md
@@ -9,4 +9,4 @@ # typescript-json-schema | ||
* Compiles your Typescript program to get complete type information. | ||
* Translates required properties, extends, annotation keywords, property initializers as defaults. You can find examples for these features in the [test examples](https://github.com/YousefED/typescript-json-schema/tree/master/test/programs). | ||
- Compiles your Typescript program to get complete type information. | ||
- 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). | ||
@@ -17,4 +17,4 @@ ## Usage | ||
* Install with `npm install typescript-json-schema -g` | ||
* Generate schema from a typescript type: `typescript-json-schema project/directory/tsconfig.json TYPE` | ||
- Install with `npm install typescript-json-schema -g` | ||
- Generate schema from a typescript type: `typescript-json-schema project/directory/tsconfig.json TYPE` | ||
@@ -26,3 +26,3 @@ To generate files for only _some_ types in `tsconfig.json` specify | ||
* Generate schema from a typescript type: `typescript-json-schema "project/directory/**/*.ts" TYPE` | ||
- Generate schema from a typescript type: `typescript-json-schema "project/directory/**/*.ts" TYPE` | ||
@@ -59,3 +59,3 @@ The `TYPE` can either be a single, fully qualified type or `"*"` to generate the schema for all types. | ||
```ts | ||
import {resolve} from "path"; | ||
import { resolve } from "path"; | ||
@@ -66,3 +66,3 @@ import * as TJS from "typescript-json-schema"; | ||
const settings: TJS.PartialArgs = { | ||
required: true | ||
required: true, | ||
}; | ||
@@ -72,4 +72,4 @@ | ||
const compilerOptions: TJS.CompilerOptions = { | ||
strictNullChecks: true | ||
} | ||
strictNullChecks: true, | ||
}; | ||
@@ -79,3 +79,7 @@ // optionally pass a base path | ||
const program = TJS.getProgramFromFiles([resolve("my-file.ts")], compilerOptions, basePath); | ||
const program = TJS.getProgramFromFiles( | ||
[resolve("my-file.ts")], | ||
compilerOptions, | ||
basePath | ||
); | ||
@@ -85,3 +89,2 @@ // We can either get the schema for one file and one type... | ||
// ... or a generator that lets us incrementally get more schemas | ||
@@ -103,3 +106,3 @@ | ||
const settings: TJS.PartialArgs = { | ||
uniqueNames: true | ||
uniqueNames: true, | ||
}; | ||
@@ -140,9 +143,9 @@ | ||
export interface Shape { | ||
/** | ||
* The size of the shape. | ||
* | ||
* @minimum 0 | ||
* @TJS-type integer | ||
*/ | ||
size: number; | ||
/** | ||
* The size of the shape. | ||
* | ||
* @minimum 0 | ||
* @TJS-type integer | ||
*/ | ||
size: number; | ||
} | ||
@@ -155,16 +158,16 @@ ``` | ||
{ | ||
"$ref": "#/definitions/Shape", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"Shape": { | ||
"properties": { | ||
"size": { | ||
"description": "The size of the shape.", | ||
"minimum": 0, | ||
"type": "integer" | ||
} | ||
}, | ||
"type": "object" | ||
"$ref": "#/definitions/Shape", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"Shape": { | ||
"properties": { | ||
"size": { | ||
"description": "The size of the shape.", | ||
"minimum": 0, | ||
"type": "integer" | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
} | ||
} | ||
@@ -177,3 +180,3 @@ ``` | ||
annotation with the full JSON of the spec (for special cases). This replaces the item types that would | ||
have been inferred from the TypeScript type of the array elements. | ||
have been inferred from the TypeScript type of the array elements. | ||
@@ -184,16 +187,16 @@ Example: | ||
export interface ShapesData { | ||
/** | ||
* Specify individual fields in items. | ||
* | ||
* @items.type integer | ||
* @items.minimum 0 | ||
*/ | ||
sizes: number[]; | ||
/** | ||
* Specify individual fields in items. | ||
* | ||
* @items.type integer | ||
* @items.minimum 0 | ||
*/ | ||
sizes: number[]; | ||
/** | ||
* Or specify a JSON spec: | ||
* | ||
* @items {"type":"string","format":"email"} | ||
*/ | ||
emails: string[]; | ||
/** | ||
* Or specify a JSON spec: | ||
* | ||
* @items {"type":"string","format":"email"} | ||
*/ | ||
emails: string[]; | ||
} | ||
@@ -206,27 +209,27 @@ ``` | ||
{ | ||
"$ref": "#/definitions/ShapesData", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"Shape": { | ||
"properties": { | ||
"sizes": { | ||
"description": "Specify individual fields in items.", | ||
"items": { | ||
"minimum": 0, | ||
"type": "integer" | ||
}, | ||
"type": "array" | ||
}, | ||
"emails": { | ||
"description": "Specify individual fields in items.", | ||
"items": { | ||
"format": "email", | ||
"type": "string" | ||
}, | ||
"type": "array" | ||
} | ||
}, | ||
"type": "object" | ||
"$ref": "#/definitions/ShapesData", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"Shape": { | ||
"properties": { | ||
"sizes": { | ||
"description": "Specify individual fields in items.", | ||
"items": { | ||
"minimum": 0, | ||
"type": "integer" | ||
}, | ||
"type": "array" | ||
}, | ||
"emails": { | ||
"description": "Or specify a JSON spec:", | ||
"items": { | ||
"format": "email", | ||
"type": "string" | ||
}, | ||
"type": "array" | ||
} | ||
}, | ||
"type": "object" | ||
} | ||
} | ||
} | ||
@@ -233,0 +236,0 @@ ``` |
@@ -361,5 +361,15 @@ import * as glob from "glob"; | ||
/** | ||
* This map holds references to all reffed definition., including schema | ||
* overrides and generateddefinitions. | ||
*/ | ||
private reffedDefinitions: { [key: string]: Definition } = {}; | ||
/** | ||
* This map only holds explicit schema overrides. This helps differentiate between | ||
* user defined schema overrides and generated definitions. | ||
*/ | ||
private schemaOverrides = new Map<string, Definition>(); | ||
/** | ||
* This is a set of all the user-defined validation keywords. | ||
@@ -478,3 +488,3 @@ */ | ||
if (tupleType) { // tuple | ||
const elemTypes: ts.NodeArray<ts.TypeNode> = tupleType.elementTypes || (propertyType as any).typeArguments; | ||
const elemTypes: ts.NodeArray<ts.TypeNode> = (propertyType as any).typeArguments; | ||
const fixedTypes = elemTypes.map(elType => this.getTypeDefinition(elType as any)); | ||
@@ -1012,2 +1022,4 @@ definition.type = "array"; | ||
fullTypeName = `${this.getTypeName(typ)}.${generateHashOfNode(getCanonicalDeclaration(sym), relativePath)}`; | ||
} else if (reffedType && this.schemaOverrides.has(reffedType.escapedName as string)) { | ||
fullTypeName = reffedType.escapedName as string; | ||
} else { | ||
@@ -1102,2 +1114,3 @@ fullTypeName = this.getTypeName(typ); | ||
this.reffedDefinitions[symbolName] = schema; | ||
this.schemaOverrides.set(symbolName, schema); | ||
} | ||
@@ -1104,0 +1117,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
201125
235
16
2725
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedcliui@6.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedtypescript@4.0.8(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
+ Addedyargs@15.4.1(transitive)
+ Addedyargs-parser@18.1.3(transitive)
- Removedansi-regex@4.1.1(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedcliui@5.0.0(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedemoji-regex@7.0.3(transitive)
- Removedfind-up@3.0.0(transitive)
- Removedis-fullwidth-code-point@2.0.0(transitive)
- Removedlocate-path@3.0.0(transitive)
- Removedp-locate@3.0.0(transitive)
- Removedpath-exists@3.0.0(transitive)
- Removedstring-width@3.1.0(transitive)
- Removedstrip-ansi@5.2.0(transitive)
- Removedtypescript@3.9.10(transitive)
- Removedwrap-ansi@5.1.0(transitive)
- Removedyargs@14.2.3(transitive)
- Removedyargs-parser@15.0.3(transitive)
Updated@types/json-schema@^7.0.5
Updatedglob@~7.1.6
Updatedtypescript@~4.0.2
Updatedyargs@^15.4.1