https://github.com/xiag-ag/typescript-to-json-schema 的擴充版本。
受到YousefED/typescript-json-schema
的啟發。以下是差異列表:
typeChecker.getTypeAtLocation()
(因此可能保留正確的類型別名)definitions
部分中公開該項目是由貢獻者社群實現的。我們歡迎任何類型的貢獻(問題、程式碼、文件、範例、測試…)。請閱讀我們的行為準則。
使用 npx 運行架構產生器:
npx ts-json-schema-generator --path ' my/project/**/*.ts ' --type ' My.Type.Name '
或安裝套件然後運行
npm install --save ts-json-schema-generator
./node_modules/.bin/ts-json-schema-generator --path ' my/project/**/*.ts ' --type ' My.Type.Name '
請注意,不同的平台(例如Windows)可能會使用不同的路徑分隔符,因此您可能需要調整上面的命令。
另請注意,您需要用*
引用路徑,否則 shell 將擴展路徑,因此僅將第一個路徑傳遞給生成器。
預設情況下,命令列產生器將使用目前工作目錄中的tsconfig.json
文件,或使用包含tsconfig.json
文件的第一個父目錄,直到檔案系統的根目錄。如果您想使用不同的tsconfig.json
文件,可以使用--tsconfig
選項。特別是,如果您需要對類型使用不同的編譯選項,您可能只想為架構生成建立一個單獨的tsconfig.json
檔案。
-p, --path <path> Source file path
-t, --type <name> Type name
-i, --id <name> $id for generated schema
-f, --tsconfig <path> Custom tsconfig.json path
-e, --expose <expose> Type exposing (choices: "all", "none", "export", default: "export")
-j, --jsDoc <extended> Read JsDoc annotations (choices: "none", "basic", "extended", default: "extended")
--markdown-description Generate `markdownDescription` in addition to `description`.
--functions <functions> How to handle functions. `fail` will throw an error. `comment` will add a comment. `hide` will treat the function like a NeverType or HiddenType.
(choices: "fail", "comment", "hide", default: "comment")
--minify Minify generated schema (default: false)
--unstable Do not sort properties
--strict-tuples Do not allow additional items on tuples
--no-top-ref Do not create a top-level $ref definition
--no-type-check Skip type checks to improve performance
--no-ref-encode Do not encode references
-o, --out <file> Set the output file (default: stdout)
--validation-keywords [value] Provide additional validation keywords to include (default: [])
--additional-properties Allow additional properties for objects with no index signature (default: false)
-V, --version output the version number
-h, --help display help for command
// main.js
const tsj = require ( "ts-json-schema-generator" ) ;
const fs = require ( "fs" ) ;
/** @type {import('ts-json-schema-generator/dist/src/Config').Config} */
const config = {
path : "path/to/source/file" ,
tsconfig : "path/to/tsconfig.json" ,
type : "*" , // Or <type-name> if you want to generate schema for that one type only
} ;
const outputPath = "path/to/output/file" ;
const schema = tsj . createGenerator ( config ) . createSchema ( config . type ) ;
const schemaString = JSON . stringify ( schema , null , 2 ) ;
fs . writeFile ( outputPath , schemaString , ( err ) => {
if ( err ) throw err ;
} ) ;
透過node main.js
運行模式產生器。
透過建立自訂格式化程式並將其新增至主格式化程式可以擴充內建格式化:
// my-function-formatter.ts
import { BaseType , Definition , FunctionType , SubTypeFormatter } from "ts-json-schema-generator" ;
import ts from "typescript" ;
export class MyFunctionTypeFormatter implements SubTypeFormatter {
// You can skip this line if you don't need childTypeFormatter
public constructor ( private childTypeFormatter : TypeFormatter ) { }
public supportsType ( type : BaseType ) : boolean {
return type instanceof FunctionType ;
}
public getDefinition ( type : FunctionType ) : Definition {
// Return a custom schema for the function property.
return {
type : "object" ,
properties : {
isFunction : {
type : "boolean" ,
const : true ,
} ,
} ,
} ;
}
// If this type does NOT HAVE children, generally all you need is:
public getChildren ( type : FunctionType ) : BaseType [ ] {
return [ ] ;
}
// However, if children ARE supported, you'll need something similar to
// this (see src/TypeFormatter/{Array,Definition,etc}.ts for some examples):
public getChildren ( type : FunctionType ) : BaseType [ ] {
return this . childTypeFormatter . getChildren ( type . getType ( ) ) ;
}
}
import { createProgram , createParser , SchemaGenerator , createFormatter } from "ts-json-schema-generator" ;
import { MyFunctionTypeFormatter } from "./my-function-formatter.ts" ;
import fs from "fs" ;
const config = {
path : "path/to/source/file" ,
tsconfig : "path/to/tsconfig.json" ,
type : "*" , // Or <type-name> if you want to generate schema for that one type only
} ;
// We configure the formatter an add our custom formatter to it.
const formatter = createFormatter ( config , ( fmt , circularReferenceTypeFormatter ) => {
// If your formatter DOES NOT support children, e.g. getChildren() { return [] }:
fmt . addTypeFormatter ( new MyFunctionTypeFormatter ( ) ) ;
// If your formatter DOES support children, you'll need this reference too:
fmt . addTypeFormatter ( new MyFunctionTypeFormatter ( circularReferenceTypeFormatter ) ) ;
} ) ;
const program = createProgram ( config ) ;
const parser = createParser ( program , config ) ;
const generator = new SchemaGenerator ( program , parser , formatter , config ) ;
const schema = generator . createSchema ( config . type ) ;
const outputPath = "path/to/output/file" ;
const schemaString = JSON . stringify ( schema , null , 2 ) ;
fs . writeFile ( outputPath , schemaString , ( err ) => {
if ( err ) throw err ;
} ) ;
與自訂格式類似,擴充內建解析的工作方式幾乎相同:
// my-constructor-parser.ts
import { Context , StringType , ReferenceType , BaseType , SubNodeParser } from "ts-json-schema-generator" ;
// use typescript exported by TJS to avoid version conflict
import ts from "ts-json-schema-generator" ;
export class MyConstructorParser implements SubNodeParser {
supportsNode ( node : ts . Node ) : boolean {
return node . kind === ts . SyntaxKind . ConstructorType ;
}
createType ( node : ts . Node , context : Context , reference ?: ReferenceType ) : BaseType | undefined {
return new StringType ( ) ; // Treat constructors as strings in this example
}
}
import { createProgram , createParser , SchemaGenerator , createFormatter } from "ts-json-schema-generator" ;
import { MyConstructorParser } from "./my-constructor-parser.ts" ;
import fs from "fs" ;
const config = {
path : "path/to/source/file" ,
tsconfig : "path/to/tsconfig.json" ,
type : "*" , // Or <type-name> if you want to generate schema for that one type only
} ;
const program = createProgram ( config ) ;
// We configure the parser an add our custom parser to it.
const parser = createParser ( program , config , ( prs ) => {
prs . addNodeParser ( new MyConstructorParser ( ) ) ;
} ) ;
const formatter = createFormatter ( config ) ;
const generator = new SchemaGenerator ( program , parser , formatter , config ) ;
const schema = generator . createSchema ( config . type ) ;
const outputPath = "path/to/output/file" ;
const schemaString = JSON . stringify ( schema , null , 2 ) ;
fs . writeFile ( outputPath , schemaString , ( err ) => {
if ( err ) throw err ;
} ) ;
interface
類型enum
類型union
、 tuple
、 type[]
類型Date
、 RegExp
、 URL
類型string
、 boolean
、 number
類型"value"
、 123
、 true
、 false
、 null
、 undefined
文字typeof
keyof
Promise<T>
展開為T
@format
) yarn --silent run run --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'
yarn --silent run debug --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'
並透過調試器協議進行連接。
AST Explorer 對於這個工具的開發人員來說太棒了!
發布由 2 分支預發布流程處理,在publish-auto.yml
中配置。所有更改都應基於預設的next
分支,並自動發布。
next
預發布標籤。結果可以使用npm install ts-json-schema-generator@next
安裝next
時,請使用squash and merge
策略。next
到stable
PR。next
合併到stable
時,請使用create a merge commit
策略。