ts-json-schema-generator
Advanced tools
Comparing version 2.0.2--canary.1925.d74c6f4.0 to 2.1.0--canary.1927.92331d0.0
{ | ||
"name": "ts-json-schema-generator", | ||
"version": "2.0.2--canary.1925.d74c6f4.0", | ||
"version": "2.1.0--canary.1927.92331d0.0", | ||
"description": "Generate JSON schema from your Typescript sources", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { LiteralType } from "../Type/LiteralType.js"; | ||
import { NullType } from "../Type/NullType.js"; | ||
import { StringType } from "../Type/StringType.js"; | ||
import { UnionType } from "../Type/UnionType.js"; | ||
@@ -11,16 +12,19 @@ import { typeName } from "../Utils/typeName.js"; | ||
getDefinition(type) { | ||
const values = uniqueArray(type.getTypes().map((item) => this.getLiteralValue(item))); | ||
const types = uniqueArray(type.getTypes().map((item) => this.getLiteralType(item))); | ||
if (types.length === 1) { | ||
let hasString = false; | ||
const types = type.getTypes().filter((t) => { | ||
const isString = t instanceof StringType; | ||
hasString = hasString || isString; | ||
return !isString; | ||
}); | ||
if (hasString) { | ||
return { | ||
type: types[0], | ||
enum: values, | ||
type: "string", | ||
}; | ||
} | ||
else { | ||
return { | ||
type: types, | ||
enum: values, | ||
}; | ||
} | ||
const values = uniqueArray(types.map((item) => this.getLiteralValue(item))); | ||
const typeNames = uniqueArray(types.map((item) => this.getLiteralType(item))); | ||
return { | ||
type: typeNames.length === 1 ? typeNames[0] : typeNames, | ||
enum: values, | ||
}; | ||
} | ||
@@ -31,3 +35,5 @@ getChildren(type) { | ||
isLiteralUnion(type) { | ||
return type.getTypes().every((item) => item instanceof LiteralType || item instanceof NullType); | ||
return type | ||
.getTypes() | ||
.every((item) => item instanceof LiteralType || item instanceof NullType || item instanceof StringType); | ||
} | ||
@@ -34,0 +40,0 @@ getLiteralValue(value) { |
@@ -80,33 +80,2 @@ import { LiteralType } from "../Type/LiteralType.js"; | ||
const definitions = this.getTypeDefinitions(type); | ||
let stringType = true; | ||
let oneNotEnum = false; | ||
for (const def of definitions) { | ||
if (def.type !== "string") { | ||
stringType = false; | ||
break; | ||
} | ||
if (def.enum === undefined) { | ||
oneNotEnum = true; | ||
} | ||
} | ||
if (stringType && oneNotEnum) { | ||
const values = []; | ||
for (const def of definitions) { | ||
if (def.enum) { | ||
values.push(...def.enum); | ||
} | ||
else if (def.const) { | ||
values.push(def.const); | ||
} | ||
else { | ||
return { | ||
type: "string", | ||
}; | ||
} | ||
} | ||
return { | ||
type: "string", | ||
enum: values, | ||
}; | ||
} | ||
const flattenedDefinitions = []; | ||
@@ -113,0 +82,0 @@ for (const def of definitions) { |
{ | ||
"name": "ts-json-schema-generator", | ||
"version": "2.0.2--canary.1925.d74c6f4.0", | ||
"version": "2.1.0--canary.1927.92331d0.0", | ||
"description": "Generate JSON schema from your Typescript sources", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -7,2 +7,3 @@ import { Definition } from "../Schema/Definition.js"; | ||
import { NullType } from "../Type/NullType.js"; | ||
import { StringType } from "../Type/StringType.js"; | ||
import { UnionType } from "../Type/UnionType.js"; | ||
@@ -17,16 +18,26 @@ import { typeName } from "../Utils/typeName.js"; | ||
public getDefinition(type: UnionType): Definition { | ||
const values = uniqueArray(type.getTypes().map((item: LiteralType | NullType) => this.getLiteralValue(item))); | ||
const types = uniqueArray(type.getTypes().map((item: LiteralType | NullType) => this.getLiteralType(item))); | ||
let hasString = false; | ||
const types = type.getTypes().filter((t) => { | ||
const isString = t instanceof StringType; | ||
hasString = hasString || isString; | ||
return !isString; | ||
}); | ||
if (types.length === 1) { | ||
if (hasString) { | ||
return { | ||
type: types[0], | ||
enum: values, | ||
type: "string", | ||
}; | ||
} else { | ||
return { | ||
type: types, | ||
enum: values, | ||
}; | ||
} | ||
const values = uniqueArray( | ||
types.map((item: LiteralType | NullType | StringType) => this.getLiteralValue(item)), | ||
); | ||
const typeNames = uniqueArray( | ||
types.map((item: LiteralType | NullType | StringType) => this.getLiteralType(item)), | ||
); | ||
return { | ||
type: typeNames.length === 1 ? typeNames[0] : typeNames, | ||
enum: values, | ||
}; | ||
} | ||
@@ -38,3 +49,5 @@ public getChildren(type: UnionType): BaseType[] { | ||
protected isLiteralUnion(type: UnionType): boolean { | ||
return type.getTypes().every((item) => item instanceof LiteralType || item instanceof NullType); | ||
return type | ||
.getTypes() | ||
.every((item) => item instanceof LiteralType || item instanceof NullType || item instanceof StringType); | ||
} | ||
@@ -41,0 +54,0 @@ protected getLiteralValue(value: LiteralType | NullType): string | number | boolean | null { |
@@ -101,34 +101,2 @@ import { JSONSchema7 } from "json-schema"; | ||
// TODO: why is this not covered by LiteralUnionTypeFormatter? | ||
// special case for string literals | string -> string | ||
let stringType = true; | ||
let oneNotEnum = false; | ||
for (const def of definitions) { | ||
if (def.type !== "string") { | ||
stringType = false; | ||
break; | ||
} | ||
if (def.enum === undefined) { | ||
oneNotEnum = true; | ||
} | ||
} | ||
if (stringType && oneNotEnum) { | ||
const values = []; | ||
for (const def of definitions) { | ||
if (def.enum) { | ||
values.push(...def.enum); | ||
} else if (def.const) { | ||
values.push(def.const); | ||
} else { | ||
return { | ||
type: "string", | ||
}; | ||
} | ||
} | ||
return { | ||
type: "string", | ||
enum: values, | ||
}; | ||
} | ||
const flattenedDefinitions: JSONSchema7[] = []; | ||
@@ -135,0 +103,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
802879
11987