Skip to content

Commit c55b1e2

Browse files
committed
fix(cli): add type guard for redundant_to_json parsing
Address code review feedback: add isValidRedundantToItem type guard to validate JSON structure before processing. Filter out invalid items instead of blindly casting to any. Use nullish coalescing for safer property access.
1 parent 0293155 commit c55b1e2

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

cli/lib/checkup.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@ export async function getCurrentDatabaseInfo(client: Client, pgMajorVersion: num
641641
};
642642
}
643643

644+
/**
645+
* Type guard to validate redundant_to_json item structure.
646+
* Returns true if item is a valid object (may have expected properties).
647+
*/
648+
function isValidRedundantToItem(item: unknown): item is Record<string, unknown> {
649+
return typeof item === "object" && item !== null && !Array.isArray(item);
650+
}
651+
644652
/**
645653
* Get redundant indexes (H004)
646654
* SQL loaded from config/pgwatch-prometheus/metrics.yml (redundant_indexes)
@@ -659,15 +667,17 @@ export async function getRedundantIndexes(client: Client, pgMajorVersion: number
659667
const jsonStr = String(transformed.redundant_to_json || "[]");
660668
const parsed = JSON.parse(jsonStr);
661669
if (Array.isArray(parsed)) {
662-
redundantTo = parsed.map((item: any) => {
663-
const sizeBytes = parseInt(String(item.index_size_bytes || 0), 10);
664-
return {
665-
index_name: String(item.index_name || ""),
666-
index_definition: String(item.index_definition || ""),
667-
index_size_bytes: sizeBytes,
668-
index_size_pretty: formatBytes(sizeBytes),
669-
};
670-
});
670+
redundantTo = parsed
671+
.filter(isValidRedundantToItem)
672+
.map((item) => {
673+
const sizeBytes = parseInt(String(item.index_size_bytes ?? 0), 10);
674+
return {
675+
index_name: String(item.index_name ?? ""),
676+
index_definition: String(item.index_definition ?? ""),
677+
index_size_bytes: sizeBytes,
678+
index_size_pretty: formatBytes(sizeBytes),
679+
};
680+
});
671681
}
672682
} catch (err) {
673683
const errorMsg = err instanceof Error ? err.message : String(err);

0 commit comments

Comments
 (0)