export class DocumentMetadataError extends Error { constructor( readonly code: string, message: string, ) { super(message); this.name = "DocumentMetadataError"; } } export function validateDescription(description: string): string { const trimmed = description.trim(); if (!trimmed) { throw new DocumentMetadataError("invalid_description", "description must not be empty"); } if (trimmed.includes("\n")) { throw new DocumentMetadataError("invalid_description", "description must be exactly one line"); } if (sentenceCount(trimmed) > 1) { throw new DocumentMetadataError( "invalid_description", "description must be exactly one sentence", ); } return trimmed; } function sentenceCount(value: string): number { // Count only punctuation that actually terminates a sentence, not dots inside // technical tokens like Node.js, .ts, 1.2, or cl.exe. return value .split(/[.!?]+(?=(?:["')\]]*)(?:\s|$))/) .filter((segment) => /[a-z0-9]/i.test(segment)) .length; }