ts-json-schema-generator 2.4.1-next.1 → 2.5.0--canary.2413.011011e.0
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/README.md +22 -21
- package/dist/factory/generator.js +1 -1
- package/dist/factory/generator.js.map +1 -1
- package/dist/factory/parser.js +13 -2
- package/dist/factory/parser.js.map +1 -1
- package/dist/factory/program.js +5 -2
- package/dist/factory/program.js.map +1 -1
- package/dist/package.json +31 -31
- package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.d.ts +2 -1
- package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.js +22 -10
- package/dist/src/AnnotationsReader/ExtendedAnnotationsReader.js.map +1 -1
- package/dist/src/Config.d.ts +5 -2
- package/dist/src/Config.js +1 -0
- package/dist/src/Config.js.map +1 -1
- package/dist/src/Error/BaseError.d.ts +1 -1
- package/dist/src/Error/BaseError.js +1 -2
- package/dist/src/Error/BaseError.js.map +1 -1
- package/dist/src/NodeParser/BinaryExpressionNodeParser.d.ts +13 -0
- package/dist/src/NodeParser/BinaryExpressionNodeParser.js +79 -0
- package/dist/src/NodeParser/BinaryExpressionNodeParser.js.map +1 -0
- package/dist/src/NodeParser/IdentifierNodeParser.d.ts +11 -0
- package/dist/src/NodeParser/IdentifierNodeParser.js +33 -0
- package/dist/src/NodeParser/IdentifierNodeParser.js.map +1 -0
- package/dist/src/NodeParser/MappedTypeNodeParser.js +1 -1
- package/dist/src/NodeParser/MappedTypeNodeParser.js.map +1 -1
- package/dist/src/NodeParser/NewExpressionParser.d.ts +13 -0
- package/dist/src/NodeParser/NewExpressionParser.js +41 -0
- package/dist/src/NodeParser/NewExpressionParser.js.map +1 -0
- package/dist/src/NodeParser/SpreadElementNodeParser.d.ts +10 -0
- package/dist/src/NodeParser/SpreadElementNodeParser.js +21 -0
- package/dist/src/NodeParser/SpreadElementNodeParser.js.map +1 -0
- package/dist/src/NodeParser/TypeOperatorNodeParser.d.ts +1 -1
- package/dist/src/NodeParser/TypeOperatorNodeParser.js +2 -1
- package/dist/src/NodeParser/TypeOperatorNodeParser.js.map +1 -1
- package/dist/src/NodeParser/TypeReferenceNodeParser.js +1 -1
- package/dist/src/NodeParser/TypeReferenceNodeParser.js.map +1 -1
- package/dist/src/SchemaGenerator.d.ts +2 -2
- package/dist/src/SchemaGenerator.js +17 -7
- package/dist/src/SchemaGenerator.js.map +1 -1
- package/dist/src/Type/AnnotatedType.d.ts +1 -3
- package/dist/src/Type/AnnotatedType.js.map +1 -1
- package/dist/src/Type/UnknownType.d.ts +1 -1
- package/dist/src/Type/UnknownType.js +1 -1
- package/dist/src/Type/UnknownType.js.map +1 -1
- package/dist/src/TypeFormatter/AnnotatedTypeFormatter.js +1 -1
- package/dist/src/TypeFormatter/AnnotatedTypeFormatter.js.map +1 -1
- package/dist/src/TypeFormatter/LiteralUnionTypeFormatter.js +10 -5
- package/dist/src/TypeFormatter/LiteralUnionTypeFormatter.js.map +1 -1
- package/dist/src/TypeFormatter/PrimitiveUnionTypeFormatter.js +6 -2
- package/dist/src/TypeFormatter/PrimitiveUnionTypeFormatter.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/src/Utils/getFullDescription.d.ts +2 -0
- package/dist/src/Utils/getFullDescription.js +33 -0
- package/dist/src/Utils/getFullDescription.js.map +1 -0
- package/dist/src/Utils/narrowType.js.map +1 -1
- package/dist/src/Utils/nodeKey.d.ts +1 -1
- package/dist/src/Utils/nodeKey.js.map +1 -1
- package/dist/ts-json-schema-generator.js +7 -3
- 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 +19 -2
- package/factory/program.ts +7 -3
- package/package.json +31 -31
- package/src/AnnotationsReader/ExtendedAnnotationsReader.ts +25 -10
- package/src/Config.ts +124 -2
- package/src/Error/BaseError.ts +1 -3
- package/src/NodeParser/BinaryExpressionNodeParser.ts +99 -0
- package/src/NodeParser/IdentifierNodeParser.ts +38 -0
- package/src/NodeParser/MappedTypeNodeParser.ts +1 -1
- package/src/NodeParser/NewExpressionParser.ts +46 -0
- package/src/NodeParser/SpreadElementNodeParser.ts +25 -0
- package/src/NodeParser/TypeOperatorNodeParser.ts +2 -2
- package/src/NodeParser/TypeReferenceNodeParser.ts +1 -1
- package/src/SchemaGenerator.ts +26 -9
- package/src/Type/AnnotatedType.ts +1 -3
- package/src/Type/UnknownType.ts +1 -1
- package/src/TypeFormatter/AnnotatedTypeFormatter.ts +2 -2
- package/src/TypeFormatter/LiteralUnionTypeFormatter.ts +12 -6
- package/src/TypeFormatter/PrimitiveUnionTypeFormatter.ts +9 -2
- package/src/Utils/castArray.ts +7 -0
- package/src/Utils/getFullDescription.ts +39 -0
- package/src/Utils/narrowType.ts +1 -6
- package/src/Utils/nodeKey.ts +3 -1
- package/ts-json-schema-generator.ts +12 -3
package/src/Type/UnknownType.ts
CHANGED
|
@@ -5,7 +5,7 @@ export class UnknownType extends BaseType {
|
|
|
5
5
|
/**
|
|
6
6
|
* If the source for this UnknownType was from a failed operation than to an actual `unknown` type present in the source code.
|
|
7
7
|
*/
|
|
8
|
-
readonly erroredSource
|
|
8
|
+
readonly erroredSource = false,
|
|
9
9
|
) {
|
|
10
10
|
super();
|
|
11
11
|
}
|
|
@@ -8,7 +8,7 @@ import type { TypeFormatter } from "../TypeFormatter.js";
|
|
|
8
8
|
import { derefType } from "../Utils/derefType.js";
|
|
9
9
|
|
|
10
10
|
export function makeNullable(def: Definition): Definition {
|
|
11
|
-
const union
|
|
11
|
+
const union = (def.oneOf || def.anyOf) as Definition[] | undefined;
|
|
12
12
|
if (union && union.filter((d: Definition) => d.type === "null").length === 0) {
|
|
13
13
|
union.push({ type: "null" });
|
|
14
14
|
} else if (def.type && def.type !== "object") {
|
|
@@ -58,7 +58,7 @@ export class AnnotatedTypeFormatter implements SubTypeFormatter {
|
|
|
58
58
|
if ("discriminator" in annotations) {
|
|
59
59
|
const deref = derefType(type.getType());
|
|
60
60
|
if (deref instanceof UnionType) {
|
|
61
|
-
deref.setDiscriminator(annotations.discriminator);
|
|
61
|
+
deref.setDiscriminator(annotations.discriminator as string);
|
|
62
62
|
delete annotations.discriminator;
|
|
63
63
|
} else {
|
|
64
64
|
throw new JsonTypeError(
|
|
@@ -55,12 +55,18 @@ export class LiteralUnionTypeFormatter implements SubTypeFormatter {
|
|
|
55
55
|
appendTypeValues(type, typeValues);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
const schema =
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
const schema =
|
|
59
|
+
typeNames.size === 1 && typeValues.size === 1
|
|
60
|
+
? {
|
|
61
|
+
type: toEnumType(Array.from(typeNames)),
|
|
62
|
+
const: Array.from(typeValues)[0],
|
|
63
|
+
}
|
|
64
|
+
: {
|
|
65
|
+
type: toEnumType(Array.from(typeNames)),
|
|
66
|
+
enum: Array.from(typeValues),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return hasString ? { anyOf: [{ type: "string" }, schema] } : schema;
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
public getChildren(): BaseType[] {
|
|
@@ -6,7 +6,6 @@ import type { BaseType } from "../Type/BaseType.js";
|
|
|
6
6
|
import { BooleanType } from "../Type/BooleanType.js";
|
|
7
7
|
import { NullType } from "../Type/NullType.js";
|
|
8
8
|
import { NumberType } from "../Type/NumberType.js";
|
|
9
|
-
import { PrimitiveType } from "../Type/PrimitiveType.js";
|
|
10
9
|
import { StringType } from "../Type/StringType.js";
|
|
11
10
|
import { UnionType } from "../Type/UnionType.js";
|
|
12
11
|
import { uniqueArray } from "../Utils/uniqueArray.js";
|
|
@@ -25,7 +24,15 @@ export class PrimitiveUnionTypeFormatter implements SubTypeFormatter {
|
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
protected isPrimitiveUnion(type: UnionType): boolean {
|
|
28
|
-
return type
|
|
27
|
+
return type
|
|
28
|
+
.getTypes()
|
|
29
|
+
.every(
|
|
30
|
+
(item) =>
|
|
31
|
+
item instanceof StringType ||
|
|
32
|
+
item instanceof NumberType ||
|
|
33
|
+
item instanceof BooleanType ||
|
|
34
|
+
item instanceof NullType,
|
|
35
|
+
);
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
protected getPrimitiveType(item: BaseType): RawTypeName {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
export function getFullDescription(node: ts.Node): string | undefined {
|
|
4
|
+
const sourceFile = node.getSourceFile();
|
|
5
|
+
const jsDocNodes = ts.getJSDocCommentsAndTags(node);
|
|
6
|
+
|
|
7
|
+
if (!jsDocNodes || jsDocNodes.length === 0) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let rawText = "";
|
|
12
|
+
|
|
13
|
+
for (const jsDoc of jsDocNodes) {
|
|
14
|
+
rawText += jsDoc.getFullText(sourceFile) + "\n";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
rawText = rawText.trim();
|
|
18
|
+
|
|
19
|
+
return getTextWithoutStars(rawText).trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getTextWithoutStars(inputText: string) {
|
|
23
|
+
const innerTextWithStars = inputText.replace(/^\/\*\*[^\S\n]*\n?/, "").replace(/(\r?\n)?[^\S\n]*\*\/$/, "");
|
|
24
|
+
|
|
25
|
+
return innerTextWithStars
|
|
26
|
+
.split(/\n/)
|
|
27
|
+
.map((line) => {
|
|
28
|
+
const trimmedLine = line.trimStart();
|
|
29
|
+
|
|
30
|
+
if (trimmedLine[0] !== "*") {
|
|
31
|
+
return line;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const textStartPos = trimmedLine[1] === " " ? 2 : 1;
|
|
35
|
+
|
|
36
|
+
return trimmedLine.substring(textStartPos);
|
|
37
|
+
})
|
|
38
|
+
.join("\n");
|
|
39
|
+
}
|
package/src/Utils/narrowType.ts
CHANGED
|
@@ -16,12 +16,7 @@ import { derefType } from "./derefType.js";
|
|
|
16
16
|
* kept, when returning false it is removed.
|
|
17
17
|
* @return The narrowed down type.
|
|
18
18
|
*/
|
|
19
|
-
export function narrowType(
|
|
20
|
-
type: BaseType,
|
|
21
|
-
// TODO: remove the next line
|
|
22
|
-
// eslint-disable-next-line no-shadow
|
|
23
|
-
predicate: (type: BaseType) => boolean,
|
|
24
|
-
): BaseType {
|
|
19
|
+
export function narrowType(type: BaseType, predicate: (type: BaseType) => boolean): BaseType {
|
|
25
20
|
const derefed = derefType(type);
|
|
26
21
|
if (derefed instanceof UnionType || derefed instanceof EnumType) {
|
|
27
22
|
let changed = false;
|
package/src/Utils/nodeKey.ts
CHANGED
|
@@ -2,7 +2,9 @@ import stringify from "safe-stable-stringify";
|
|
|
2
2
|
import type { Node } from "typescript";
|
|
3
3
|
import type { Context } from "../NodeParser.js";
|
|
4
4
|
|
|
5
|
-
export function hash(
|
|
5
|
+
export function hash(
|
|
6
|
+
a: string | boolean | number | (string | boolean | number | Record<string, unknown>)[] | Record<string, unknown>,
|
|
7
|
+
): string | number {
|
|
6
8
|
if (typeof a === "number") {
|
|
7
9
|
return a;
|
|
8
10
|
}
|
|
@@ -11,14 +11,14 @@ import pkg from "./package.json";
|
|
|
11
11
|
|
|
12
12
|
const args = new Command()
|
|
13
13
|
.option("-p, --path <path>", "Source file path")
|
|
14
|
-
.option("-t, --type <name
|
|
14
|
+
.option("-t, --type <name...>", "Type name(s)")
|
|
15
15
|
.option("-i, --id <name>", "$id for generated schema")
|
|
16
16
|
.option("-f, --tsconfig <path>", "Custom tsconfig.json path")
|
|
17
17
|
.addOption(
|
|
18
18
|
new Option("-e, --expose <expose>", "Type exposing").choices(["all", "none", "export"]).default("export"),
|
|
19
19
|
)
|
|
20
20
|
.addOption(
|
|
21
|
-
new Option("-j, --jsDoc <extended>", "Read
|
|
21
|
+
new Option("-j, --jsDoc <extended>", "Read JSDoc annotations")
|
|
22
22
|
.choices(["none", "basic", "extended"])
|
|
23
23
|
.default("extended"),
|
|
24
24
|
)
|
|
@@ -27,6 +27,14 @@ const args = new Command()
|
|
|
27
27
|
jsDoc: "extended",
|
|
28
28
|
}),
|
|
29
29
|
)
|
|
30
|
+
.addOption(
|
|
31
|
+
new Option(
|
|
32
|
+
"--full-description",
|
|
33
|
+
"Include the full raw JSDoc comment as `fullDescription` in the schema.",
|
|
34
|
+
).implies({
|
|
35
|
+
jsDoc: "extended",
|
|
36
|
+
}),
|
|
37
|
+
)
|
|
30
38
|
.addOption(
|
|
31
39
|
new Option(
|
|
32
40
|
"--functions <functions>",
|
|
@@ -65,6 +73,7 @@ const config: Config = {
|
|
|
65
73
|
topRef: args.topRef,
|
|
66
74
|
jsDoc: args.jsDoc,
|
|
67
75
|
markdownDescription: args.markdownDescription,
|
|
76
|
+
fullDescription: args.fullDescription,
|
|
68
77
|
sortProps: !args.unstable,
|
|
69
78
|
strictTuples: args.strictTuples,
|
|
70
79
|
skipTypeCheck: !args.typeCheck,
|
|
@@ -75,7 +84,7 @@ const config: Config = {
|
|
|
75
84
|
};
|
|
76
85
|
|
|
77
86
|
try {
|
|
78
|
-
const schema = createGenerator(config).createSchema(
|
|
87
|
+
const schema = createGenerator(config).createSchema(config.type);
|
|
79
88
|
|
|
80
89
|
const stringify = config.sortProps ? stableStringify : JSON.stringify;
|
|
81
90
|
// need as string since TS can't figure out that the string | undefined case doesn't happen
|