美文网首页
typescript-json-schema

typescript-json-schema

作者: antlove | 来源:发表于2020-04-24 10:03 被阅读0次

https://github.com/YousefED/typescript-json-schema

interface.ts
interface XY {
    x: number;
    y: number;
}

interface Shape {
    size: number;
}

interface Circle {
    shape: Shape;
    point: XY;
    radius: number;
}
main.js
const TJS = require('typescript-json-schema');
const path = require('path');
const settings = {
    required: true
};
const compilerOptions = {
    strictNullChecks: true
};
const program = TJS.getProgramFromFiles([path.resolve('interface.ts')], compilerOptions, './');
const shapeSchema = TJS.generateSchema(program, 'Circle', settings);
console.log(JSON.stringify(shapeSchema));
结果
{
    "type": "object",
    "properties": {
        "shape": {
            "$ref": "#/definitions/Shape"
        },
        "point": {
            "$ref": "#/definitions/XY"
        },
        "radius": {
            "type": "number"
        }
    },
    "required": [
        "point",
        "radius",
        "shape"
    ],
    "definitions": {
        "Shape": {
            "type": "object",
            "properties": {
                "size": {
                    "type": "number"
                }
            },
            "required": [
                "size"
            ]
        },
        "XY": {
            "type": "object",
            "properties": {
                "x": {
                    "type": "number"
                },
                "y": {
                    "type": "number"
                }
            },
            "required": [
                "x",
                "y"
            ]
        }
    },
    "$schema": "http://json-schema.org/draft-07/schema#"
}

相关文章

网友评论

      本文标题:typescript-json-schema

      本文链接:https://www.haomeiwen.com/subject/bdsxwhtx.html