requirements-analysis
Version:
简化的需求分析MCP服务 - 基于AI软件工程(优化版)6步流程
144 lines (122 loc) • 4.35 kB
text/typescript
/**
* 参数验证工具
*/
import { ProjectInfo, ValidationError } from '../types';
export class Validator {
/**
* 验证项目信息
*/
static validateProjectInfo(projectInfo: any): ValidationError[] {
const errors: ValidationError[] = [];
// 必填字段验证
const requiredFields = [
{ field: 'projectName', name: '项目名称' },
{ field: 'projectType', name: '项目类型' },
{ field: 'industry', name: '所属行业' },
{ field: 'background', name: '项目背景' },
{ field: 'objectives', name: '核心目标' }
];
for (const { field, name } of requiredFields) {
if (!projectInfo[field] || typeof projectInfo[field] !== 'string' || projectInfo[field].trim().length === 0) {
errors.push({
field,
message: `${name}不能为空`,
suggestion: `请提供具体的${name}`
});
}
}
// 字段长度验证
if (projectInfo.projectName && projectInfo.projectName.length > 100) {
errors.push({
field: 'projectName',
message: '项目名称过长',
suggestion: '项目名称应控制在100字符以内'
});
}
if (projectInfo.background && projectInfo.background.length < 20) {
errors.push({
field: 'background',
message: '项目背景描述过于简单',
suggestion: '请详细描述项目背景,至少20个字符'
});
}
if (projectInfo.objectives && projectInfo.objectives.length < 20) {
errors.push({
field: 'objectives',
message: '核心目标描述过于简单',
suggestion: '请详细描述核心目标,建议包含量化指标'
});
}
// 枚举值验证
const validProjectTypes = ['new', 'upgrade', 'integration', 'other'];
if (projectInfo.projectType && !validProjectTypes.includes(projectInfo.projectType)) {
errors.push({
field: 'projectType',
message: '项目类型无效',
suggestion: `项目类型必须是以下之一:${validProjectTypes.join(', ')}`
});
}
const validDeployments = ['cloud', 'onpremise', 'hybrid'];
if (projectInfo.deployment && !validDeployments.includes(projectInfo.deployment)) {
errors.push({
field: 'deployment',
message: '部署要求无效',
suggestion: `部署要求必须是以下之一:${validDeployments.join(', ')}`
});
}
return errors;
}
/**
* 生成参数建议
*/
static generateParameterSuggestion(): string {
return `
## 📋 参数格式示例
\`\`\`json
{
"projectName": "智能客服系统",
"projectType": "new",
"industry": "金融科技",
"background": "当前客服响应时间过长,客户满意度下降,人工成本高昂,需要智能化客服系统提升服务效率和用户体验",
"objectives": "提升客服响应速度50%,降低人工成本30万/年,客户满意度提升至95%以上,支持7x24小时服务",
"budget": "100-150万",
"timeline": "6个月",
"teamSize": "8人团队",
"deployment": "cloud",
"specialRequirements": "需要支持多语言,集成现有CRM系统"
}
\`\`\`
## 📝 字段说明
### 必填字段
- **projectName**: 项目名称(1-100字符)
- **projectType**: 项目类型(new/upgrade/integration/other)
- **industry**: 所属行业(如:金融科技、制造业、教育等)
- **background**: 项目背景(至少20字符,详细描述现状和问题)
- **objectives**: 核心目标(至少20字符,建议包含量化指标)
### 可选字段
- **budget**: 项目预算
- **timeline**: 项目周期
- **teamSize**: 团队规模
- **deployment**: 部署要求(cloud/onpremise/hybrid)
- **specialRequirements**: 特殊要求
`;
}
/**
* 格式化验证错误
*/
static formatValidationErrors(errors: ValidationError[]): string {
if (errors.length === 0) {
return '';
}
let message = '🚫 参数验证失败,请检查以下问题:\n\n';
errors.forEach((error, index) => {
message += `${index + 1}. **${error.field}**: ${error.message}\n`;
if (error.suggestion) {
message += ` 💡 建议:${error.suggestion}\n`;
}
message += '\n';
});
message += this.generateParameterSuggestion();
return message;
}
}