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#"
}
网友评论