ts-json-schema-generator 2.5.0-next.6 → 2.5.0-next.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/factory/generator.js +1 -1
- package/dist/factory/generator.js.map +1 -1
- package/dist/factory/parser.js +4 -1
- package/dist/factory/parser.js.map +1 -1
- package/dist/package.json +5 -4
- package/dist/src/Config.d.ts +4 -2
- package/dist/src/Config.js.map +1 -1
- package/dist/src/SchemaGenerator.d.ts +2 -2
- package/dist/src/SchemaGenerator.js +10 -5
- package/dist/src/SchemaGenerator.js.map +1 -1
- package/dist/src/Utils/castArray.d.ts +1 -0
- package/dist/src/Utils/castArray.js +10 -0
- package/dist/src/Utils/castArray.js.map +1 -0
- package/dist/ts-json-schema-generator.js +7 -2
- package/dist/ts-json-schema-generator.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/factory/generator.ts +1 -1
- package/factory/parser.ts +5 -1
- package/package.json +5 -4
- package/src/Config.ts +122 -2
- package/src/SchemaGenerator.ts +12 -5
- package/src/Utils/castArray.ts +7 -0
- package/ts-json-schema-generator.ts +11 -2
package/factory/generator.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { createProgram } from "./program.js";
|
|
|
7
7
|
|
|
8
8
|
export function createGenerator(config: Config): SchemaGenerator {
|
|
9
9
|
const completedConfig = { ...DEFAULT_CONFIG, ...config };
|
|
10
|
-
const program = createProgram(completedConfig);
|
|
10
|
+
const program = config.tsProgram || createProgram(completedConfig);
|
|
11
11
|
const parser = createParser(program, completedConfig);
|
|
12
12
|
const formatter = createFormatter(completedConfig);
|
|
13
13
|
|
package/factory/parser.ts
CHANGED
|
@@ -62,6 +62,7 @@ import { SatisfiesNodeParser } from "../src/NodeParser/SatisfiesNodeParser.js";
|
|
|
62
62
|
import { PromiseNodeParser } from "../src/NodeParser/PromiseNodeParser.js";
|
|
63
63
|
import { SpreadElementNodeParser } from "../src/NodeParser/SpreadElementNodeParser.js";
|
|
64
64
|
import { IdentifierNodeParser } from "../src/NodeParser/IdentifierNodeParser.js";
|
|
65
|
+
import { castArray } from "../src/Utils/castArray.js";
|
|
65
66
|
|
|
66
67
|
export type ParserAugmentor = (parser: MutableParser) => void;
|
|
67
68
|
|
|
@@ -73,7 +74,10 @@ export function createParser(program: ts.Program, config: CompletedConfig, augme
|
|
|
73
74
|
return new ExposeNodeParser(typeChecker, nodeParser, config.expose, config.jsDoc);
|
|
74
75
|
}
|
|
75
76
|
function withTopRef(nodeParser: NodeParser): NodeParser {
|
|
76
|
-
|
|
77
|
+
const typeArr = castArray(config.type);
|
|
78
|
+
// If we have multiple types, don't set a top-level $ref.
|
|
79
|
+
const topRefFullName = typeArr && typeArr.length === 1 ? typeArr[0] : undefined;
|
|
80
|
+
return new TopRefNodeParser(chainNodeParser, topRefFullName, config.topRef);
|
|
77
81
|
}
|
|
78
82
|
function withJsDoc(nodeParser: SubNodeParser): SubNodeParser {
|
|
79
83
|
const extraTags = new Set(config.extraTags);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-json-schema-generator",
|
|
3
|
-
"version": "2.5.0-next.
|
|
3
|
+
"version": "2.5.0-next.8",
|
|
4
4
|
"description": "Generate JSON schema from your Typescript sources",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ts",
|
|
@@ -58,12 +58,13 @@
|
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@types/json-schema": "^7.0.15",
|
|
60
60
|
"commander": "^14.0.0",
|
|
61
|
-
"glob": "^
|
|
61
|
+
"glob": "^13.0.0",
|
|
62
62
|
"json5": "^2.2.3",
|
|
63
63
|
"normalize-path": "^3.0.0",
|
|
64
64
|
"safe-stable-stringify": "^2.5.0",
|
|
65
65
|
"tslib": "^2.8.1",
|
|
66
|
-
"typescript": "^5.8.3"
|
|
66
|
+
"typescript": "^5.8.3",
|
|
67
|
+
"@typescript/vfs": "1.6.2"
|
|
67
68
|
},
|
|
68
69
|
"devDependencies": {
|
|
69
70
|
"@auto-it/conventional-commits": "^11.3.0",
|
|
@@ -81,7 +82,7 @@
|
|
|
81
82
|
"auto": "^11.3.0",
|
|
82
83
|
"chai": "^6.0.1",
|
|
83
84
|
"cross-env": "^10.0.0",
|
|
84
|
-
"eslint": "9.
|
|
85
|
+
"eslint": "9.39.1",
|
|
85
86
|
"eslint-config-prettier": "^10.1.5",
|
|
86
87
|
"eslint-plugin-prettier": "^5.5.1",
|
|
87
88
|
"globals": "^16.3.0",
|
package/src/Config.ts
CHANGED
|
@@ -1,29 +1,149 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
|
|
1
3
|
export interface Config {
|
|
4
|
+
/**
|
|
5
|
+
* Glob pattern(s) for source TypeScript files to process.
|
|
6
|
+
* If not provided, falls back to files from tsconfig.
|
|
7
|
+
*/
|
|
2
8
|
path?: string;
|
|
3
|
-
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Name of the type(s)/interface(s) to generate schema for.
|
|
12
|
+
* Use "*" to generate schemas for all exported types.
|
|
13
|
+
*/
|
|
14
|
+
type?: string | string[];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Minify the output JSON schema (no whitespace).
|
|
18
|
+
* When false, the schema is pretty-printed with 2-space indentation.
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
4
21
|
minify?: boolean;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Sets the `$id` property in the root of the generated schema.
|
|
25
|
+
* Used for schema identification and referencing.
|
|
26
|
+
*/
|
|
5
27
|
schemaId?: string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Path to a custom tsconfig.json file for TypeScript compilation.
|
|
31
|
+
* If not provided, uses default TypeScript configuration.
|
|
32
|
+
*/
|
|
6
33
|
tsconfig?: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Controls which types are exposed as definitions in the schema.
|
|
37
|
+
* - "all": Exposes all types except type literals
|
|
38
|
+
* - "none": Exposes no types automatically
|
|
39
|
+
* - "export": Only exposes exported types (respects @internal JSDoc tag)
|
|
40
|
+
* @default "export"
|
|
41
|
+
*/
|
|
7
42
|
expose?: "all" | "none" | "export";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Wraps the root type in a `$ref` definition.
|
|
46
|
+
* When false, inlines the root type definition directly.
|
|
47
|
+
* @default true
|
|
48
|
+
*/
|
|
8
49
|
topRef?: boolean;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Controls how JSDoc comments are parsed and included in the schema.
|
|
53
|
+
* - "none": Ignores all JSDoc annotations
|
|
54
|
+
* - "basic": Parses standard JSON Schema JSDoc tags
|
|
55
|
+
* - "extended": Parses all tags plus descriptions, examples, and type overrides
|
|
56
|
+
* @default "extended"
|
|
57
|
+
*/
|
|
9
58
|
jsDoc?: "none" | "extended" | "basic";
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Adds a `markdownDescription` field alongside `description` in the schema.
|
|
62
|
+
* Preserves markdown formatting including newlines.
|
|
63
|
+
* Only works with `jsDoc: "extended"`.
|
|
64
|
+
* @default false
|
|
65
|
+
*/
|
|
10
66
|
markdownDescription?: boolean;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Includes the complete raw JSDoc comment as `fullDescription` in the schema.
|
|
70
|
+
* Only works with `jsDoc: "extended"`.
|
|
71
|
+
* @default false
|
|
72
|
+
*/
|
|
11
73
|
fullDescription?: boolean;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Sorts object properties alphabetically in the output.
|
|
77
|
+
* @default true
|
|
78
|
+
*/
|
|
12
79
|
sortProps?: boolean;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Controls whether tuples allow additional items beyond their defined length.
|
|
83
|
+
* @default false
|
|
84
|
+
*/
|
|
13
85
|
strictTuples?: boolean;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Skips TypeScript type checking to improve performance.
|
|
89
|
+
* Speeds up generation but may miss type errors.
|
|
90
|
+
* @default false
|
|
91
|
+
*/
|
|
14
92
|
skipTypeCheck?: boolean;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* URI-encodes `$ref` values (e.g., `#/definitions/Foo%3CBar%3E`).
|
|
96
|
+
* When false, uses raw names in reference paths.
|
|
97
|
+
* @default true
|
|
98
|
+
*/
|
|
15
99
|
encodeRefs?: boolean;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Array of additional JSDoc tag names to include in the schema.
|
|
103
|
+
* Custom tags (e.g., `@customProperty`) are parsed and included in output.
|
|
104
|
+
* Values are parsed as JSON5.
|
|
105
|
+
* @default []
|
|
106
|
+
*/
|
|
16
107
|
extraTags?: string[];
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Sets default value for `additionalProperties` on objects without index signatures.
|
|
111
|
+
* When false, objects get `additionalProperties: false` by default.
|
|
112
|
+
* When true, allows additional properties on all objects.
|
|
113
|
+
* @default false
|
|
114
|
+
*/
|
|
17
115
|
additionalProperties?: boolean;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Controls discriminator style for discriminated unions.
|
|
119
|
+
* - "json-schema": Uses `if`/`then`/`allOf` with properties containing discriminator enum
|
|
120
|
+
* - "open-api": Uses OpenAPI 3.x style with `discriminator: { propertyName }` and `oneOf`
|
|
121
|
+
* @default "json-schema"
|
|
122
|
+
*/
|
|
18
123
|
discriminatorType?: "json-schema" | "open-api";
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Controls how function types are handled in the schema.
|
|
127
|
+
* - "fail": Throws error when encountering function types
|
|
128
|
+
* - "comment": Generates schema with `$comment` describing the function signature
|
|
129
|
+
* - "hide": Treats functions as NeverType (excluded from schema)
|
|
130
|
+
* @default "comment"
|
|
131
|
+
*/
|
|
19
132
|
functions?: FunctionOptions;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Pre-compiled TypeScript Program instance to use.
|
|
136
|
+
* Bypasses the default setup of a TypeScript program, and so some configuration options may not be applied.
|
|
137
|
+
* Useful for programmatic usage with existing TypeScript compilation, or for vfs scenarios where you do not want file-system representation.
|
|
138
|
+
*/
|
|
139
|
+
tsProgram?: ts.Program;
|
|
20
140
|
}
|
|
21
141
|
|
|
22
142
|
export type CompletedConfig = Config & typeof DEFAULT_CONFIG;
|
|
23
143
|
|
|
24
144
|
export type FunctionOptions = "fail" | "comment" | "hide";
|
|
25
145
|
|
|
26
|
-
export const DEFAULT_CONFIG: Omit<Required<Config>, "path" | "type" | "schemaId" | "tsconfig"> = {
|
|
146
|
+
export const DEFAULT_CONFIG: Omit<Required<Config>, "path" | "type" | "schemaId" | "tsconfig" | "tsProgram"> = {
|
|
27
147
|
expose: "export",
|
|
28
148
|
topRef: true,
|
|
29
149
|
jsDoc: "extended",
|
package/src/SchemaGenerator.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { TypeFormatter } from "./TypeFormatter.js";
|
|
|
10
10
|
import type { StringMap } from "./Utils/StringMap.js";
|
|
11
11
|
import { hasJsDocTag } from "./Utils/hasJsDocTag.js";
|
|
12
12
|
import { removeUnreachable } from "./Utils/removeUnreachable.js";
|
|
13
|
+
import { castArray } from "./Utils/castArray.js";
|
|
13
14
|
import { symbolAtNode } from "./Utils/symbolAtNode.js";
|
|
14
15
|
|
|
15
16
|
export class SchemaGenerator {
|
|
@@ -20,8 +21,8 @@ export class SchemaGenerator {
|
|
|
20
21
|
protected readonly config?: Config,
|
|
21
22
|
) {}
|
|
22
23
|
|
|
23
|
-
public createSchema(
|
|
24
|
-
const rootNodes = this.getRootNodes(
|
|
24
|
+
public createSchema(fullNames?: string | string[]): Schema {
|
|
25
|
+
const rootNodes = this.getRootNodes(castArray(fullNames));
|
|
25
26
|
return this.createSchemaFromNodes(rootNodes);
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -60,9 +61,15 @@ export class SchemaGenerator {
|
|
|
60
61
|
};
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
protected getRootNodes(
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
protected getRootNodes(fullNames: string[] | undefined): ts.Node[] {
|
|
65
|
+
// ["*"] means generate everything.
|
|
66
|
+
if (fullNames && fullNames.includes("*") && fullNames.length > 1) {
|
|
67
|
+
throw new Error("Cannot mix '*' with specific type names");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const generateAll = !fullNames || fullNames.length === 0 || (fullNames.length === 1 && fullNames[0] === "*");
|
|
71
|
+
if (!generateAll) {
|
|
72
|
+
return fullNames.map((name) => this.findNamedNode(name));
|
|
66
73
|
}
|
|
67
74
|
|
|
68
75
|
const rootFileNames = this.program.getRootFileNames();
|
|
@@ -11,7 +11,16 @@ import pkg from "./package.json";
|
|
|
11
11
|
|
|
12
12
|
const args = new Command()
|
|
13
13
|
.option("-p, --path <path>", "Source file path")
|
|
14
|
-
.option(
|
|
14
|
+
.option(
|
|
15
|
+
"-t, --type <name>",
|
|
16
|
+
"Type name (can be passed multiple times)",
|
|
17
|
+
(value: string, previous: string[] | undefined) => {
|
|
18
|
+
if (previous) {
|
|
19
|
+
return previous.concat(value);
|
|
20
|
+
}
|
|
21
|
+
return [value];
|
|
22
|
+
},
|
|
23
|
+
)
|
|
15
24
|
.option("-i, --id <name>", "$id for generated schema")
|
|
16
25
|
.option("-f, --tsconfig <path>", "Custom tsconfig.json path")
|
|
17
26
|
.addOption(
|
|
@@ -84,7 +93,7 @@ const config: Config = {
|
|
|
84
93
|
};
|
|
85
94
|
|
|
86
95
|
try {
|
|
87
|
-
const schema = createGenerator(config).createSchema(
|
|
96
|
+
const schema = createGenerator(config).createSchema(config.type);
|
|
88
97
|
|
|
89
98
|
const stringify = config.sortProps ? stableStringify : JSON.stringify;
|
|
90
99
|
// need as string since TS can't figure out that the string | undefined case doesn't happen
|