json-schema-to-typescript
Advanced tools
Comparing version 2.4.0 to 2.4.1
"use strict"; | ||
var prettyPrinter_1 = require('./prettyPrinter'); | ||
var TsTypes_1 = require('./TsTypes'); | ||
@@ -47,5 +46,5 @@ var fs_1 = require('fs'); | ||
var _this = this; | ||
return prettyPrinter_1.format(Array.from(this.namedEnums.values()).concat(Array.from(this.declarations.values())) | ||
return Array.from(this.namedEnums.values()).concat(Array.from(this.declarations.values())) | ||
.map(function (_) { return _.toDeclaration(_this.settings); }) | ||
.join('\n')); | ||
.join('\n'); | ||
}; | ||
@@ -52,0 +51,0 @@ Compiler.prototype.isRequired = function (propertyName, schema) { |
export declare namespace TsType { | ||
interface TsTypeSettings { | ||
declarationDescription?: boolean; | ||
declareReferenced?: boolean; | ||
@@ -8,11 +7,10 @@ declareSimpleType?: boolean; | ||
endTypeWithSemicolon?: boolean; | ||
propertyDescription?: boolean; | ||
useConstEnums?: boolean; | ||
useFullReferencePathAsName?: boolean; | ||
useInterfaceDeclaration?: boolean; | ||
} | ||
var DEFAULT_SETTINGS: TsTypeSettings; | ||
const DEFAULT_SETTINGS: TsTypeSettings; | ||
abstract class TsTypeBase { | ||
id: string; | ||
description?: string; | ||
protected generateComment(string: string): string[]; | ||
protected safeId(): string; | ||
@@ -97,3 +95,3 @@ protected toBlockComment(settings: TsTypeSettings): string; | ||
static reference(id: string): Interface; | ||
protected _type(settings: TsTypeSettings, declaration?: boolean): string; | ||
protected _type(settings: TsTypeSettings): string; | ||
isSimpleType(): boolean; | ||
@@ -100,0 +98,0 @@ toDeclaration(settings: TsTypeSettings): string; |
@@ -8,11 +8,9 @@ "use strict"; | ||
var lodash_1 = require('lodash'); | ||
var multiLineCommentStart = '/** '; | ||
var multiLineCommentIndent = ' * '; | ||
var multiLineCommentEnd = ' */'; | ||
var newLineRegex = /\\n|\n/; | ||
var COMMENT_START = '/**'; | ||
var COMMENT_INDENT = ' * '; | ||
var COMMENT_END = ' */'; | ||
var INDENT_STRING = ' '; | ||
var TsType; | ||
(function (TsType) { | ||
TsType.DEFAULT_SETTINGS = { | ||
declarationDescription: true, | ||
// declareProperties: false, | ||
declareReferenced: true, | ||
@@ -22,6 +20,4 @@ declareSimpleType: false, | ||
endTypeWithSemicolon: true, | ||
propertyDescription: true, | ||
useConstEnums: true, | ||
useFullReferencePathAsName: false, | ||
useInterfaceDeclaration: true | ||
useFullReferencePathAsName: false | ||
}; | ||
@@ -31,2 +27,9 @@ var TsTypeBase = (function () { | ||
} | ||
TsTypeBase.prototype.generateComment = function (string) { | ||
return [ | ||
COMMENT_START | ||
].concat(string.split('\n').map(function (_) { return COMMENT_INDENT + _; }), [ | ||
COMMENT_END | ||
]); | ||
}; | ||
TsTypeBase.prototype.safeId = function () { | ||
@@ -36,8 +39,5 @@ return nameToTsSafeName(this.id); | ||
TsTypeBase.prototype.toBlockComment = function (settings) { | ||
return this.description && settings.declarationDescription ? | ||
"" + (this.description | ||
.split(newLineRegex) | ||
.map(function (line, lineNum) { return (lineNum > 0 ? multiLineCommentIndent : multiLineCommentStart) + line; }) | ||
.join('\n') + multiLineCommentEnd + '\n') : | ||
''; | ||
return this.description && !this.isSimpleType() | ||
? this.generateComment(this.description).join('\n') + "\n" | ||
: ''; | ||
}; | ||
@@ -159,3 +159,3 @@ TsTypeBase.prototype._toDeclaration = function (decl, settings) { | ||
// else declare as identifier | ||
return "" + this.identifier + (this.value ? ('=' + this.value) : ''); | ||
return "" + this.identifier + (this.value ? (' = ' + this.value) : ''); | ||
}; | ||
@@ -183,3 +183,9 @@ EnumValue.prototype.toString = function () { | ||
Enum.prototype.toDeclaration = function (settings) { | ||
return this.toBlockComment(settings) + "export " + (settings.useConstEnums ? 'const ' : '') + "enum " + this.safeId() + "{\n " + this.enumValues.map(function (_) { return _.toDeclaration(); }).join(',\n') + "\n }"; | ||
return this.toBlockComment(settings) | ||
+ ("export " + (settings.useConstEnums ? 'const ' : '') + "enum " + this.safeId() + " {") | ||
+ '\n' | ||
+ INDENT_STRING | ||
+ this.enumValues.map(function (_) { return _.toDeclaration(); }).join(",\n" + INDENT_STRING) | ||
+ '\n' | ||
+ '}'; | ||
}; | ||
@@ -213,3 +219,3 @@ return Enum; | ||
.map(function (_) { return _.toSafeType(settings); }) | ||
.join('&'); | ||
.join(' & '); | ||
}; | ||
@@ -231,3 +237,3 @@ Intersection.prototype.toSafeType = function (settings) { | ||
.map(function (_) { return _.toSafeType(settings); }) | ||
.join('|'); | ||
.join(' | '); | ||
}; | ||
@@ -248,28 +254,14 @@ return Union; | ||
}; | ||
Interface.prototype._type = function (settings, declaration) { | ||
if (declaration === void 0) { declaration = false; } | ||
var id = this.safeId(); | ||
return declaration || !id ? "{\n " + this.props.map(function (_) { | ||
var indentString = ' '; | ||
var decl = indentString + _.name; | ||
if (!_.required) | ||
decl += '?'; | ||
decl += ': ' + _.type.toType(settings); | ||
if (settings.endPropertyWithSemicolon) | ||
decl += ';'; | ||
//All descriptions will be inside jsdoc-style comments to support hinting in editors | ||
//(ie intellisense) | ||
if (settings.propertyDescription && _.type.description && !_.type.id) | ||
decl = _.type.description | ||
.split(newLineRegex) | ||
.map(function (line, lineNum) { return (lineNum > 0 ? multiLineCommentIndent : indentString + multiLineCommentStart) + line; }) | ||
.join('\n' + indentString) + multiLineCommentEnd + '\n' + decl; | ||
return decl; | ||
}).join('\n') + "\n}" : id; | ||
Interface.prototype._type = function (settings) { | ||
var _this = this; | ||
return "{\n" | ||
+ (this.props.map(function (_) { | ||
return ("" + INDENT_STRING + (_.type.description | ||
? _this.generateComment(_.type.description).join("\n" + INDENT_STRING) + ("\n" + INDENT_STRING) | ||
: '') + _.name + (_.required ? '' : '?') + ": " + _.type.toType(settings).replace(/\n/g, '\n' + INDENT_STRING) + (settings.endPropertyWithSemicolon ? ';' : '')); | ||
}).join('\n') + "\n}"); | ||
}; | ||
Interface.prototype.isSimpleType = function () { return false; }; | ||
Interface.prototype.toDeclaration = function (settings) { | ||
if (settings.useInterfaceDeclaration) | ||
return this.toBlockComment(settings) + "export interface " + this.safeId() + " " + this._type(settings, true); | ||
return this._toDeclaration("export type " + this.safeId() + " = " + this._type(settings, true), settings); | ||
return this.toBlockComment(settings) + "export interface " + this.safeId() + " " + this._type(settings); | ||
}; | ||
@@ -276,0 +268,0 @@ return Interface; |
{ | ||
"name": "json-schema-to-typescript", | ||
"version": "2.4.0", | ||
"version": "2.4.1", | ||
"description": "compile json schema to typescript typings", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -44,3 +44,6 @@ # json-schema-to-typescript [![Build Status][build]](https://circleci.com/gh/bcherny/json-schema-to-typescript) [![npm]](https://www.npmjs.com/package/json-schema-to-typescript) [![mit]](https://opensource.org/licenses/MIT) | ||
lastName: string; | ||
age?: number; // Age in years | ||
/** | ||
* Age in years | ||
*/ | ||
age?: number; | ||
hairColor?: "black" | "brown" | "blue"; | ||
@@ -70,3 +73,3 @@ } | ||
- [x] array | ||
- [x] array of type | ||
- [x] homogeneous array | ||
- [x] boolean | ||
@@ -78,4 +81,4 @@ - [x] integer | ||
- [x] string | ||
- [x] string enum | ||
- [x] number enum | ||
- [x] homogeneous enum | ||
- [x] heterogeneous enum | ||
- [x] Non/extensible interfaces | ||
@@ -82,0 +85,0 @@ - [ ] Custom JSON-schema extensions |
import { EnumJSONSchema, JSONSchema, NamedEnumJSONSchema } from './JSONSchema' | ||
import { format } from './prettyPrinter' | ||
import { TsType } from './TsTypes' | ||
@@ -40,4 +39,3 @@ import { readFile, readFileSync } from 'fs' | ||
toString(): string { | ||
return format( | ||
[ | ||
return [ | ||
...Array.from(this.namedEnums.values()), | ||
@@ -48,3 +46,3 @@ ...Array.from(this.declarations.values()) | ||
.join('\n') | ||
) | ||
} | ||
@@ -51,0 +49,0 @@ |
import { camelCase, upperFirst } from 'lodash' | ||
const multiLineCommentStart = '/** ' | ||
const multiLineCommentIndent = ' * ' | ||
const multiLineCommentEnd = ' */' | ||
const newLineRegex = /\\n|\n/ | ||
const COMMENT_START = '/**' | ||
const COMMENT_INDENT = ' * ' | ||
const COMMENT_END = ' */' | ||
const INDENT_STRING = ' ' | ||
@@ -11,4 +11,2 @@ export namespace TsType { | ||
export interface TsTypeSettings { | ||
declarationDescription?: boolean | ||
// TODO declareProperties?: boolean | ||
declareReferenced?: boolean | ||
@@ -18,11 +16,7 @@ declareSimpleType?: boolean | ||
endTypeWithSemicolon?: boolean | ||
propertyDescription?: boolean | ||
useConstEnums?: boolean | ||
useFullReferencePathAsName?: boolean | ||
useInterfaceDeclaration?: boolean | ||
} | ||
export var DEFAULT_SETTINGS: TsTypeSettings = { | ||
declarationDescription: true, | ||
// declareProperties: false, | ||
export const DEFAULT_SETTINGS: TsTypeSettings = { | ||
declareReferenced: true, | ||
@@ -32,6 +26,4 @@ declareSimpleType: false, | ||
endTypeWithSemicolon: true, | ||
propertyDescription: true, | ||
useConstEnums: true, | ||
useFullReferencePathAsName: false, | ||
useInterfaceDeclaration: true | ||
useFullReferencePathAsName: false | ||
} | ||
@@ -43,2 +35,10 @@ | ||
protected generateComment(string: string): string[] { | ||
return [ | ||
COMMENT_START, | ||
...string.split('\n').map(_ => COMMENT_INDENT + _), | ||
COMMENT_END | ||
] | ||
} | ||
protected safeId() { | ||
@@ -48,9 +48,5 @@ return nameToTsSafeName(this.id) | ||
protected toBlockComment(settings: TsTypeSettings) { | ||
return this.description && settings.declarationDescription ? | ||
`${this.description | ||
.split(newLineRegex) | ||
.map((line, lineNum) => (lineNum > 0 ? multiLineCommentIndent : multiLineCommentStart) + line) | ||
.join('\n') + multiLineCommentEnd + '\n' | ||
}` : | ||
'' | ||
return this.description && !this.isSimpleType() | ||
? `${this.generateComment(this.description).join('\n')}\n` | ||
: '' | ||
} | ||
@@ -136,3 +132,3 @@ protected _toDeclaration(decl: string, settings: TsTypeSettings): string { | ||
// else declare as identifier | ||
return `${this.identifier}${this.value ? ('=' + this.value) : ''}` | ||
return `${this.identifier}${this.value ? (' = ' + this.value) : ''}` | ||
} | ||
@@ -157,5 +153,9 @@ | ||
toDeclaration(settings: TsTypeSettings): string { | ||
return `${this.toBlockComment(settings)}export ${settings.useConstEnums ? 'const ' : ''}enum ${this.safeId()}{ | ||
${this.enumValues.map(_ => _.toDeclaration()).join(',\n')} | ||
}` | ||
return this.toBlockComment(settings) | ||
+ `export ${settings.useConstEnums ? 'const ' : ''}enum ${this.safeId()} {` | ||
+ '\n' | ||
+ INDENT_STRING | ||
+ this.enumValues.map(_ => _.toDeclaration()).join(`,\n${INDENT_STRING}`) | ||
+ '\n' | ||
+ '}' | ||
} | ||
@@ -180,3 +180,3 @@ } | ||
.map(_ => _.toSafeType(settings)) | ||
.join('&') | ||
.join(' & ') | ||
} | ||
@@ -192,3 +192,3 @@ toSafeType(settings: TsTypeSettings) { | ||
.map(_ => _.toSafeType(settings)) | ||
.join('|') | ||
.join(' | ') | ||
} | ||
@@ -206,32 +206,19 @@ } | ||
} | ||
protected _type(settings: TsTypeSettings, declaration: boolean = false) { | ||
let id = this.safeId() | ||
return declaration || !id ? `{ | ||
${this.props.map(_ => { | ||
const indentString = ' ' | ||
let decl = indentString + _.name | ||
if (!_.required) | ||
decl += '?' | ||
decl += ': ' + _.type.toType(settings) | ||
if (settings.endPropertyWithSemicolon) | ||
decl += ';' | ||
//All descriptions will be inside jsdoc-style comments to support hinting in editors | ||
//(ie intellisense) | ||
if (settings.propertyDescription && _.type.description && !_.type.id) | ||
decl = _.type.description | ||
.split(newLineRegex) | ||
.map((line, lineNum) => (lineNum > 0 ? multiLineCommentIndent : indentString + multiLineCommentStart) + line) | ||
.join('\n' + indentString) + multiLineCommentEnd + '\n' + decl | ||
return decl | ||
}).join('\n')} | ||
}` : id | ||
protected _type(settings: TsTypeSettings) { | ||
return `{\n` | ||
+ `${this.props.map(_ => | ||
`${INDENT_STRING}${_.type.description | ||
? this.generateComment(_.type.description).join(`\n${INDENT_STRING}`) + `\n${INDENT_STRING}` | ||
: '' | ||
}${_.name}${_.required ? '' : '?'}: ${ | ||
_.type.toType(settings).replace(/\n/g, '\n' + INDENT_STRING) | ||
}${ | ||
settings.endPropertyWithSemicolon ? ';' : '' | ||
}` | ||
).join('\n')} | ||
}` | ||
} | ||
isSimpleType() { return false } | ||
toDeclaration(settings: TsTypeSettings): string { | ||
if (settings.useInterfaceDeclaration) | ||
return `${this.toBlockComment(settings)}export interface ${this.safeId()} ${this._type(settings, true)}` | ||
return this._toDeclaration(`export type ${this.safeId()} = ${this._type(settings, true)}`, settings) | ||
return `${this.toBlockComment(settings)}export interface ${this.safeId()} ${this._type(settings)}` | ||
} | ||
@@ -238,0 +225,0 @@ } |
@@ -31,9 +31,8 @@ export var schema = { | ||
{ | ||
settings: { | ||
useInterfaceDeclaration: true | ||
}, | ||
types: `export interface ExampleSchema { | ||
firstName: string; | ||
lastName: string; | ||
/** Age in years */ | ||
/** | ||
* Age in years | ||
*/ | ||
age?: number; | ||
@@ -45,17 +44,3 @@ height?: number; | ||
}` | ||
}, | ||
{ | ||
settings: { | ||
propertyDescription: false | ||
}, | ||
types: `export interface ExampleSchema { | ||
firstName: string; | ||
lastName: string; | ||
age?: number; | ||
height?: number; | ||
favoriteFoods?: any[]; | ||
likesDogs?: boolean; | ||
[k: string]: any; | ||
}` | ||
} | ||
] |
@@ -16,3 +16,5 @@ export var schema = { | ||
export var types = `/** My cool schema */ | ||
export var types = `/** | ||
* My cool schema | ||
*/ | ||
export interface ExampleSchema { | ||
@@ -19,0 +21,0 @@ value: number | string; |
@@ -162,3 +162,5 @@ export var schema = { | ||
export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string"; | ||
/** Core schema meta-schema */ | ||
/** | ||
* Core schema meta-schema | ||
*/ | ||
export interface HttpJsonSchemaOrgDraft04Schema { | ||
@@ -213,3 +215,5 @@ id?: string; | ||
types:`export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string"; | ||
/** Core schema meta-schema */ | ||
/** | ||
* Core schema meta-schema | ||
*/ | ||
export interface HttpJsonSchemaOrgDraft04Schema { | ||
@@ -216,0 +220,0 @@ id?: string; |
@@ -17,9 +17,13 @@ export var schema = { | ||
export var types = `/** Array of authorized user ids. */ | ||
export type UserIdArray = string[]; | ||
/** My cool schema */ | ||
// TODO: 2nd block comment should annotate UserIdArray, not users | ||
export var types = `export type UserIdArray = string[]; | ||
/** | ||
* My cool schema | ||
*/ | ||
export interface ExampleSchema { | ||
/** | ||
* Array of authorized user ids. | ||
*/ | ||
users?: UserIdArray; | ||
[k: string]: any; | ||
}` | ||
@@ -24,4 +24,6 @@ export var schema = { | ||
lastName: string; | ||
/** Age in years */ | ||
/** | ||
* Age in years | ||
*/ | ||
age?: number; | ||
}` |
@@ -22,3 +22,5 @@ export var schema = | ||
lastName: string; | ||
/** Age in years */ | ||
/** | ||
* Age in years | ||
*/ | ||
age?: number; | ||
@@ -25,0 +27,0 @@ height?: number; |
@@ -22,11 +22,17 @@ export var schema = { | ||
export var types = `/** My cool schema */ | ||
export var types = `/** | ||
* My cool schema | ||
*/ | ||
export interface ExampleSchema { | ||
/** first name single line description */ | ||
/** | ||
* first name single line description | ||
*/ | ||
firstName: string; | ||
lastName: string; | ||
/** Age description with | ||
* multiple lines */ | ||
/** | ||
* Age description with | ||
* multiple lines | ||
*/ | ||
age?: number; | ||
[k: string]: any; | ||
}` |
@@ -21,9 +21,13 @@ export var schema = { | ||
export var types = `/** My cool schema */ | ||
export var types = `/** | ||
* My cool schema | ||
*/ | ||
export interface ExampleSchema { | ||
firstName: string; | ||
lastName: string; | ||
/** Age in years */ | ||
/** | ||
* Age in years | ||
*/ | ||
age?: number; | ||
[k: string]: any; | ||
}` |
import { compile } from '../src/index' | ||
import { JSONSchema } from '../src/JSONSchema' | ||
import { TsType } from '../src/TsTypes' | ||
import test from 'ava' | ||
import { find } from 'lodash' | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
var modules = new Map<string, any>() | ||
var dir = __dirname + '/cases' | ||
fs.readdirSync(dir).forEach(function(moduleName) { | ||
if (!/^.*\.js$/.test(moduleName)) | ||
return | ||
var p = path.join(dir, moduleName) | ||
try { | ||
let module = require(p) | ||
modules.set(moduleName, module) | ||
} catch (e) { | ||
console.error('Unable to load module', moduleName, e) | ||
} | ||
}) | ||
modules.forEach((exports, name) => { | ||
const dir = __dirname + '/cases' | ||
const modules = fs.readdirSync(dir) | ||
.filter(_ => /^.*\.js$/.test(_)) | ||
.map(_ => [_, require(path.join(dir, _))]) as [string, TestCase][] | ||
const only = find(modules, _ => _[1].only) | ||
if (only) { | ||
run(only[1], only[0]) | ||
} else { | ||
modules.forEach(_ => run(_[1], _[0])) | ||
} | ||
interface TestCase { | ||
configurations?: { settings: TsType.TsTypeSettings, types: string }[] | ||
error?: { type: ErrorConstructor } | ||
schema: JSONSchema | ||
settings?: TsType.TsTypeSettings | ||
types?: string | ||
only?: boolean | ||
} | ||
function run(exports: TestCase, name: string) { | ||
if (exports.configurations) { | ||
@@ -36,2 +48,2 @@ exports.configurations.forEach((cfg: any) => { | ||
} | ||
}) | ||
} |
@@ -12,3 +12,3 @@ { | ||
"lib": [ | ||
"es6" | ||
"es2015" | ||
], | ||
@@ -15,0 +15,0 @@ "strictNullChecks": true, |
@@ -12,3 +12,3 @@ { | ||
"lib": [ | ||
"es6" | ||
"es2015" | ||
], | ||
@@ -15,0 +15,0 @@ "strictNullChecks": true, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
124
79209
41
2332