fast-typescript-to-jsonschema
生成typescript类型的jsonschema数据
特性
- 编译Typescript文件以获取完整的类型信息
- 将所需的属性、继承、注释、属性初始值转换为jsonschema
使用
yarn add fast-typescript-to-jsonschema -D
interface ITest {
attr1: string;
attr2: number;
attr3?: boolean;
}
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);
node ./test.js
jsonSchema
返回结果如下:
{
"additionalProperties": false,
"properties": {
"attr1": {
"type": "string",
},
"attr2": {
"type": "number",
},
"attr3": {
"type": "boolean",
},
},
"required": [
"attr1",
"attr2",
],
"type": "object",
}
注释
示例1
interface Interface_1 {
attr: string;
}
结果:
{
"additionalProperties": false,
"properties": {
"attr": {
"type": "string",
},
},
"required": [
"attr",
],
"type": "object",
}
示例2
interface Interface_4 {
attr: string[];
}
结果:
{
"additionalProperties": false,
"properties": {
"attr": {
"items": {
"type": "string",
},
"type": "array",
},
},
"required": [
"attr",
],
"type": "object",
}
更多支持的类型解析请看,目录如下:
贡献
我们非常欢迎您的贡献,您可以通过以下方式与我们共建。