* 验证单个高亮 * @param id 高亮ID * @param highlight 高亮数据 * @returns 错误列表
(id: string, highlight: unknown)
| 63 | * @returns 错误列表 |
| 64 | */ |
| 65 | static validateHighlight(id: string, highlight: unknown): string[] { |
| 66 | const errors: string[] = []; |
| 67 | |
| 68 | if (!this.isRecord(highlight)) { |
| 69 | errors.push(`高亮 ${id}: 数据必须是对象`); |
| 70 | return errors; |
| 71 | } |
| 72 | |
| 73 | // 必需字段验证 |
| 74 | if (!highlight.text || typeof highlight.text !== 'string') { |
| 75 | errors.push(`高亮 ${id}: 缺少有效的text字段`); |
| 76 | } |
| 77 | |
| 78 | if (typeof highlight.position !== 'number') { |
| 79 | errors.push(`高亮 ${id}: position必须是数字`); |
| 80 | } |
| 81 | |
| 82 | if (typeof highlight.created !== 'number') { |
| 83 | errors.push(`高亮 ${id}: created必须是数字`); |
| 84 | } |
| 85 | |
| 86 | if (typeof highlight.updated !== 'number') { |
| 87 | errors.push(`高亮 ${id}: updated必须是数字`); |
| 88 | } |
| 89 | |
| 90 | // 可选字段验证 |
| 91 | if (highlight.backgroundColor && typeof highlight.backgroundColor !== 'string') { |
| 92 | errors.push(`高亮 ${id}: backgroundColor必须是字符串`); |
| 93 | } |
| 94 | |
| 95 | if (highlight.blockId && typeof highlight.blockId !== 'string') { |
| 96 | errors.push(`高亮 ${id}: blockId必须是字符串`); |
| 97 | } |
| 98 | |
| 99 | if (highlight.contextBefore && typeof highlight.contextBefore !== 'string') { |
| 100 | errors.push(`高亮 ${id}: contextBefore必须是字符串`); |
| 101 | } |
| 102 | |
| 103 | if (highlight.contextAfter && typeof highlight.contextAfter !== 'string') { |
| 104 | errors.push(`高亮 ${id}: contextAfter必须是字符串`); |
| 105 | } |
| 106 | |
| 107 | if (highlight.textFingerprint && typeof highlight.textFingerprint !== 'string') { |
| 108 | errors.push(`高亮 ${id}: textFingerprint必须是字符串`); |
| 109 | } |
| 110 | |
| 111 | if (highlight.isCloze && typeof highlight.isCloze !== 'boolean') { |
| 112 | errors.push(`高亮 ${id}: isCloze必须是布尔值`); |
| 113 | } |
| 114 | |
| 115 | // 验证评论数组 |
| 116 | if (highlight.comments) { |
| 117 | if (!Array.isArray(highlight.comments)) { |
| 118 | errors.push(`高亮 ${id}: comments必须是数组`); |
| 119 | } else { |
| 120 | highlight.comments.forEach((comment: unknown, index: number) => { |
| 121 | const commentErrors = this.validateComment(comment, `${id}.comments[${index}]`); |
| 122 | errors.push(...commentErrors); |
no test coverage detected