Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typescript-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-json-schema - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

.gitattributes

28

package.json
{
"name": "typescript-json-schema",
"version": "0.0.4",
"version": "0.0.5",
"description": "typescript-json-schema generates JSON Schema files from your Typescript sources",
"main": "./dist/typescript-json-schema.js",
"main": "typescript-json-schema.js",
"bin": {

@@ -14,7 +14,2 @@ "typescript-json-schema": "./bin/typescript-json-schema"

},
"dependencies": {
"typescript": "~1.7.5",
"glob": "~6.0.2",
"yargs": "^3.30.0"
},
"repository": {

@@ -28,5 +23,2 @@ "type": "git",

}],
"scripts": {
"build": "tsc -p ."
},
"optionalDependencies": {},

@@ -39,3 +31,17 @@ "keywords": [

"schema"
]
],
"dependencies": {
"typescript": "~1.8.0",
"glob": "~7.0.0",
"yargs": "^4.1.0"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"source-map-support": "^0.4.0"
},
"scripts": {
"test": "npm run build && mocha --require source-map-support/register test",
"build": "tsc -p ."
}
}
# typescript-json-schema
[![npm version](https://img.shields.io/npm/v/typescript-json-schema.svg)](https://www.npmjs.com/package/typescript-json-schema)
[![npm version](https://img.shields.io/npm/v/typescript-json-schema.svg)](https://www.npmjs.com/package/typescript-json-schema) [![Build Status](https://travis-ci.org/YousefED/typescript-json-schema.svg?branch=master)](https://travis-ci.org/YousefED/typescript-json-schema)

@@ -5,0 +5,0 @@ Generate json-schemas from your Typescript sources.

@@ -15,3 +15,2 @@ {

"suppressImplicitAnyIndexErrors": true,
"outDir": "dist/",
"sourceMap": true

@@ -25,3 +24,4 @@ },

"node_modules",
"example"
"example",
"test/programs"
],

@@ -31,5 +31,9 @@ "compileOnSave": true,

"files": [
"test/schema.test.ts",
"typescript-json-schema.ts",
"typings/assertion-error/assertion-error.d.ts",
"typings/chai/chai.d.ts",
"typings/glob/glob.d.ts",
"typings/minimatch/minimatch.d.ts",
"typings/mocha/mocha.d.ts",
"typings/node/node.d.ts",

@@ -36,0 +40,0 @@ "typings/typescript/typescript.d.ts"

@@ -5,5 +5,13 @@ import * as ts from "typescript";

var vm = require("vm");
const vm = require("vm");
export module TJS {
const defaultArgs = {
useRef: true,
useRootRef: false,
useTitle: false,
useDefaultProperties: false,
usePropertyOrder: false
};
class JsonSchemaGenerator {

@@ -27,3 +35,3 @@ private static validationKeywords = [

constructor(allSymbols: { [name: string]: ts.Type }, inheritingTypes: { [baseName: string]: string[] }, tc: ts.TypeChecker, private useRef: boolean = false, private useRootRef: boolean = false) {
constructor(allSymbols: { [name: string]: ts.Type }, inheritingTypes: { [baseName: string]: string[] }, tc: ts.TypeChecker, private args = defaultArgs) {
this.allSymbols = allSymbols;

@@ -162,3 +170,5 @@ this.inheritingTypes = inheritingTypes;

let definition: any = this.getDefinitionForType(propertyType, tc);
definition.title = propertyName;
if (this.args.useTitle) {
definition.title = propertyName;
}

@@ -211,3 +221,3 @@ const comments = prop.getDocumentationComment();

private getEnumDefinition(clazzType: ts.Type, tc: ts.TypeChecker, asRef = this.useRef): any {
private getEnumDefinition(clazzType: ts.Type, tc: ts.TypeChecker, asRef = this.args.useRef): any {
const node = clazzType.getSymbol().getDeclarations()[0];

@@ -262,3 +272,3 @@ const fullName = tc.typeToString(clazzType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);

private getClassDefinition(clazzType: ts.Type, tc: ts.TypeChecker, asRef = this.useRef): any {
private getClassDefinition(clazzType: ts.Type, tc: ts.TypeChecker, asRef = this.args.useRef): any {
const node = clazzType.getSymbol().getDeclarations()[0];

@@ -296,10 +306,17 @@ const clazz = <ts.ClassDeclaration>node;

const definition = {
let definition: any = {
type: "object",
title: fullName,
defaultProperties: [], // TODO: set via comment or parameter instead of hardcode here, json-editor specific
properties: propertyDefinitions,
propertyOrder: propertyOrder
properties: propertyDefinitions
};
if (this.args.useTitle) {
definition.title = fullName;
}
if (this.args.useDefaultProperties) {
definition.defaultProperties = [];
}
if (this.args.usePropertyOrder) {
definition.propertyOrder = propertyOrder;
}
if (asRef) {

@@ -317,5 +334,5 @@ this.reffedDefinitions[fullName] = definition;

public getClassDefinitionByName(clazzName: string, includeReffedDefinitions: boolean = true): any {
let def = this.getClassDefinition(this.allSymbols[clazzName], this.tc, this.useRootRef);
let def = this.getClassDefinition(this.allSymbols[clazzName], this.tc, this.args.useRootRef);
if (this.useRef && includeReffedDefinitions) {
if (this.args.useRef && includeReffedDefinitions && Object.keys(this.reffedDefinitions).length > 0) {
def.definitions = this.reffedDefinitions;

@@ -328,3 +345,3 @@ }

export function generateSchema(compileFiles: string[], fullTypeName: string, useRef: boolean, useRootRef: boolean) {
export function generateSchema(compileFiles: string[], fullTypeName: string, args = defaultArgs) {
const options: ts.CompilerOptions = { noEmit: true, emitDecoratorMetadata: true, experimentalDecorators: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS };

@@ -340,3 +357,2 @@ const program = ts.createProgram(compileFiles, options);

if (diagnostics.length == 0) {

@@ -374,3 +390,3 @@

const generator = new JsonSchemaGenerator(allSymbols, inheritingTypes, tc, useRef, useRootRef);
const generator = new JsonSchemaGenerator(allSymbols, inheritingTypes, tc, args);
let definition = generator.getClassDefinitionByName(fullTypeName);

@@ -384,7 +400,6 @@ definition["$schema"] = "http://json-schema.org/draft-04/schema#";

export function exec(filePattern: string, fullTypeName: string, useRef: boolean, useRootRef: boolean) {
export function exec(filePattern: string, fullTypeName: string, args) {
const files: string[] = glob.sync(filePattern);
const definition = TJS.generateSchema(files, fullTypeName, useRef, useRootRef);
console.log(JSON.stringify(definition, null, 4));
//fs.writeFile(outFile, JSON.stringify(definition, null, 4));
const definition = TJS.generateSchema(files, fullTypeName, args);
process.stdout.write(JSON.stringify(definition, null, 4) + "\n");
}

@@ -395,12 +410,24 @@

var args = require('yargs')
var args = require("yargs")
.usage(helpText)
.demand(2)
.boolean('r').alias('r', 'refs').default('r', true)
.describe('r', 'Create shared ref definitions.')
.boolean('t').alias('t', 'topRef').default('t', false)
.describe('t', 'Create a top-level ref definition.')
.boolean("refs").default("refs", defaultArgs.useRef)
.describe("refs", "Create shared ref definitions.")
.boolean("topRef").default("topRef", defaultArgs.useRootRef)
.describe("topRef", "Create a top-level ref definition.")
.boolean("titles").default("titles", defaultArgs.useTitle)
.describe("titles", "Creates titles in the output schema.")
.boolean("defaultProps").default("defaultProps", defaultArgs.useDefaultProperties)
.describe("defaultProps", "Create default properties definitions.")
.boolean("propOrder").default("propOrder", defaultArgs.usePropertyOrder)
.describe("propOrder", "Create property order definitions.")
.argv;
exec(args._[0], args._[1], args.r, args.t);
exec(args._[0], args._[1], {
useRef: args.refs,
useRootRef: args.topRef,
useTitle: args.titles,
useDefaultProperties: args.defaultProps,
usePropertyOrder: args.propOrder
});
}

@@ -410,3 +437,3 @@ }

if (typeof window === "undefined" && require.main === module) {
TJS.run();
TJS.run();
}

@@ -413,0 +440,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc