From a51f6b1b69baecaa5286580eb0acb734240bf8ac Mon Sep 17 00:00:00 2001 From: gwagjiug Date: Wed, 16 Apr 2025 16:26:46 +0900 Subject: [PATCH 1/2] fix: add validation to createRef and parseRef methods in SchemaComponentsMap --- src/schema-components-map.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/schema-components-map.ts b/src/schema-components-map.ts index 6ddc4dcc..88425f97 100644 --- a/src/schema-components-map.ts +++ b/src/schema-components-map.ts @@ -13,17 +13,26 @@ export class SchemaComponentsMap { this._data = []; } - createRef = (paths: string[]) => { + createRef = (paths: string[]): string => { + if (!Array.isArray(paths)) { + throw new Error(`Expected an array, but received: ${typeof paths}`); + } + if (paths.length === 0) { + throw new Error("Paths array cannot be empty."); + } return ["#", ...paths].join("/"); }; - parseRef = (ref: string) => { + parseRef = (ref: string): string[] => { + if (!ref.startsWith("#/")) { + throw new Error(`Invalid ref format: ${ref}. It should start with "#/"`); + } return ref.split("/"); }; createComponent( $ref: string, - rawTypeData: SchemaComponent["rawTypeData"], + rawTypeData: SchemaComponent["rawTypeData"] ): SchemaComponent { const parsed = this.parseRef($ref); const typeName = parsed[parsed.length - 1]!; @@ -60,8 +69,8 @@ export class SchemaComponentsMap { filter(...componentNames: (string[] | string)[]) { return this._data.filter((it) => componentNames.some((componentName) => - it.$ref.startsWith(`#/components/${componentName}`), - ), + it.$ref.startsWith(`#/components/${componentName}`) + ) ); } From 43e0d11a756a4613fdbd08a70effa3416a830d59 Mon Sep 17 00:00:00 2001 From: gwagjiug Date: Wed, 16 Apr 2025 16:34:40 +0900 Subject: [PATCH 2/2] refactor: simplify method signatures in SchemaComponentsMap --- src/schema-components-map.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/schema-components-map.ts b/src/schema-components-map.ts index 88425f97..ece1028a 100644 --- a/src/schema-components-map.ts +++ b/src/schema-components-map.ts @@ -13,7 +13,7 @@ export class SchemaComponentsMap { this._data = []; } - createRef = (paths: string[]): string => { + createRef = (paths: string[]) => { if (!Array.isArray(paths)) { throw new Error(`Expected an array, but received: ${typeof paths}`); } @@ -23,7 +23,7 @@ export class SchemaComponentsMap { return ["#", ...paths].join("/"); }; - parseRef = (ref: string): string[] => { + parseRef = (ref: string) => { if (!ref.startsWith("#/")) { throw new Error(`Invalid ref format: ${ref}. It should start with "#/"`); } @@ -32,7 +32,7 @@ export class SchemaComponentsMap { createComponent( $ref: string, - rawTypeData: SchemaComponent["rawTypeData"] + rawTypeData: SchemaComponent["rawTypeData"], ): SchemaComponent { const parsed = this.parseRef($ref); const typeName = parsed[parsed.length - 1]!; @@ -69,8 +69,8 @@ export class SchemaComponentsMap { filter(...componentNames: (string[] | string)[]) { return this._data.filter((it) => componentNames.some((componentName) => - it.$ref.startsWith(`#/components/${componentName}`) - ) + it.$ref.startsWith(`#/components/${componentName}`), + ), ); }