ts-json-schema-generator
Generate JSON schema from your Typescript sources
Last updated 15 days ago by domoritz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ts-json-schema-generator 
SYNC missed versions from official npm registry.

ts-json-schema-generator

Test codecov npm dependencies npm version

Extended version of https://github.com/xiag-ag/typescript-to-json-schema.

Inspired by YousefED/typescript-json-schema. Here's the differences list:

  • this implementation avoids the use of typeChecker.getTypeAtLocation() (so probably it keeps correct type aliases)
  • processing AST and formatting JSON schema have been split into two independent steps
  • not exported types, interfaces, enums are not exposed in the definitions section in the JSON schema

Contributors

This project is made possible by a community of contributors. We welcome contributions of any kind (issues, code, documentation, examples, tests,...). Please read our code of conduct.

CLI Usage

npm install --save ts-json-schema-generator
./node_modules/.bin/ts-json-schema-generator --path 'my/project/**.*.ts' --type 'My.Type.Full.Name'

Note that different platforms (e.g. Windows) may different path separators so you may hve to adjust the command above.

Programmatic Usage

// main.js

const tsj = require("ts-json-schema-generator");
const fs = require("fs");

const config = {
    path: "path/to/source/file",
    tsconfig: "path/to/tsconfig.json",
    type: "*", // Or <type-name> if you want to generate schema for that one type only
};

const output_path = "path/to/output/file";

const schema = tsj.createGenerator(config).createSchema(config.type);
const schemaString = JSON.stringify(schema, null, 2);
fs.writeFile(output_path, schemaString, (err) => {
    if (err) throw err;
});

Run the schema generator via node main.js.

Options

-p, --path 'index.ts'
    The path to the TypeScript source file. If this is not provided, the type will be searched in the project specified in the `.tsconfig`.

-t, --type 'My.Type.Full.Name'
    The type the generated schema will represent. If omitted, the generated schema will contain all
    types found in the files matching path. The same is true if '*' is specified.

-e, --expose <all|none|export>
    all: Create shared $ref definitions for all types.
    none: Do not create shared $ref definitions.
    export (default): Create shared $ref definitions only for exported types.

-f, --tsconfig 'my/project/tsconfig.json'
    Use a custom tsconfig file for processing typescript (see https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) instead of the default:
    {
        "compilerOptions": {
            "noEmit": true,
            "emitDecoratorMetadata": true,
            "experimentalDecorators": true,
            "target": "ES5",
            "module": "CommonJS",
            "strictNullChecks": false,
        }
    }

-j, --jsDoc <extended|none|basic>
    none: Do not use JsDoc annotations.
    basic: Read JsDoc annotations to provide schema properties.
    extended (default): Also read @nullable, and @asType annotations.

--unstable
    Do not sort properties.

--strict-tuples
    Do not allow additional items on tuples.

--no-top-ref
    Do not create a top-level $ref definition.

--no-type-check
    Skip type checks for better performance.

--no-ref-encode
    Do not encode references. According to the standard, references must be valid URIs but some tools do not support encoded references.

--validationKeywords
    Provide additional validation keywords to include.

-o, --out
    Specify the output file path. Without this option, the generator logs the response in the console.

--additional-properties <true|false>
    Controls whether or not to allow additional properties for objects that have no index signature.

    true: Additional properties are allowed
    false (default): Additional properties are not allowed

Current state

  • interface types
  • enum types
  • union, tuple, type[] types
  • string, boolean, number types
  • "value", 123, true, false, null, undefined literals
  • type aliases
  • generics
  • typeof
  • keyof
  • conditional types

Run locally

yarn --silent run run --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'

Debug

yarn --silent run debug --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'

And connect via the debugger protocol.

AST Explorer is amazing for developers of this tool!

Current Tags

  • 0.70.2                                ...           latest (15 days ago)

87 Versions

  • 0.70.2                                ...           15 days ago
  • 0.70.1                                ...           a month ago
  • 0.70.0                                ...           a month ago
  • 0.69.0                                ...           a month ago
  • 0.68.1                                ...           2 months ago
  • 0.68.0                                ...           2 months ago
  • 0.67.1                                ...           3 months ago
  • 0.67.0                                ...           3 months ago
  • 0.66.0                                ...           3 months ago
  • 0.65.0                                ...           4 months ago
  • 0.64.0                                ...           4 months ago
  • 0.63.0                                ...           4 months ago
  • 0.62.0                                ...           4 months ago
  • 0.61.1                                ...           4 months ago
  • 0.61.0                                ...           5 months ago
  • 0.60.0                                ...           5 months ago
  • 0.59.0                                ...           5 months ago
  • 0.58.1                                ...           7 months ago
  • 0.58.0                                ...           7 months ago
  • 0.57.0                                ...           7 months ago
  • 0.56.0                                ...           8 months ago
  • 0.55.0                                ...           9 months ago
  • 0.54.0                                ...           9 months ago
  • 0.53.0                                ...           9 months ago
  • 0.52.14                                ...           a year ago
  • 0.52.13                                ...           a year ago
  • 0.32.14                                ...           a year ago
  • 0.52.12                                ...           a year ago
  • 0.52.9                                ...           a year ago
  • 0.52.10                                ...           a year ago
  • 0.52.2                                ...           a year ago
  • 0.51.0                                ...           a year ago
  • 0.50.2                                ...           a year ago
  • 0.50.1                                ...           a year ago
  • 0.50.0                                ...           a year ago
  • 0.49.0                                ...           a year ago
  • 0.48.0                                ...           a year ago
  • 0.47.0                                ...           a year ago
  • 0.46.0                                ...           a year ago
  • 0.45.0                                ...           a year ago
  • 0.44.0                                ...           a year ago
  • 0.43.0                                ...           a year ago
  • 0.42.0                                ...           a year ago
  • 0.41.0                                ...           a year ago
  • 0.40.0                                ...           a year ago
  • 0.39.0                                ...           a year ago
  • 0.38.1                                ...           2 years ago
  • 0.38.0                                ...           2 years ago
  • 0.37.1                                ...           2 years ago
  • 0.37.0                                ...           2 years ago
  • 0.36.3                                ...           2 years ago
  • 0.36.0                                ...           2 years ago
  • 0.35.1                                ...           2 years ago
  • 0.35.0                                ...           2 years ago
  • 0.34.0                                ...           2 years ago
  • 0.33.0                                ...           2 years ago
  • 0.32.0                                ...           2 years ago
  • 0.31.0                                ...           2 years ago
  • 0.30.0                                ...           2 years ago
  • 0.29.0                                ...           2 years ago
  • 0.28.2                                ...           2 years ago
  • 0.28.1                                ...           2 years ago
  • 0.28.0                                ...           2 years ago
  • 0.27.0                                ...           2 years ago
  • 0.26.0                                ...           2 years ago
  • 0.25.1                                ...           2 years ago
  • 0.25.0                                ...           2 years ago
  • 0.24.7                                ...           2 years ago
  • 0.24.6                                ...           2 years ago
  • 0.24.5                                ...           2 years ago
  • 0.24.3                                ...           2 years ago
  • 0.24.2                                ...           2 years ago
  • 0.24.1                                ...           2 years ago
  • 0.23.0                                ...           2 years ago
  • 0.22.0                                ...           2 years ago
  • 0.21.0                                ...           2 years ago
  • 0.20.0                                ...           2 years ago
  • 0.19.0                                ...           2 years ago
  • 0.18.0                                ...           3 years ago
  • 0.16.0                                ...           3 years ago
  • 0.15.0                                ...           3 years ago
  • 0.14.0                                ...           3 years ago
  • 0.13.0                                ...           3 years ago
  • 0.12.0                                ...           3 years ago
  • 0.11.0                                ...           3 years ago
  • 0.10.0                                ...           3 years ago
  • 0.9.0                                ...           3 years ago

Copyright 2014 - 2017 © taobao.org |