swagger-tools/src/tools/validate.ts
rimskij 70fada22d6 refactor: consolidate shared utilities and reduce code duplication
- Extract HTTP_METHODS constant to types.ts (eliminates duplication in 3 files)
- Add DEFAULT_CACHE_MAX_SIZE and DEFAULT_CACHE_TTL_MINUTES constants to cache.ts
- Create schema-utils.ts with getSchemas, findSchema, getSchemaNames, getSchemaCount
- Create spec-guards.ts with isOpenAPIV3, isSwaggerV2, getSpecVersion type guards
- Create tool-response.ts with successResponse, errorResponse helpers
- Update all tool handlers to use response helpers (~50 lines reduced)
- Update parser.ts to use type guards for version detection

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-12 17:45:18 +01:00

23 lines
926 B
TypeScript

import { z } from 'zod';
import { validateWithWarnings } from '../lib/validator.js';
import { formatValidation } from '../utils/format.js';
import { successResponse, errorResponse } from '../lib/tool-response.js';
import type { ToolResponse } from '../lib/tool-response.js';
export const validateToolName = 'validate-spec';
export const validateToolDescription = 'Validate an OpenAPI/Swagger specification against the schema. Reports errors and warnings.';
export const validateToolSchema = {
path: z.string().describe('Path to the OpenAPI/Swagger spec file (YAML or JSON)'),
};
export async function validateToolHandler({ path }: { path: string }): Promise<ToolResponse> {
try {
const result = await validateWithWarnings(path);
const text = formatValidation(result);
return successResponse(text, { ...result });
} catch (err) {
return errorResponse((err as Error).message, 'validating spec');
}
}