typescript-json-schema
Advanced tools
Comparing version 0.10.0 to 0.11.0
{ | ||
"name": "typescript-json-schema", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "typescript-json-schema generates JSON Schema files from your Typescript sources", | ||
@@ -44,3 +44,3 @@ "main": "typescript-json-schema.js", | ||
"typescript": "~2.1.5", | ||
"yargs": "^6.6.0" | ||
"yargs": "^7.0.2" | ||
}, | ||
@@ -47,0 +47,0 @@ "devDependencies": { |
@@ -27,13 +27,14 @@ # typescript-json-schema | ||
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] | ||
--useTypeOfKeyword Use `typeOf` keyword (https://goo.gl/DC6sni) for functions. [boolean] [default: false] | ||
--out, -o The output file, defaults to using stdout | ||
--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] | ||
--useTypeOfKeyword Use `typeOf` keyword (https://goo.gl/DC6sni) for functions. [boolean] [default: false] | ||
--out, -o The output file, defaults to using stdout | ||
--validationKeywords Provide additional validation keywords to include [array] [default: []] | ||
``` | ||
@@ -40,0 +41,0 @@ |
@@ -82,12 +82,12 @@ { | ||
"varBoolean", | ||
"varBooleanArray", | ||
"varFloat", | ||
"varFloatArray", | ||
"varInteger", | ||
"varFloat", | ||
"varIntegerArray", | ||
"varMixedArray", | ||
"varString", | ||
"varBooleanArray", | ||
"varIntegerArray", | ||
"varFloatArray", | ||
"varStringArray", | ||
"varMixedArray" | ||
"varStringArray" | ||
], | ||
"type": "object" | ||
} |
@@ -23,4 +23,4 @@ { | ||
"required": [ | ||
"mainWidget", | ||
"name", | ||
"mainWidget", | ||
"otherWidgets" | ||
@@ -27,0 +27,0 @@ ], |
@@ -23,4 +23,4 @@ { | ||
"required": [ | ||
"mainWidget", | ||
"name", | ||
"mainWidget", | ||
"otherWidgets" | ||
@@ -27,0 +27,0 @@ ], |
@@ -37,6 +37,6 @@ { | ||
"valNullable", | ||
"valTrueOrNull", | ||
"valTrue" | ||
"valTrue", | ||
"valTrueOrNull" | ||
], | ||
"$schema": "http://json-schema.org/draft-04/schema#" | ||
} |
@@ -17,6 +17,6 @@ { | ||
"required": [ | ||
"foo", | ||
"bar" | ||
"bar", | ||
"foo" | ||
], | ||
"$schema": "http://json-schema.org/draft-04/schema#" | ||
} |
@@ -12,4 +12,4 @@ { | ||
"required": [ | ||
"foo", | ||
"bar" | ||
"bar", | ||
"foo" | ||
], | ||
@@ -16,0 +16,0 @@ "definitions": { |
@@ -42,7 +42,7 @@ { | ||
"required": [ | ||
"primitive", | ||
"alias", | ||
"object", | ||
"alias" | ||
"primitive" | ||
], | ||
"type": "object" | ||
} |
{ | ||
"type": "object", | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"additionalProperties": false, | ||
"properties": { | ||
"value": { | ||
"allOf": [ | ||
{ | ||
"$ref": "#/definitions/Type1" | ||
}, | ||
{ | ||
"$ref": "#/definitions/Type2" | ||
} | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"value" | ||
], | ||
"definitions": { | ||
"Type1": { | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"value1": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"value1" | ||
] | ||
}, | ||
"Type2": { | ||
"type": "object", | ||
"properties": { | ||
}, | ||
"value2": { | ||
@@ -38,7 +16,12 @@ "type": "number" | ||
"required": [ | ||
"value1", | ||
"value2" | ||
] | ||
], | ||
"type": "object" | ||
} | ||
}, | ||
"$schema": "http://json-schema.org/draft-04/schema#" | ||
} | ||
"required": [ | ||
"value" | ||
], | ||
"type": "object" | ||
} |
@@ -46,13 +46,13 @@ { | ||
"required": [ | ||
"array1", | ||
"array2", | ||
"boolean1", | ||
"number1", | ||
"integer1", | ||
"integer2", | ||
"string1", | ||
"array1", | ||
"array2", | ||
"number1", | ||
"object1", | ||
"object2" | ||
"object2", | ||
"string1" | ||
], | ||
"$schema": "http://json-schema.org/draft-04/schema#" | ||
} |
@@ -49,5 +49,5 @@ { | ||
"required": [ | ||
"height", | ||
"kind", | ||
"width", | ||
"height" | ||
"width" | ||
] | ||
@@ -54,0 +54,0 @@ }, |
@@ -55,4 +55,6 @@ "use strict"; | ||
assertSchema("class-single", "main.ts", "MyObject"); | ||
assertSchema("class-extends", "main.ts", "MyObject"); | ||
assertSchema("interface-single", "main.ts", "MyObject"); | ||
assertSchema("interface-multi", "main.ts", "MyObject"); | ||
assertSchema("interface-extends", "main.ts", "MyObject"); | ||
assertSchema("interface-recursion", "main.ts", "MyObject", { | ||
@@ -103,3 +105,5 @@ useRootRef: true | ||
assertSchema("type-union", "main.ts", "MyObject"); | ||
assertSchema("type-intersection", "main.ts", "MyObject"); | ||
assertSchema("type-intersection", "main.ts", "MyObject", { | ||
disableExtraProperties: true | ||
}); | ||
assertSchema("type-union-tagged", "main.ts", "Shape"); | ||
@@ -112,3 +116,5 @@ assertSchema("type-aliases-union-namespace", "main.ts", "MyModel"); | ||
assertSchema("typeof-keyword", "main.ts", "MyObject", { useTypeOfKeyword: true }); | ||
var userValidationOpts = { validationKeywords: ["chance", "important"] }; | ||
assertSchema("user-validation-keywords", "main.ts", "MyObject", userValidationOpts); | ||
}); | ||
//# sourceMappingURL=schema.test.js.map |
import * as ts from "typescript"; | ||
export declare function getDefaultArgs(): Args; | ||
export declare type ValidationKeywords = { | ||
[prop: string]: boolean; | ||
}; | ||
export declare type Args = { | ||
@@ -16,2 +19,3 @@ useRef: boolean; | ||
out: string; | ||
validationKeywords: string[]; | ||
}; | ||
@@ -54,2 +58,3 @@ export declare type PartialArgs = Partial<Args>; | ||
private reffedDefinitions; | ||
private userValidationKeywords; | ||
constructor(allSymbols: { | ||
@@ -56,0 +61,0 @@ [name: string]: ts.Type; |
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
var ts = require("typescript"); | ||
@@ -22,6 +30,28 @@ var glob = require("glob"); | ||
ignoreErrors: false, | ||
out: "" | ||
out: "", | ||
validationKeywords: [], | ||
}; | ||
} | ||
exports.getDefaultArgs = getDefaultArgs; | ||
function extend(target) { | ||
var _ = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
_[_i - 1] = arguments[_i]; | ||
} | ||
if (target == null) { | ||
throw new TypeError("Cannot convert undefined or null to object"); | ||
} | ||
var to = Object(target); | ||
for (var index = 1; index < arguments.length; index++) { | ||
var nextSource = arguments[index]; | ||
if (nextSource != null) { | ||
for (var nextKey in nextSource) { | ||
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { | ||
to[nextKey] = nextSource[nextKey]; | ||
} | ||
} | ||
} | ||
} | ||
return to; | ||
} | ||
var JsonSchemaGenerator = (function () { | ||
@@ -40,2 +70,6 @@ function JsonSchemaGenerator(allSymbols, userSymbols, inheritingTypes, tc, args) { | ||
this.tc = tc; | ||
this.userValidationKeywords = args.validationKeywords.reduce(function (acc, word) { | ||
return (__assign({}, acc, (_a = {}, _a[word] = true, _a))); | ||
var _a; | ||
}, {}); | ||
} | ||
@@ -69,3 +103,3 @@ Object.defineProperty(JsonSchemaGenerator.prototype, "ReffedDefinitions", { | ||
var _a = (doc.name === "TJS" ? new RegExp(REGEX_TJS_JSDOC).exec(doc.text).slice(1, 3) : [doc.name, doc.text]), name = _a[0], text = _a[1]; | ||
if (JsonSchemaGenerator.validationKeywords[name]) { | ||
if (JsonSchemaGenerator.validationKeywords[name] || _this.userValidationKeywords[name]) { | ||
definition[name] = _this.parseValue(text); | ||
@@ -420,3 +454,3 @@ } | ||
if (requiredProps.length > 0) { | ||
definition.required = requiredProps; | ||
definition.required = requiredProps.sort(); | ||
} | ||
@@ -526,6 +560,16 @@ } | ||
else if (typ.flags & ts.TypeFlags.Intersection) { | ||
definition.allOf = []; | ||
if (this.args.disableExtraProperties) { | ||
definition.additionalProperties = false; | ||
} | ||
var types = typ.types; | ||
for (var i = 0; i < types.length; ++i) { | ||
definition.allOf.push(this.getTypeDefinition(types[i], tc)); | ||
var other = this.getTypeDefinition(types[i], tc, false); | ||
definition.type = other.type; | ||
definition.properties = extend(definition.properties || {}, other.properties); | ||
if (Object.keys(other.default || {}).length > 0) { | ||
definition.default = extend(definition.default || {}, other.default); | ||
} | ||
if (other.required) { | ||
definition.required = (definition.required || []).concat(other.required); | ||
} | ||
} | ||
@@ -766,2 +810,4 @@ } | ||
.describe("out", "The output file, defaults to using stdout") | ||
.array("validationKeywords").default("validationKeywords", defaultArgs.validationKeywords) | ||
.describe("validationKeywords", "Provide additional validation keywords to include.") | ||
.argv; | ||
@@ -780,3 +826,4 @@ exec(args._[0], args._[1], { | ||
ignoreErrors: args.ignoreErrors, | ||
out: args.out | ||
out: args.out, | ||
validationKeywords: args.validationKeywords, | ||
}); | ||
@@ -783,0 +830,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
132025
112
2876
120
+ Addeddefine-properties@1.2.1(transitive)
+ Addedobject.assign@4.1.7(transitive)
+ Addedyargs@7.1.2(transitive)
+ Addedyargs-parser@5.0.1(transitive)
- Removedyargs@6.6.0(transitive)
- Removedyargs-parser@4.2.1(transitive)
Updatedyargs@^7.0.2