Skip to content

Commit a51f6b1

Browse files
committed
fix: add validation to createRef and parseRef methods in SchemaComponentsMap
1 parent b0eaa72 commit a51f6b1

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/schema-components-map.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,26 @@ export class SchemaComponentsMap {
1313
this._data = [];
1414
}
1515

16-
createRef = (paths: string[]) => {
16+
createRef = (paths: string[]): string => {
17+
if (!Array.isArray(paths)) {
18+
throw new Error(`Expected an array, but received: ${typeof paths}`);
19+
}
20+
if (paths.length === 0) {
21+
throw new Error("Paths array cannot be empty.");
22+
}
1723
return ["#", ...paths].join("/");
1824
};
1925

20-
parseRef = (ref: string) => {
26+
parseRef = (ref: string): string[] => {
27+
if (!ref.startsWith("#/")) {
28+
throw new Error(`Invalid ref format: ${ref}. It should start with "#/"`);
29+
}
2130
return ref.split("/");
2231
};
2332

2433
createComponent(
2534
$ref: string,
26-
rawTypeData: SchemaComponent["rawTypeData"],
35+
rawTypeData: SchemaComponent["rawTypeData"]
2736
): SchemaComponent {
2837
const parsed = this.parseRef($ref);
2938
const typeName = parsed[parsed.length - 1]!;
@@ -60,8 +69,8 @@ export class SchemaComponentsMap {
6069
filter(...componentNames: (string[] | string)[]) {
6170
return this._data.filter((it) =>
6271
componentNames.some((componentName) =>
63-
it.$ref.startsWith(`#/components/${componentName}`),
64-
),
72+
it.$ref.startsWith(`#/components/${componentName}`)
73+
)
6574
);
6675
}
6776

0 commit comments

Comments
 (0)