typescript-json-schema
Advanced tools
Comparing version 0.5.1 to 0.6.0
{ | ||
"name": "typescript-json-schema", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "typescript-json-schema generates JSON Schema files from your Typescript sources", | ||
@@ -35,3 +35,3 @@ "main": "typescript-json-schema.js", | ||
"json-stable-stringify": "^1.0.1", | ||
"typescript": "~2.1.4", | ||
"typescript": "~2.1.5", | ||
"yargs": "^6.6.0" | ||
@@ -44,9 +44,9 @@ }, | ||
"@types/json-stable-stringify": "^1.0.29", | ||
"@types/mocha": "^2.2.37", | ||
"@types/node": "^7.0.0", | ||
"ajv": "^4.10.4", | ||
"@types/mocha": "^2.2.38", | ||
"@types/node": "^7.0.4", | ||
"ajv": "^4.11.2", | ||
"chai": "^3.5.0", | ||
"mocha": "^3.2.0", | ||
"source-map-support": "^0.4.8", | ||
"tslint": "^4.3.1" | ||
"source-map-support": "^0.4.10", | ||
"tslint": "^4.4.2" | ||
}, | ||
@@ -53,0 +53,0 @@ "scripts": { |
@@ -8,2 +8,3 @@ # typescript-json-schema | ||
## Features | ||
* Compiles your Typescript program to get complete type information. | ||
@@ -14,3 +15,3 @@ * Translates required properties, extends, annotation keywords, property initializers as defaults. | ||
### Node.js | ||
### Command line | ||
@@ -24,3 +25,57 @@ * Install with `npm install typescript-json-schema -g` | ||
``` | ||
Usage: node typescript-json-schema.js <path-to-typescript-files-or-tsconfig> <type> | ||
Options: | ||
--refs Create shared ref definitions. [boolean] [default: true] | ||
--aliasRefs Create shared ref definitions for the type aliases. [boolean] [default: false] | ||
--topRef Create a top-level ref definition. [boolean] [default: false] | ||
--titles Creates titles in the output schema. [boolean] [default: false] | ||
--defaultProps Create default properties definitions. [boolean] [default: false] | ||
--noExtraProps Disable additional properties in objects by default. [boolean] [default: false] | ||
--propOrder Create property order definitions. [boolean] [default: false] | ||
--required Create required array for non-optional properties. [boolean] [default: false] | ||
--strictNullChecks Make values non-nullable by default. [boolean] [default: false] | ||
--out, -o The output file, defaults to using stdout | ||
``` | ||
### Programmatic use | ||
```ts | ||
import {resolve} from "path"; | ||
import {CompilerOptions} from "typescript"; | ||
import * as TJS from "typescript-json-schema"; | ||
// optionally pass argument to schema generator | ||
const settings: TJS.PartialArgs = { | ||
generateRequired: true | ||
}; | ||
// optionally pass ts compiler options | ||
compilerOptions: CompilerOptions = { | ||
strictNullChecks: true | ||
} | ||
const program = TJS.getProgramFromFiles([resolve("my-file.ts")], compilerOptions); | ||
// We can either get the schema for one file and one type... | ||
const schema = TJS.generateSchema(program, "MyType", settings); | ||
// ... or a generator that lets us incrementally get more schemas | ||
const generator = TJS.buildGenerator(program, settings); | ||
// all symbols | ||
const symbols = generator.getUserSymbols(); | ||
// Get symbols for different types from generator. | ||
generator.getSchemaForSymbol("MyType"); | ||
generator.getSchemaForSymbol("MyType"); | ||
``` | ||
## Background | ||
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. |
export interface MyObject { | ||
/** | ||
* A name | ||
* A name | ||
*/ | ||
@@ -5,0 +5,0 @@ name?: string; |
/** | ||
/** | ||
* Type-level description | ||
@@ -4,0 +4,0 @@ * @additionalProperties true |
@@ -15,3 +15,3 @@ | ||
* multiple lines | ||
* | ||
* | ||
* @additionalProperties false | ||
@@ -18,0 +18,0 @@ * @unsupportedAnnotationThatShouldBeIgnored |
import { CompilerOptions } from "typescript"; | ||
export declare function assertSchema(group: string, name: string, type: string, settings?: any, compilerOptions?: CompilerOptions): void; | ||
import * as TJS from "../typescript-json-schema"; | ||
export declare function assertSchema(group: string, name: string, type: string, settings?: TJS.PartialArgs, compilerOptions?: CompilerOptions): void; |
@@ -12,8 +12,4 @@ "use strict"; | ||
it(group + " should create correct schema", function () { | ||
var defaults = TJS.getDefaultArgs(); | ||
defaults.generateRequired = true; | ||
for (var pref in defaults) { | ||
if (!(pref in settings)) { | ||
settings[pref] = defaults[pref]; | ||
} | ||
if (!("generateRequired" in settings)) { | ||
settings.generateRequired = true; | ||
} | ||
@@ -30,2 +26,14 @@ var actual = TJS.generateSchema(TJS.getProgramFromFiles([path_1.resolve(base + group + "/" + name)], compilerOptions), type, settings); | ||
exports.assertSchema = assertSchema; | ||
describe("interfaces", function () { | ||
it("should return an instance of JsonSchemaGenerator", function () { | ||
var program = TJS.getProgramFromFiles([path_1.resolve(base + "comments/main.ts")]); | ||
var generator = TJS.buildGenerator(program); | ||
chai_1.assert.instanceOf(generator, TJS.JsonSchemaGenerator); | ||
chai_1.assert.doesNotThrow(function () { return generator.getSchemaForSymbol("MyObject"); }); | ||
chai_1.assert.doesNotThrow(function () { return generator.getSchemaForSymbol("Vector3D"); }); | ||
var symbols = generator.getUserSymbols(); | ||
chai_1.assert(symbols.indexOf("MyObject") > -1); | ||
chai_1.assert(symbols.indexOf("Vector3D") > -1); | ||
}); | ||
}); | ||
describe("schema", function () { | ||
@@ -32,0 +40,0 @@ assertSchema("array-and-description", "main.ts", "MyObject"); |
@@ -13,2 +13,6 @@ { | ||
"strictNullChecks": false, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"removeComments": true, | ||
@@ -15,0 +19,0 @@ "noLib": false, |
@@ -16,3 +16,3 @@ { | ||
"no-duplicate-variable": true, | ||
"no-consecutive-blank-lines": false, | ||
"no-consecutive-blank-lines": [false, 2], | ||
"no-console": [ | ||
@@ -28,3 +28,3 @@ true, | ||
"no-eval": true, | ||
"no-inferrable-types": false, | ||
"no-inferrable-types": [true, "ignore-params"], | ||
"no-shadowed-variable": true, | ||
@@ -31,0 +31,0 @@ "no-string-literal": false, |
import * as ts from "typescript"; | ||
export declare function getDefaultArgs(): { | ||
export declare function getDefaultArgs(): Args; | ||
export declare type Args = { | ||
useRef: boolean; | ||
@@ -14,2 +15,3 @@ useTypeAliasRef: boolean; | ||
}; | ||
export declare type PartialArgs = Partial<Args>; | ||
export declare type Definition = { | ||
@@ -44,2 +46,3 @@ $ref?: string; | ||
private allSymbols; | ||
private userSymbols; | ||
private inheritingTypes; | ||
@@ -50,16 +53,7 @@ private tc; | ||
[name: string]: ts.Type; | ||
}, userSymbols: { | ||
[name: string]: ts.Type; | ||
}, inheritingTypes: { | ||
[baseName: string]: string[]; | ||
}, tc: ts.TypeChecker, args?: { | ||
useRef: boolean; | ||
useTypeAliasRef: boolean; | ||
useRootRef: boolean; | ||
useTitle: boolean; | ||
useDefaultProperties: boolean; | ||
disableExtraProperties: boolean; | ||
usePropertyOrder: boolean; | ||
generateRequired: boolean; | ||
strictNullChecks: boolean; | ||
out: string; | ||
}); | ||
}, tc: ts.TypeChecker, args?: Args); | ||
readonly ReffedDefinitions: { | ||
@@ -83,32 +77,10 @@ [key: string]: Definition; | ||
getSchemaForSymbol(symbolName: string, includeReffedDefinitions?: boolean): Definition; | ||
getSchemaForSymbols(symbols: { | ||
[name: string]: ts.Type; | ||
}): Definition; | ||
getSchemaForSymbols(symbols: string[]): Definition; | ||
getUserSymbols(): string[]; | ||
} | ||
export declare function getProgramFromFiles(files: string[], compilerOptions?: ts.CompilerOptions): ts.Program; | ||
export declare function generateSchema(program: ts.Program, fullTypeName: string, args?: { | ||
useRef: boolean; | ||
useTypeAliasRef: boolean; | ||
useRootRef: boolean; | ||
useTitle: boolean; | ||
useDefaultProperties: boolean; | ||
disableExtraProperties: boolean; | ||
usePropertyOrder: boolean; | ||
generateRequired: boolean; | ||
strictNullChecks: boolean; | ||
out: string; | ||
}): Definition; | ||
export declare function buildGenerator(program: ts.Program, args?: PartialArgs): JsonSchemaGenerator; | ||
export declare function generateSchema(program: ts.Program, fullTypeName: string, args?: PartialArgs): Definition; | ||
export declare function programFromConfig(configFileName: string): ts.Program; | ||
export declare function exec(filePattern: string, fullTypeName: string, args?: { | ||
useRef: boolean; | ||
useTypeAliasRef: boolean; | ||
useRootRef: boolean; | ||
useTitle: boolean; | ||
useDefaultProperties: boolean; | ||
disableExtraProperties: boolean; | ||
usePropertyOrder: boolean; | ||
generateRequired: boolean; | ||
strictNullChecks: boolean; | ||
out: string; | ||
}): void; | ||
export declare function exec(filePattern: string, fullTypeName: string, args?: Args): void; | ||
export declare function run(): void; |
@@ -25,3 +25,3 @@ "use strict"; | ||
var JsonSchemaGenerator = (function () { | ||
function JsonSchemaGenerator(allSymbols, inheritingTypes, tc, args) { | ||
function JsonSchemaGenerator(allSymbols, userSymbols, inheritingTypes, tc, args) { | ||
if (args === void 0) { args = getDefaultArgs(); } | ||
@@ -35,2 +35,3 @@ this.args = args; | ||
this.allSymbols = allSymbols; | ||
this.userSymbols = userSymbols; | ||
this.inheritingTypes = inheritingTypes; | ||
@@ -375,3 +376,2 @@ this.tc = tc; | ||
} | ||
return definition; | ||
} | ||
@@ -383,3 +383,2 @@ else if (modifierFlags & ts.ModifierFlags.Abstract) { | ||
definition.oneOf = oneOf; | ||
return definition; | ||
} | ||
@@ -422,2 +421,3 @@ else { | ||
} | ||
return definition; | ||
}; | ||
@@ -480,3 +480,3 @@ JsonSchemaGenerator.prototype.addSimpleType = function (def, type) { | ||
var unionType = typ; | ||
isStringEnum = (unionType.types.every(function (propType, i, r) { | ||
isStringEnum = (unionType.types.every(function (propType) { | ||
return (propType.getFlags() & ts.TypeFlags.StringLiteral) !== 0; | ||
@@ -567,9 +567,11 @@ })); | ||
}; | ||
for (var id in symbols) { | ||
if (symbols.hasOwnProperty(id)) { | ||
root.definitions[id] = this.getTypeDefinition(symbols[id], this.tc, this.args.useRootRef); | ||
} | ||
for (var i = 0; i < symbols.length; i++) { | ||
var symbol = symbols[i]; | ||
root.definitions[symbol] = this.getTypeDefinition(this.userSymbols[symbol], this.tc, this.args.useRootRef); | ||
} | ||
return root; | ||
}; | ||
JsonSchemaGenerator.prototype.getUserSymbols = function () { | ||
return Object.keys(this.userSymbols); | ||
}; | ||
return JsonSchemaGenerator; | ||
@@ -611,4 +613,10 @@ }()); | ||
exports.getProgramFromFiles = getProgramFromFiles; | ||
function generateSchema(program, fullTypeName, args) { | ||
if (args === void 0) { args = getDefaultArgs(); } | ||
function buildGenerator(program, args) { | ||
if (args === void 0) { args = {}; } | ||
var settings = getDefaultArgs(); | ||
for (var pref in args) { | ||
if (args.hasOwnProperty(pref)) { | ||
settings[pref] = args[pref]; | ||
} | ||
} | ||
var typeChecker = program.getTypeChecker(); | ||
@@ -620,3 +628,3 @@ var diagnostics = ts.getPreEmitDiagnostics(program); | ||
var inheritingTypes_1 = {}; | ||
program.getSourceFiles().forEach(function (sourceFile, sourceFileIdx) { | ||
program.getSourceFiles().forEach(function (sourceFile, _sourceFileIdx) { | ||
function inspect(node, tc) { | ||
@@ -650,11 +658,3 @@ if (node.kind === ts.SyntaxKind.ClassDeclaration | ||
}); | ||
var generator = new JsonSchemaGenerator(allSymbols_1, inheritingTypes_1, typeChecker, args); | ||
var definition = void 0; | ||
if (fullTypeName === "*") { | ||
definition = generator.getSchemaForSymbols(userSymbols_1); | ||
} | ||
else { | ||
definition = generator.getSchemaForSymbol(fullTypeName); | ||
} | ||
return definition; | ||
return new JsonSchemaGenerator(allSymbols_1, userSymbols_1, inheritingTypes_1, typeChecker, settings); | ||
} | ||
@@ -666,10 +666,24 @@ else { | ||
var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; | ||
console.warn(diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); | ||
console.error(diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); | ||
} | ||
else { | ||
console.warn(message); | ||
console.error(message); | ||
} | ||
}); | ||
return null; | ||
} | ||
} | ||
exports.buildGenerator = buildGenerator; | ||
function generateSchema(program, fullTypeName, args) { | ||
if (args === void 0) { args = {}; } | ||
var generator = buildGenerator(program, args); | ||
var definition; | ||
if (fullTypeName === "*") { | ||
definition = generator.getSchemaForSymbols(generator.getUserSymbols()); | ||
} | ||
else { | ||
definition = generator.getSchemaForSymbol(fullTypeName); | ||
} | ||
return definition; | ||
} | ||
exports.generateSchema = generateSchema; | ||
@@ -676,0 +690,0 @@ function programFromConfig(configFileName) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
115839
79
2496
Updatedtypescript@~2.1.5