Skip to content

Commit

Permalink
refactor: moved RestType title addition to generated schema to RestTy…
Browse files Browse the repository at this point in the history
…peFormatter
  • Loading branch information
Filipe Pomar committed May 25, 2022
1 parent 56f75d2 commit 7bf5a18
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
13 changes: 11 additions & 2 deletions src/TypeFormatter/RestTypeFormatter.ts
Expand Up @@ -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());
}
Expand Down
22 changes: 9 additions & 13 deletions src/TypeFormatter/TupleTypeFormatter.ts
Expand Up @@ -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:
Expand All @@ -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
};
}

Expand Down
10 changes: 6 additions & 4 deletions test/valid-data/type-named-tuple-member/main.ts
Expand Up @@ -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[]];
16 changes: 12 additions & 4 deletions test/valid-data/type-named-tuple-member/schema.json
Expand Up @@ -31,7 +31,7 @@
"minItems": 2,
"type": "array"
},
"MyUniformTupleWithRest": {
"MyNamedUniformTupleWithRest": {
"additionalItems": {
"title": "third",
"type": "number"
Expand All @@ -49,7 +49,7 @@
"minItems": 2,
"type": "array"
},
"MyTupleWithRest": {
"MyNamedTupleWithRest": {
"additionalItems": {
"title": "third",
"type": "string"
Expand All @@ -67,7 +67,7 @@
"minItems": 2,
"type": "array"
},
"MyNestedArrayWithinTuple": {
"MyNamedNestedArrayWithinTuple": {
"items": [
{
"title": "first",
Expand All @@ -89,7 +89,7 @@
"minItems": 3,
"type": "array"
},
"MyNestedArrayWithinTupleWithRest": {
"MyNamedNestedArrayWithinTupleWithRest": {
"additionalItems": {
"title": "third",
"items": {
Expand All @@ -109,6 +109,14 @@
],
"minItems": 2,
"type": "array"
},
"MyNamedTupleWithOnlyRest": {
"items": {
"title": "first",
"type": "number"
},
"minItems": 0,
"type": "array"
}
}
}

0 comments on commit 7bf5a18

Please sign in to comment.