Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-json-schema-generator

Package Overview
Dependencies
Maintainers
3
Versions
335
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-json-schema-generator - npm Package Compare versions

Comparing version 2.1.0 to 2.1.1--canary.1933.12b85ba.0

2

dist/package.json
{
"name": "ts-json-schema-generator",
"version": "2.1.0",
"version": "2.1.1--canary.1933.12b85ba.0",
"description": "Generate JSON schema from your Typescript sources",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -19,2 +19,3 @@ import ts from "typescript";

import { removeUndefined } from "../Utils/removeUndefined.js";
import { uniqueTypeArray } from "../Utils/uniqueTypeArray.js";
export class MappedTypeNodeParser {

@@ -84,4 +85,3 @@ childNodeParser;

getProperties(node, keyListType, context) {
return keyListType
.getTypes()
return uniqueTypeArray(keyListType.getFlattenedTypes(derefType))
.filter((type) => type instanceof LiteralType)

@@ -88,0 +88,0 @@ .map((type) => [type, this.mapKey(node, type, context)])

@@ -12,2 +12,3 @@ import { BaseType } from "./BaseType.js";

normalize(): BaseType;
getFlattenedTypes(deref?: (type: BaseType) => BaseType): BaseType[];
}
import { BaseType } from "./BaseType.js";
import { uniqueTypeArray } from "../Utils/uniqueTypeArray.js";
import { NeverType } from "./NeverType.js";
import { derefType } from "../Utils/derefType.js";
import { derefAliasedType, derefType, isHiddenType } from "../Utils/derefType.js";
export class UnionType extends BaseType {

@@ -52,3 +52,14 @@ types;

}
getFlattenedTypes(deref = derefAliasedType) {
return this.getTypes()
.filter((t) => !isHiddenType(t))
.map(deref)
.flatMap((t) => {
if (t instanceof UnionType) {
return t.getFlattenedTypes(deref);
}
return t;
});
}
}
//# sourceMappingURL=UnionType.js.map

@@ -5,3 +5,2 @@ import { LiteralType } from "../Type/LiteralType.js";

import { UnionType } from "../Type/UnionType.js";
import { derefAliasedType, isHiddenType } from "../Utils/derefType.js";
import { typeName } from "../Utils/typeName.js";

@@ -18,4 +17,4 @@ import { uniqueArray } from "../Utils/uniqueArray.js";

let hasNull = false;
const flattenedTypes = flattenTypes(type);
const types = flattenedTypes.filter((t) => {
const literals = type.getFlattenedTypes();
const types = literals.filter((t) => {
if (t instanceof StringType) {

@@ -62,17 +61,7 @@ hasString = true;

}
function flattenTypes(type) {
export function isLiteralUnion(type) {
return type
.getTypes()
.filter((t) => !isHiddenType(t))
.map(derefAliasedType)
.flatMap((t) => {
if (t instanceof UnionType) {
return flattenTypes(t);
}
return t;
});
.getFlattenedTypes()
.every((item) => item instanceof LiteralType || item instanceof NullType || item instanceof StringType);
}
export function isLiteralUnion(type) {
return flattenTypes(type).every((item) => item instanceof LiteralType || item instanceof NullType || item instanceof StringType);
}
function getLiteralValue(value) {

@@ -79,0 +68,0 @@ return value instanceof LiteralType ? value.getValue() : null;

{
"name": "ts-json-schema-generator",
"version": "2.1.0",
"version": "2.1.1--canary.1933.12b85ba.0",
"description": "Generate JSON schema from your Typescript sources",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -21,2 +21,3 @@ import ts from "typescript";

import { removeUndefined } from "../Utils/removeUndefined.js";
import { uniqueTypeArray } from "../Utils/uniqueTypeArray.js";

@@ -106,4 +107,3 @@ export class MappedTypeNodeParser implements SubNodeParser {

protected getProperties(node: ts.MappedTypeNode, keyListType: UnionType, context: Context): ObjectProperty[] {
return keyListType
.getTypes()
return uniqueTypeArray(keyListType.getFlattenedTypes(derefType))
.filter((type): type is LiteralType => type instanceof LiteralType)

@@ -110,0 +110,0 @@ .map((type) => [type, this.mapKey(node, type, context)])

import { BaseType } from "./BaseType.js";
import { uniqueTypeArray } from "../Utils/uniqueTypeArray.js";
import { NeverType } from "./NeverType.js";
import { derefType } from "../Utils/derefType.js";
import { derefAliasedType, derefType, isHiddenType } from "../Utils/derefType.js";

@@ -59,2 +59,17 @@ export class UnionType extends BaseType {

}
/**
* Get the types in this union as a flat list.
*/
public getFlattenedTypes(deref: (type: BaseType) => BaseType = derefAliasedType): BaseType[] {
return this.getTypes()
.filter((t) => !isHiddenType(t))
.map(deref)
.flatMap((t) => {
if (t instanceof UnionType) {
return t.getFlattenedTypes(deref);
}
return t;
});
}
}

@@ -9,3 +9,2 @@ import { Definition } from "../Schema/Definition.js";

import { UnionType } from "../Type/UnionType.js";
import { derefAliasedType, isHiddenType } from "../Utils/derefType.js";
import { typeName } from "../Utils/typeName.js";

@@ -24,6 +23,6 @@ import { uniqueArray } from "../Utils/uniqueArray.js";

const flattenedTypes = flattenTypes(type);
const literals = type.getFlattenedTypes();
// filter out String types since we need to be more careful about them
const types = flattenedTypes.filter((t) => {
const types = literals.filter((t) => {
if (t instanceof StringType) {

@@ -75,21 +74,8 @@ hasString = true;

function flattenTypes(type: UnionType): (StringType | LiteralType | NullType)[] {
export function isLiteralUnion(type: UnionType): boolean {
return type
.getTypes()
.filter((t) => !isHiddenType(t))
.map(derefAliasedType)
.flatMap((t) => {
if (t instanceof UnionType) {
return flattenTypes(t);
}
return t as StringType | LiteralType | NullType;
});
.getFlattenedTypes()
.every((item) => item instanceof LiteralType || item instanceof NullType || item instanceof StringType);
}
export function isLiteralUnion(type: UnionType): boolean {
return flattenTypes(type).every(
(item) => item instanceof LiteralType || item instanceof NullType || item instanceof StringType,
);
}
function getLiteralValue(value: LiteralType | NullType): LiteralValue | null {

@@ -96,0 +82,0 @@ return value instanceof LiteralType ? value.getValue() : null;

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc