119 lines
3.7 KiB
JavaScript
119 lines
3.7 KiB
JavaScript
#!/usr/bin/env node
|
||
|
||
const YAML = require('yaml');
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
/**
|
||
* 验证OpenAPI文档的基本语法和结构
|
||
*/
|
||
function validateOpenAPIDoc() {
|
||
try {
|
||
console.log('🔍 开始验证营收系统OpenAPI文档...\n');
|
||
|
||
// 读取主文档
|
||
const mainDocPath = path.join(__dirname, 'main', 'openapi.yaml');
|
||
const mainContent = fs.readFileSync(mainDocPath, 'utf8');
|
||
const mainDoc = YAML.parse(mainContent);
|
||
|
||
// 验证基本结构
|
||
console.log('✅ 主文档语法正确');
|
||
console.log(`📋 API标题: ${mainDoc.info.title}`);
|
||
console.log(`📋 API版本: ${mainDoc.info.version}`);
|
||
console.log(`📋 OpenAPI版本: ${mainDoc.openapi}`);
|
||
|
||
// 验证服务器配置
|
||
if (mainDoc.servers && mainDoc.servers.length > 0) {
|
||
console.log(`🌐 配置了 ${mainDoc.servers.length} 个服务器环境:`);
|
||
mainDoc.servers.forEach((server, index) => {
|
||
console.log(` ${index + 1}. ${server.description}: ${server.url}`);
|
||
});
|
||
}
|
||
|
||
// 验证标签
|
||
if (mainDoc.tags && mainDoc.tags.length > 0) {
|
||
console.log(`🏷️ 定义了 ${mainDoc.tags.length} 个标签:`);
|
||
mainDoc.tags.forEach((tag, index) => {
|
||
console.log(` ${index + 1}. ${tag.name}: ${tag.description}`);
|
||
});
|
||
}
|
||
|
||
// 验证路径
|
||
if (mainDoc.paths) {
|
||
const pathCount = Object.keys(mainDoc.paths).length;
|
||
console.log(`🛣️ 定义了 ${pathCount} 个API路径:`);
|
||
Object.keys(mainDoc.paths).forEach((path, index) => {
|
||
console.log(` ${index + 1}. ${path}`);
|
||
});
|
||
}
|
||
|
||
// 验证组件文件
|
||
console.log('\n🔧 验证组件文件:');
|
||
const componentFiles = [
|
||
'components/schemas.yaml',
|
||
'components/responses.yaml',
|
||
'components/parameters.yaml',
|
||
'components/headers.yaml',
|
||
'components/security.yaml'
|
||
];
|
||
|
||
componentFiles.forEach(file => {
|
||
const filePath = path.join(__dirname, 'main', file);
|
||
if (fs.existsSync(filePath)) {
|
||
try {
|
||
const content = fs.readFileSync(filePath, 'utf8');
|
||
YAML.parse(content);
|
||
console.log(` ✅ ${file}`);
|
||
} catch (error) {
|
||
console.log(` ❌ ${file}: ${error.message}`);
|
||
}
|
||
} else {
|
||
console.log(` ⚠️ ${file}: 文件不存在`);
|
||
}
|
||
});
|
||
|
||
// 验证路径文件
|
||
console.log('\n🛤️ 验证路径文件:');
|
||
const pathFiles = [
|
||
'paths/bill-query.yaml',
|
||
'paths/bill-pay.yaml',
|
||
'paths/pay-invalid.yaml',
|
||
'paths/withholding-signing.yaml',
|
||
'paths/withholding-termination.yaml',
|
||
'paths/withholding-send-disc.yaml',
|
||
'paths/withholding-back-disc.yaml'
|
||
];
|
||
|
||
pathFiles.forEach(file => {
|
||
const filePath = path.join(__dirname, 'main', file);
|
||
if (fs.existsSync(filePath)) {
|
||
try {
|
||
const content = fs.readFileSync(filePath, 'utf8');
|
||
YAML.parse(content);
|
||
console.log(` ✅ ${file}`);
|
||
} catch (error) {
|
||
console.log(` ❌ ${file}: ${error.message}`);
|
||
}
|
||
} else {
|
||
console.log(` ⚠️ ${file}: 文件不存在`);
|
||
}
|
||
});
|
||
|
||
console.log('\n🎉 OpenAPI文档验证完成!');
|
||
console.log('\n📖 使用方法:');
|
||
console.log('1. 在Swagger Editor中打开 docs/main/openapi.yaml');
|
||
console.log('2. 或使用命令: npx swagger-ui-serve docs/main/openapi.yaml');
|
||
console.log('3. 或导入到Postman等API测试工具中');
|
||
|
||
} catch (error) {
|
||
console.error('❌ 验证失败:', error.message);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 如果直接运行此脚本
|
||
if (require.main === module) {
|
||
validateOpenAPIDoc();
|
||
}
|
||
|
||
module.exports = { validateOpenAPIDoc };
|