UNPKG

@memory-bank/mcp

Version:

Memory-enabled Co-Pilot (MCP) server for managing project documentation and context

100 lines 3.82 kB
import { DomainError, DomainErrorCodes } from '../../shared/errors/DomainError.js'; import { SCHEMA_VERSION, // ここで SCHEMA_VERSION をインポート BaseJsonDocumentV2Schema, DocumentMetadataV2Schema, // DocumentMetadataV2Schema をインポート BranchContextJsonV2Schema, ActiveContextJsonV2Schema, ProgressJsonV2Schema, SystemPatternsJsonV2Schema, } from '@memory-bank/schemas'; /** * Implementation of IDocumentValidator using Zod schemas * This keeps validation logic separate from domain entities */ export class ZodDocumentValidator { /** * Validates content for a specific document type * @param documentType Type of document * @param content Content to validate * @returns true if valid, throws error if not */ validateContent(documentType, content) { try { switch (documentType) { case 'branch_context': BranchContextJsonV2Schema.shape.content.parse(content); break; case 'active_context': ActiveContextJsonV2Schema.shape.content.parse(content); break; case 'progress': ProgressJsonV2Schema.shape.content.parse(content); break; case 'system_patterns': SystemPatternsJsonV2Schema.shape.content.parse(content); break; default: if (Object.keys(content).length === 0) { throw new Error('Content cannot be empty'); } break; } return true; } catch (error) { throw new DomainError(DomainErrorCodes.VALIDATION_ERROR, `Invalid content for ${documentType} document: ${error.message}`); } } /** * Validates a complete document object * @param document Document to validate * @returns true if valid, throws error if not */ validateDocument(document) { try { BaseJsonDocumentV2Schema.parse(document); const baseDocument = document; const documentType = baseDocument.metadata.documentType; switch (documentType) { case 'branch_context': BranchContextJsonV2Schema.parse(document); break; case 'active_context': ActiveContextJsonV2Schema.parse(document); break; case 'progress': ProgressJsonV2Schema.parse(document); break; case 'system_patterns': SystemPatternsJsonV2Schema.parse(document); break; default: BaseJsonDocumentV2Schema.parse(document); break; } return true; } catch (error) { if (error instanceof DomainError) { throw error; } throw new DomainError(DomainErrorCodes.VALIDATION_ERROR, `Invalid document structure: ${error.message}`); } } /** * Validates metadata structure * @param metadata Metadata to validate * @returns true if valid, throws error if not */ validateMetadata(metadata) { try { DocumentMetadataV2Schema.parse(metadata); // DocumentMetadataV2Schema を直接使う return true; } catch (error) { throw new DomainError(DomainErrorCodes.VALIDATION_ERROR, `Invalid document metadata: ${error.message}`); } } /** * @returns The current schema version */ getSchemaVersion() { return SCHEMA_VERSION; } } //# sourceMappingURL=ZodDocumentValidator.js.map