What is ts-json-schema-generator?
The ts-json-schema-generator package is a tool that generates JSON schemas from TypeScript types. It is useful for ensuring that your TypeScript types and JSON schemas stay in sync, which can be particularly helpful for validating JSON data against TypeScript interfaces or types.
What are ts-json-schema-generator's main functionalities?
Generate JSON Schema from TypeScript Interface
This feature allows you to generate a JSON schema from a TypeScript interface. You need to specify the path to your TypeScript file and the type you want to generate the schema for.
const { createGenerator } = require('ts-json-schema-generator');
const config = {
path: 'path/to/your/file.ts',
tsconfig: 'path/to/your/tsconfig.json',
type: '*', // Or specify a particular type
};
const schema = createGenerator(config).createSchema(config.type);
console.log(JSON.stringify(schema, null, 2));
Generate JSON Schema for Specific Type
This feature allows you to generate a JSON schema for a specific TypeScript type. You need to specify the path to your TypeScript file and the type you want to generate the schema for.
const { createGenerator } = require('ts-json-schema-generator');
const config = {
path: 'path/to/your/file.ts',
tsconfig: 'path/to/your/tsconfig.json',
type: 'MyType', // Specify the type you want to generate the schema for
};
const schema = createGenerator(config).createSchema(config.type);
console.log(JSON.stringify(schema, null, 2));
Generate JSON Schema with Custom Settings
This feature allows you to generate a JSON schema with custom settings. You can specify various options such as exposing all types, referencing the top-level schema, and using extended JSDoc annotations.
const { createGenerator } = require('ts-json-schema-generator');
const config = {
path: 'path/to/your/file.ts',
tsconfig: 'path/to/your/tsconfig.json',
type: 'MyType',
expose: 'all', // Expose all types
topRef: true, // Reference the top-level schema
jsDoc: 'extended', // Use extended JSDoc annotations
};
const schema = createGenerator(config).createSchema(config.type);
console.log(JSON.stringify(schema, null, 2));
Other packages similar to ts-json-schema-generator
typescript-json-schema
The typescript-json-schema package generates JSON schemas from TypeScript types. It is similar to ts-json-schema-generator but offers different configuration options and may have different performance characteristics.
json-schema-to-typescript
The json-schema-to-typescript package converts JSON schemas to TypeScript interfaces. While it performs the reverse operation of ts-json-schema-generator, it is often used in conjunction with it to ensure consistency between JSON schemas and TypeScript types.
ajv
The ajv package is a JSON schema validator that can be used to validate JSON data against JSON schemas. While it does not generate schemas from TypeScript types, it is often used alongside ts-json-schema-generator to validate data against the generated schemas.
ts-json-schema-generator
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
Run the schema generator with npx:
npx ts-json-schema-generator --path 'my/project/**/*.ts' --type 'My.Type.Name'
Or install the package and then run it
npm install --save ts-json-schema-generator
./node_modules/.bin/ts-json-schema-generator --path 'my/project/**/*.ts' --type 'My.Type.Name'
Note that different platforms (e.g. Windows) may use different path separators so you may have to adjust the command above.
Also note that you need to quote paths with *
as otherwise the shell will expand the paths and therefore only pass the first path to the generator.
Options
-p, --path <path> Source file path
-t, --type <name> Type name
-i, --id <name> $id for generated schema
-f, --tsconfig <path> Custom tsconfig.json path
-e, --expose <expose> Type exposing (choices: "all", "none", "export", default: "export")
-j, --jsDoc <extended> Read JsDoc annotations (choices: "none", "basic", "extended", default: "extended")
--markdown-description Generate `markdownDescription` in addition to `description`.
--functions <functions> How to handle functions. `fail` will throw an error. `comment` will add a comment. `hide` will treat the function like a NeverType or HiddenType.
(choices: "fail", "comment", "hide", default: "comment")
--minify Minify generated schema (default: false)
--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 to improve performance
--no-ref-encode Do not encode references
-o, --out <file> Set the output file (default: stdout)
--validation-keywords [value] Provide additional validation keywords to include (default: [])
--additional-properties Allow additional properties for objects with no index signature (default: false)
-V, --version output the version number
-h, --help display help for command
Programmatic Usage
const tsj = require("ts-json-schema-generator");
const fs = require("fs");
const config = {
path: "path/to/source/file",
tsconfig: "path/to/tsconfig.json",
type: "*",
};
const outputPath = "path/to/output/file";
const schema = tsj.createGenerator(config).createSchema(config.type);
const schemaString = JSON.stringify(schema, null, 2);
fs.writeFile(outputPath, schemaString, (err) => {
if (err) throw err;
});
Run the schema generator via node main.js
.
Custom formatting
Extending the built-in formatting is possible by creating a custom formatter and adding it to the main formatter:
- First we create a formatter, in this case for formatting function types (note that there is a built in one):
import { BaseType, Definition, FunctionType, SubTypeFormatter } from "ts-json-schema-generator";
import ts from "typescript";
export class MyFunctionTypeFormatter implements SubTypeFormatter {
public constructor(private childTypeFormatter: TypeFormatter) {}
public supportsType(type: BaseType): boolean {
return type instanceof FunctionType;
}
public getDefinition(type: FunctionType): Definition {
return {
type: "object",
properties: {
isFunction: {
type: "boolean",
const: true,
},
},
};
}
public getChildren(type: FunctionType): BaseType[] {
return [];
}
public getChildren(type: FunctionType): BaseType[] {
return this.childTypeFormatter.getChildren(type.getType());
}
}
- Then we add the formatter as a child to the core formatter using the augmentation callback:
import { createProgram, createParser, SchemaGenerator, createFormatter } from "ts-json-schema-generator";
import { MyFunctionTypeFormatter } from "./my-function-formatter.ts";
import fs from "fs";
const config = {
path: "path/to/source/file",
tsconfig: "path/to/tsconfig.json",
type: "*",
};
const formatter = createFormatter(config, (fmt, circularReferenceTypeFormatter) => {
fmt.addTypeFormatter(new MyFunctionTypeFormatter());
fmt.addTypeFormatter(new MyFunctionTypeFormatter(circularReferenceTypeFormatter));
});
const program = createProgram(config);
const parser = createParser(program, config);
const generator = new SchemaGenerator(program, parser, formatter, config);
const schema = generator.createSchema(config.type);
const outputPath = "path/to/output/file";
const schemaString = JSON.stringify(schema, null, 2);
fs.writeFile(outputPath, schemaString, (err) => {
if (err) throw err;
});
Custom parsing
Similar to custom formatting, extending the built-in parsing works practically the same way:
- First we create a parser, in this case for parsing construct types:
import { Context, StringType, ReferenceType, BaseType, SubNodeParser } from "ts-json-schema-generator";
import ts from "ts-json-schema-generator";
export class MyConstructorParser implements SubNodeParser {
supportsNode(node: ts.Node): boolean {
return node.kind === ts.SyntaxKind.ConstructorType;
}
createType(node: ts.Node, context: Context, reference?: ReferenceType): BaseType | undefined {
return new StringType();
}
}
- Then we add the parser as a child to the core parser using the augmentation callback:
import { createProgram, createParser, SchemaGenerator, createFormatter } from "ts-json-schema-generator";
import { MyConstructorParser } from "./my-constructor-parser.ts";
import fs from "fs";
const config = {
path: "path/to/source/file",
tsconfig: "path/to/tsconfig.json",
type: "*",
};
const program = createProgram(config);
const parser = createParser(program, config, (prs) => {
prs.addNodeParser(new MyConstructorParser());
});
const formatter = createFormatter(config);
const generator = new SchemaGenerator(program, parser, formatter, config);
const schema = generator.createSchema(config.type);
const outputPath = "path/to/output/file";
const schemaString = JSON.stringify(schema, null, 2);
fs.writeFile(outputPath, schemaString, (err) => {
if (err) throw err;
});
Current state
interface
typesenum
typesunion
, tuple
, type[]
typesDate
, RegExp
, URL
typesstring
, boolean
, number
types"value"
, 123
, true
, false
, null
, undefined
literals- type aliases
- generics
typeof
keyof
- conditional types
- functions
Promise<T>
unwraps to T
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!
Publish
Publishing is handled by a 2-branch pre-release process, configured in publish-auto.yml
. All changes should be based off the default next
branch, and are published automatically.
- PRs made into the default branch are auto-deployed to the
next
pre-release tag on NPM. The result can be installed with npm install ts-json-schema-generator@next
- When merging into
next
, please use the squash and merge
strategy.
- To release a new stable version, open a PR from
next
into stable
using this compare link.
- When merging from
next
into stable
, please use the create a merge commit
strategy.