fast-typescript-to-jsonschema
English | 简体中文
a tool generate json schema from typescript.
Feature
- compile Typescript to get all type information
- convert properties, extends, annotations, and initial values to jsonschema
Usage
- install
yarn add fast-typescript-to-jsonschema -D
- create
type.ts
interface ITest {
attr1: string;
attr2: number;
attr3?: boolean;
}
- create
test.js
const { default: genTypeSchema } = require('fast-typescript-to-jsonschema');
const path = require('path');
const file = path.resolve(__dirname, './type.ts');
genTypeSchema.genJsonDataFormFile(file);
const json = genTypeSchema.genJsonData();
const jsonSchema = genTypeSchema.getJsonSchema(file, 'ITest');
console.log(jsonSchema);
- execute script
node ./test.js
jsonSchema
result:
{
"additionalProperties": false,
"properties": {
"attr1": {
"type": "string",
},
"attr2": {
"type": "number",
},
"attr3": {
"type": "boolean",
},
},
"required": [
"attr1",
"attr2",
],
"type": "object",
}
see more examples at example.
Example1
interface Interface_1 {
attr: string;
}
result:
{
"additionalProperties": false,
"properties": {
"attr": {
"type": "string",
},
},
"required": [
"attr",
],
"type": "object",
}
Example2
interface Interface_4 {
attr: string[];
}
result:
{
"additionalProperties": false,
"properties": {
"attr": {
"items": {
"type": "string",
},
"type": "array",
},
},
"required": [
"attr",
],
"type": "object",
}
Read more supported types here
Contribution
Contributions are extremely welcomed by our team, you can contribute to this repository by several ways below.