83 lines
2.3 KiB
Dart
83 lines
2.3 KiB
Dart
import 'package:swagger_generator_flutter/pipeline/validate/core/validation_context.dart';
|
|
import 'package:swagger_generator_flutter/pipeline/validate/core/validation_result.dart';
|
|
import 'package:swagger_generator_flutter/pipeline/validate/core/validation_rule.dart';
|
|
|
|
/// 服务器配置验证规则
|
|
class ServerValidationRule extends ValidationRule {
|
|
@override
|
|
String get id => 'server_validation';
|
|
|
|
@override
|
|
String get name => '服务器配置验证';
|
|
|
|
@override
|
|
ValidationResult validate(ValidationContext context) {
|
|
final servers = context.document.servers;
|
|
final errors = <ValidationError>[];
|
|
final warnings = <ValidationWarning>[];
|
|
|
|
if (servers.isEmpty) {
|
|
warnings.add(
|
|
const ValidationWarning(
|
|
path: 'servers',
|
|
message: '未定义服务器配置',
|
|
suggestion: '建议添加至少一个服务器配置',
|
|
),
|
|
);
|
|
return ValidationResult(isValid: true, warnings: warnings);
|
|
}
|
|
|
|
for (var i = 0; i < servers.length; i++) {
|
|
final server = servers[i];
|
|
final path = 'servers[$i]';
|
|
|
|
if (server.url.isEmpty) {
|
|
errors.add(
|
|
ValidationError(
|
|
path: '$path.url',
|
|
message: '服务器 URL 不能为空',
|
|
type: ValidationErrorType.required,
|
|
),
|
|
);
|
|
} else if (!_isValidUrl(server.url)) {
|
|
errors.add(
|
|
ValidationError(
|
|
path: '$path.url',
|
|
message: '服务器 URL 格式无效: ${server.url}',
|
|
type: ValidationErrorType.format,
|
|
suggestion: '请使用有效的 URL 格式,如 "https://api.example.com"',
|
|
),
|
|
);
|
|
}
|
|
|
|
// 验证服务器变量
|
|
server.variables.forEach((name, variable) {
|
|
if (variable.defaultValue.isEmpty) {
|
|
errors.add(
|
|
ValidationError(
|
|
path: '$path.variables.$name.default',
|
|
message: '服务器变量必须有默认值',
|
|
type: ValidationErrorType.required,
|
|
),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
return ValidationResult(
|
|
isValid: errors.isEmpty,
|
|
errors: errors,
|
|
warnings: warnings,
|
|
);
|
|
}
|
|
|
|
bool _isValidUrl(String url) {
|
|
try {
|
|
final uri = Uri.parse(url);
|
|
return uri.hasScheme && (uri.scheme == 'http' || uri.scheme == 'https');
|
|
} on Object {
|
|
return false;
|
|
}
|
|
}
|
|
}
|