mirror of
https://github.com/10h30/blog-balodeplao.git
synced 2026-05-12 15:21:15 +09:00
feat: enhance taxonomy data validation and improve error handling in buildMap function
This commit is contained in:
+40
-10
@@ -11,18 +11,48 @@ export type TaxonomyItem = {
|
|||||||
|
|
||||||
export type TaxonomyMap = Map<string, TaxonomyItem>;
|
export type TaxonomyMap = Map<string, TaxonomyItem>;
|
||||||
|
|
||||||
function buildMap(data: unknown[]): TaxonomyMap {
|
export type TaxonomyType = "category" | "destination" | "tag";
|
||||||
return new Map((data as TaxonomyItem[]).map((item) => [item.slug, item]));
|
|
||||||
|
function isTaxonomyItem(value: unknown): value is TaxonomyItem {
|
||||||
|
if (typeof value !== "object" || value === null) return false;
|
||||||
|
const v = value as Record<string, unknown>;
|
||||||
|
return (
|
||||||
|
typeof v.slug === "string" &&
|
||||||
|
typeof v.name === "string" &&
|
||||||
|
(v.parent === undefined || typeof v.parent === "string") &&
|
||||||
|
(v.description === undefined || typeof v.description === "string")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const taxonomies: Record<string, TaxonomyMap> = {
|
function buildMap(data: TaxonomyItem[]): TaxonomyMap {
|
||||||
category: buildMap(categoryData),
|
if (!Array.isArray(data)) {
|
||||||
destination: buildMap(destinationData),
|
throw new Error(`[taxonomy] expected an array but received ${typeof data}`);
|
||||||
tag: buildMap(tagData),
|
}
|
||||||
|
return new Map(
|
||||||
|
data.map((item, i) => {
|
||||||
|
if (!isTaxonomyItem(item)) {
|
||||||
|
throw new Error(
|
||||||
|
`[taxonomy] invalid item at index ${i}: ${JSON.stringify(item)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return [item.slug, item];
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const taxonomies: Record<TaxonomyType, TaxonomyMap> = {
|
||||||
|
category: buildMap(categoryData as TaxonomyItem[]),
|
||||||
|
destination: buildMap(destinationData as TaxonomyItem[]),
|
||||||
|
tag: buildMap(tagData as TaxonomyItem[]),
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getTaxonomyMap(type: string): TaxonomyMap {
|
export function getTaxonomyMap(type: TaxonomyType): TaxonomyMap {
|
||||||
return taxonomies[type] ?? new Map();
|
if (!(type in taxonomies)) {
|
||||||
|
throw new Error(
|
||||||
|
`[taxonomy] unknown taxonomy type "${type}". Expected one of: ${Object.keys(taxonomies).join(", ")}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return taxonomies[type];
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildHierarchicalPath(
|
function buildHierarchicalPath(
|
||||||
@@ -37,12 +67,12 @@ function buildHierarchicalPath(
|
|||||||
return `${buildHierarchicalPath(item.parent, map, visited)}/${slug}`;
|
return `${buildHierarchicalPath(item.parent, map, visited)}/${slug}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTaxonomyPath(type: string, slug: string): string {
|
export function getTaxonomyPath(type: TaxonomyType, slug: string): string {
|
||||||
return buildHierarchicalPath(slug, getTaxonomyMap(type));
|
return buildHierarchicalPath(slug, getTaxonomyMap(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTaxonomyItem(
|
export function getTaxonomyItem(
|
||||||
type: string,
|
type: TaxonomyType,
|
||||||
slug: string,
|
slug: string,
|
||||||
): TaxonomyItem | undefined {
|
): TaxonomyItem | undefined {
|
||||||
return getTaxonomyMap(type).get(slug);
|
return getTaxonomyMap(type).get(slug);
|
||||||
|
|||||||
Reference in New Issue
Block a user