https://github.com/lbovet/typson
Converts TypeScript to JSON-schema
https://github.com/lbovet/typson
Last synced: 24 days ago
JSON representation
Converts TypeScript to JSON-schema
- Host: GitHub
- URL: https://github.com/lbovet/typson
- Owner: lbovet
- License: apache-2.0
- Created: 2013-11-30T20:31:50.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-11-05T00:51:49.000Z (over 1 year ago)
- Last Synced: 2025-03-29T01:14:11.599Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 613 KB
- Stars: 327
- Watchers: 13
- Forks: 29
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A swisspush project
# TypsonWrite your type definitions in TypeScript and Typson will generate json-schemas.
[See it in action](http://lbovet.github.io/typson-demo/) with its buddy [docson](https://github.com/lbovet/docson)
[](https://travis-ci.org/lbovet/typson)
## Features
* Available as Node.js module
* Can also run as-is in the browser.
* Integrates with Swagger.
* Supports types in multiple files.
* Translates required properties, extends, enums, maps, annotation keywords.## Usage
### Node.js
* Install with `npm install typson -g`
* Generate definitions from a type script: `typson schema example/invoice/line.ts`
* Generate a schema from a type declared in a type script: `typson schema example/invoice/line.ts Invoice`### Browser
```
require(["lib/typson-schema"], function(typson) {
typson.schema("example/invoice/line.ts", "Invoice").done(function(schema) {
console.log(schema);
});
});```
### Swagger
#### Static
Generated definitions are compatible with Swagger, you can copy Typson's output to your API files.#### Dynamic
You can make [Swagger UI](https://github.com/wordnik/swagger-ui) read type definitions directly by integrating Typson, you will need a modified version of [swagger.js](https://github.com/lbovet/swagger-js/tree/model-loader).
This version just adds the capability to load the models from another source.See how it looks like in the [Swagger Typson example](http://lbovet.github.io/swagger-ui/dist/index.html) (Note: this example also illustrate [Docson](https://github.com/lbovet/docson) integration in Swagger).
Then, adapt Swagger UI's `index.html` to
1. Include Typson integration [_after_ the main inline script](https://github.com/lbovet/swagger-ui/blob/716515235a43c37b7d6a0f749570eb17580d3f1a/dist/index.html#L63-L69):
```
requirejs.config({
baseUrl: "/typson"
});
requirejs(["lib/typson-swagger"]);
```
2. Initialize Swagger UI [only once Typson is ready](https://github.com/lbovet/swagger-ui/blob/716515235a43c37b7d6a0f749570eb17580d3f1a/dist/index.html#L30-L31):
```
var typsonReady = $.Deferred();
typsonReady.done(function () {
```
instead of jQuery's `$(function() {` initializer.Then, just replace the `models` section of your API file with a `tsModels` property [containing the URL pointing to the type script defining the models](https://github.com/lbovet/swagger-ui/blob/716515235a43c37b7d6a0f749570eb17580d3f1a/dist/api/test#L65).
## Similar Projects
* https://github.com/ysangkok/typescript-interface-to-jsonschema
* http://blog2.vorburger.ch/2013/11/devoxx-hackathon-2-typescript-ide-w.html[](https://bitdeli.com/free "Bitdeli Badge")