Skip to content

Commit

Permalink
added example
Browse files Browse the repository at this point in the history
  • Loading branch information
YousefED committed Oct 2, 2015
1 parent ad14f1c commit 8a5071e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ typescript-json-schema/dist/typescript-json-schema.js.map
.ntvs_analysis.dat
dist/typescript-json-schema.js.map
typescript-json-schema.sln
typescript-json-schema.sln
local.ts
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ Inspired and builds upon [Typson](https://github.com/lbovet/typson/).

## Features
* Compiles your Typescript program to get complete type information.
* Translates required properties, extends, enums, maps, annotation keywords.
* Translates required properties, extends, annotation keywords, property intializers as defaults.

## Usage

### Node.js

* Install with `npm install typescript-json-schema -g`
* Generate schema from a typescript type: `typescript-json-schema project/directory/**/*.ts fully.qualified.type.to.generate`
* Generate schema from a typescript type: `typescript-json-schema project/directory/**/*.ts fully.qualified.type.to.generate`

## TODO
* better support for maps, enums
19 changes: 9 additions & 10 deletions dist/typescript-json-schema.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions example/invoice/line.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// <reference path="../misc/dimension.ts"/>
/// <reference path="../product.ts"/>

/**
* Represents the document sent to the customer for payment.
*/
interface Invoice {
/**
* Who will pay?
* Not me!
*/
customer: string;

/**
* Invoice content
* @minItems 1
* @maxItems 50
*/
lines: InvoiceLine[];

dimension: Dimension; // Total dimension of the order

blob: any; // Additional stuff
}

interface InvoiceLine {

product: Product;

/**
* @minimum 0
* @exclusiveMinimum true
* @maximum 10
* @exclusiveMaximum false
* @multipleOf 2
*/
quantity: number;
}
11 changes: 11 additions & 0 deletions example/misc/dimension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Typescript-JSON-Schema can handle defaults in classes as illustrated below
class Dimension {
/** Width in cm */
width: number = 10;

/** Height in cm */
height: number = 10;

/** Length in cm */
length: number = 10;
}
28 changes: 28 additions & 0 deletions example/product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path="misc/dimension.ts"/>

interface Product {
/**
* Uniquely defines the product
* @pattern [A-Z][a-z][0-9]_
*/
name: string;

/** How big it is */
dimension?: Dimension;

/** Classification */
category: Category;
}

interface WeightedProduct extends Product {
weight: number;
}

interface Category {
/** Uniquely identifies the category */
name: string;

/** Classification level from 1 to 5 (highest)
* @type integer */
level: number;
}
1 change: 0 additions & 1 deletion typescript-json-schema.njsproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
<!-- Do not delete the following Import Project. While this appears to do nothing it is a marker for setting TypeScript properties before our import that depends on them. -->
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<TypeScriptOutDir>dist</TypeScriptOutDir>
<TypeScriptOutFile>typescript-json-schema.js</TypeScriptOutFile>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="False" />
<Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
Expand Down
20 changes: 8 additions & 12 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,11 @@ export module TJS {

program.getSourceFiles().forEach(sourceFile => {
function inspect(node: ts.Node, tc: ts.TypeChecker) {
if (node.kind == ts.SyntaxKind.ClassDeclaration) {
let clazz = <ts.ClassDeclaration>node;
let clazzType = tc.getTypeAtLocation(clazz);
let fullName = tc.typeToString(clazzType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
allSymbols[fullName] = clazzType;
clazzType.getBaseTypes().forEach(baseType => {
if (node.kind == ts.SyntaxKind.ClassDeclaration || node.kind == ts.SyntaxKind.InterfaceDeclaration) {
let nodeType = tc.getTypeAtLocation(node);
let fullName = tc.typeToString(nodeType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
allSymbols[fullName] = nodeType;
nodeType.getBaseTypes().forEach(baseType => {
let baseName = tc.typeToString(baseType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
if (!inheritingTypes[baseName]) {
inheritingTypes[baseName] = [];
Expand Down Expand Up @@ -288,9 +287,6 @@ if (typeof window === "undefined" && require.main === module) {
}
}


//var files: string[] = glob.sync("C:/Users/Yousef/Documents/Programming/tweetbeam-client/Beam/**/*.ts");
//var outFile = "C:/Users/Yousef/Documents/Programming/JavaWorkspace/TweetBeam/resources/schemas/settings.json";
//var fullTypeName = "beam.Settings";

//node typescript-json-schema.js C:/Users/Yousef/Documents/Programming/tweetbeam-client/Beam/**/*.ts beam.Settings
//TJS.exec("example/**/*.ts", "Invoice");
//node typescript-json-schema.js example/**/*.ts Invoice
debugger;

0 comments on commit 8a5071e

Please sign in to comment.