* 验证单个值
(value: any, schema: ToolParameterSchema, fieldPath: string)
| 34 | * 验证单个值 |
| 35 | */ |
| 36 | private static validateValue(value: any, schema: ToolParameterSchema, fieldPath: string): void { |
| 37 | // 检查类型 |
| 38 | if (!this.isValidType(value, schema.type)) { |
| 39 | throw new ToolValidationError( |
| 40 | `参数 ${fieldPath} 类型错误,期望: ${schema.type},实际: ${typeof value}`, |
| 41 | fieldPath, |
| 42 | value |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | // 检查枚举值 |
| 47 | if (schema.enum && !schema.enum.includes(value)) { |
| 48 | throw new ToolValidationError( |
| 49 | `参数 ${fieldPath} 值必须是: ${schema.enum.join(', ')} 中的一个`, |
| 50 | fieldPath, |
| 51 | value |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | // 递归验证数组和对象 |
| 56 | if (schema.type === 'array' && Array.isArray(value) && schema.items) { |
| 57 | value.forEach((item, index) => { |
| 58 | this.validateValue(item, schema.items!, `${fieldPath}[${index}]`); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | if (schema.type === 'object' && value && typeof value === 'object' && schema.properties) { |
| 63 | for (const [key, subValue] of Object.entries(value)) { |
| 64 | const subSchema = schema.properties[key]; |
| 65 | if (subSchema) { |
| 66 | this.validateValue(subValue, subSchema, `${fieldPath}.${key}`); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * 检查值类型是否匹配 |
no test coverage detected