From 7bf5a18514de6bd95115f1c8e056d2a7d58f5a3f Mon Sep 17 00:00:00 2001 From: Filipe Pomar Date: Wed, 25 May 2022 19:54:53 +0200 Subject: [PATCH] refactor: moved RestType title addition to generated schema to RestTypeFormatter --- src/TypeFormatter/RestTypeFormatter.ts | 13 +++++++++-- src/TypeFormatter/TupleTypeFormatter.ts | 22 ++++++++----------- .../type-named-tuple-member/main.ts | 10 +++++---- .../type-named-tuple-member/schema.json | 16 ++++++++++---- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/TypeFormatter/RestTypeFormatter.ts b/src/TypeFormatter/RestTypeFormatter.ts index 6b09d2aee..cc22d674e 100644 --- a/src/TypeFormatter/RestTypeFormatter.ts +++ b/src/TypeFormatter/RestTypeFormatter.ts @@ -7,12 +7,21 @@ import { TypeFormatter } from "../TypeFormatter"; export class RestTypeFormatter implements SubTypeFormatter { public constructor(protected childTypeFormatter: TypeFormatter) {} - public supportsType(type: RestType): boolean { + public supportsType(type: BaseType): boolean { return type instanceof RestType; } + public getDefinition(type: RestType): Definition { - return this.childTypeFormatter.getDefinition(type.getType()); + const definition = this.childTypeFormatter.getDefinition(type.getType()); + const title = type.getTitle(); + + if (title !== null && typeof definition.items === "object") { + return { ...definition, items: { ...definition.items, title } }; + } + + return definition; } + public getChildren(type: RestType): BaseType[] { return this.childTypeFormatter.getChildren(type.getType()); } diff --git a/src/TypeFormatter/TupleTypeFormatter.ts b/src/TypeFormatter/TupleTypeFormatter.ts index 8c887e76d..53a1cbda3 100644 --- a/src/TypeFormatter/TupleTypeFormatter.ts +++ b/src/TypeFormatter/TupleTypeFormatter.ts @@ -19,9 +19,8 @@ export class TupleTypeFormatter implements SubTypeFormatter { const subTypes = type.getTypes().filter(notUndefined); const requiredElements = subTypes.filter((t) => !(t instanceof OptionalType) && !(t instanceof RestType)); - const optionalElements = subTypes.filter((t) => t instanceof OptionalType) as OptionalType[]; - const restElements = subTypes.filter((t): t is RestType => t instanceof RestType); - const restType = restElements[0]; + const optionalElements = subTypes.filter((t): t is OptionalType => t instanceof OptionalType); + const restType = subTypes.find((t): t is RestType => t instanceof RestType); const firstItemType = requiredElements.length > 0 ? requiredElements[0] : optionalElements[0]?.getType(); // Check whether the tuple is of any of the following forms: @@ -48,21 +47,18 @@ export class TupleTypeFormatter implements SubTypeFormatter { const requiredDefinitions = requiredElements.map((item) => this.childTypeFormatter.getDefinition(item)); const optionalDefinitions = optionalElements.map((item) => this.childTypeFormatter.getDefinition(item)); const itemsTotal = requiredDefinitions.length + optionalDefinitions.length; - const restDefinition = restType - ? { - ...this.childTypeFormatter.getDefinition(restType.getType().getItem()), - ...(restType.getTitle() ? { title: restType.getTitle() as string } : {}), - } - : undefined; + const additionalItems = restType ? this.childTypeFormatter.getDefinition(restType).items : undefined; return { type: "array", minItems: requiredDefinitions.length, ...(itemsTotal ? { items: requiredDefinitions.concat(optionalDefinitions) } : {}), // with items - ...(!itemsTotal && restDefinition ? { items: restDefinition } : {}), // with only rest param - ...(!itemsTotal && !restDefinition ? { maxItems: 0 } : {}), // empty - ...(restDefinition && itemsTotal ? { additionalItems: restDefinition } : {}), // with items and rest - ...(!restDefinition && itemsTotal ? { maxItems: itemsTotal } : {}), // without rest + ...(!itemsTotal && additionalItems ? { items: additionalItems } : {}), // with only rest param + ...(!itemsTotal && !additionalItems ? { maxItems: 0 } : {}), // empty + ...(additionalItems && !Array.isArray(additionalItems) && itemsTotal + ? { additionalItems: additionalItems } + : {}), // with rest items + ...(!additionalItems && itemsTotal ? { maxItems: itemsTotal } : {}), // without rest }; } diff --git a/test/valid-data/type-named-tuple-member/main.ts b/test/valid-data/type-named-tuple-member/main.ts index 244e27861..0b5613c94 100644 --- a/test/valid-data/type-named-tuple-member/main.ts +++ b/test/valid-data/type-named-tuple-member/main.ts @@ -2,10 +2,12 @@ export type MyNamedUniformTuple = [first: string, second: string]; export type MyNamedTuple = [first: string, second: number]; -export type MyUniformTupleWithRest = [first: number, second: number, ...third: number[]]; +export type MyNamedUniformTupleWithRest = [first: number, second: number, ...third: number[]]; -export type MyTupleWithRest = [first: string, second: number, ...third: string[]]; +export type MyNamedTupleWithRest = [first: string, second: number, ...third: string[]]; -export type MyNestedArrayWithinTuple = [first: string, second: number, third: string[]]; +export type MyNamedNestedArrayWithinTuple = [first: string, second: number, third: string[]]; -export type MyNestedArrayWithinTupleWithRest = [first: string, second: number, ...third: string[][]]; +export type MyNamedNestedArrayWithinTupleWithRest = [first: string, second: number, ...third: string[][]]; + +export type MyNamedTupleWithOnlyRest = [...first: number[]]; diff --git a/test/valid-data/type-named-tuple-member/schema.json b/test/valid-data/type-named-tuple-member/schema.json index e411a6a27..1ac861c63 100644 --- a/test/valid-data/type-named-tuple-member/schema.json +++ b/test/valid-data/type-named-tuple-member/schema.json @@ -31,7 +31,7 @@ "minItems": 2, "type": "array" }, - "MyUniformTupleWithRest": { + "MyNamedUniformTupleWithRest": { "additionalItems": { "title": "third", "type": "number" @@ -49,7 +49,7 @@ "minItems": 2, "type": "array" }, - "MyTupleWithRest": { + "MyNamedTupleWithRest": { "additionalItems": { "title": "third", "type": "string" @@ -67,7 +67,7 @@ "minItems": 2, "type": "array" }, - "MyNestedArrayWithinTuple": { + "MyNamedNestedArrayWithinTuple": { "items": [ { "title": "first", @@ -89,7 +89,7 @@ "minItems": 3, "type": "array" }, - "MyNestedArrayWithinTupleWithRest": { + "MyNamedNestedArrayWithinTupleWithRest": { "additionalItems": { "title": "third", "items": { @@ -109,6 +109,14 @@ ], "minItems": 2, "type": "array" + }, + "MyNamedTupleWithOnlyRest": { + "items": { + "title": "first", + "type": "number" + }, + "minItems": 0, + "type": "array" } } }