From def8bbcfe5dd6eae45cb422916b37898f35f923c Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 24 Oct 2025 13:11:06 +0200 Subject: [PATCH] Improves Anthropic models API response validation Enhances type checking and validation for API response structure Adds more robust type validation to ensure: - Correct response object structure - Presence of required model fields - Type safety for model data retrieval Prevents potential runtime errors by implementing stricter type checks --- api/anthropicClient.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/api/anthropicClient.ts b/api/anthropicClient.ts index 68d8d83..bcabcca 100644 --- a/api/anthropicClient.ts +++ b/api/anthropicClient.ts @@ -136,11 +136,19 @@ export class AnthropicClient { } // Validate basic response structure - const data = response.json as any; - if (!data || !Array.isArray(data.data)) { + const data = response.json as unknown; + if (!data || typeof data !== 'object' || !('data' in data) || !Array.isArray((data as { data: unknown }).data)) { throw new Error('Invalid models API response structure'); } + // Validate that each model has required fields + const modelsData = (data as { data: unknown[] }).data; + if (!modelsData.every((model: unknown) => + model && typeof model === 'object' && 'id' in model && typeof (model as { id: unknown }).id === 'string' + )) { + throw new Error('Invalid model structure in API response'); + } + return data as ModelsListResponse; } catch (error) { if (error instanceof Error) {