在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:YousefED/typescript-json-schema开源软件地址:https://github.com/YousefED/typescript-json-schema开源编程语言:TypeScript 99.0%开源软件介绍:typescript-json-schemaGenerate json-schemas from your Typescript sources. Features
UsageCommand line
To generate files for only some types in In case no
The
Programmatic useimport { resolve } from "path";
import * as TJS from "typescript-json-schema";
// optionally pass argument to schema generator
const settings: TJS.PartialArgs = {
required: true,
};
// optionally pass ts compiler options
const compilerOptions: TJS.CompilerOptions = {
strictNullChecks: true,
};
// optionally pass a base path
const basePath = "./my-dir";
const program = TJS.getProgramFromFiles(
[resolve("my-file.ts")],
compilerOptions,
basePath
);
// We can either get the schema for one file and one type...
const schema = TJS.generateSchema(program, "MyType", settings);
// ... or a generator that lets us incrementally get more schemas
const generator = TJS.buildGenerator(program, settings);
// generator can be also reused to speed up generating the schema if usecase allows:
const schemaWithReusedGenerator = TJS.generateSchema(program, "MyType", settings, [], generator);
// all symbols
const symbols = generator.getUserSymbols();
// Get symbols for different types from generator.
generator.getSchemaForSymbol("MyType");
generator.getSchemaForSymbol("AnotherType"); // In larger projects type names may not be unique,
// while unique names may be enabled.
const settings: TJS.PartialArgs = {
uniqueNames: true,
};
const generator = TJS.buildGenerator(program, settings);
// A list of all types of a given name can then be retrieved.
const symbolList = generator.getSymbols("MyType");
// Choose the appropriate type, and continue with the symbol's unique name.
generator.getSchemaForSymbol(symbolList[1].name);
// Also it is possible to get a list of all symbols.
const fullSymbolList = generator.getSymbols();
type SymbolRef = {
name: string;
typeName: string;
fullyQualifiedName: string;
symbol: ts.Symbol;
};
AnnotationsThe schema generator converts annotations to JSON schema properties. For example export interface Shape {
/**
* The size of the shape.
*
* @minimum 0
* @TJS-type integer
*/
size: number;
} will be translated to {
"$ref": "#/definitions/Shape",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Shape": {
"properties": {
"size": {
"description": "The size of the shape.",
"minimum": 0,
"type": "integer"
}
},
"type": "object"
}
}
} Note that we needed to use You can also override the type of array items, either listing each field in its own annotation or one annotation with the full JSON of the spec (for special cases). This replaces the item types that would have been inferred from the TypeScript type of the array elements. Example: export interface ShapesData {
/**
* Specify individual fields in items.
*
* @items.type integer
* @items.minimum 0
*/
sizes: number[];
/**
* Or specify a JSON spec:
*
* @items {"type":"string","format":"email"}
*/
emails: string[];
} Translation: {
"$ref": "#/definitions/ShapesData",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Shape": {
"properties": {
"sizes": {
"description": "Specify individual fields in items.",
"items": {
"minimum": 0,
"type": "integer"
},
"type": "array"
},
"emails": {
"description": "Or specify a JSON spec:",
"items": {
"format": "email",
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
}
} This same syntax can be used for
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论