1
0

feat: 添加模板变量验证功能

- 在 ResourceValidator 中添加 validate_template_vars 方法
- 在验证阶段检查用户是否提供了模板所需的必需变量
- 缺少必需变量时返回 ERROR 级别错误
- 添加 9 个单元测试用例验证功能
- 同步更新 OpenSpec 规格文档
This commit is contained in:
2026-03-03 01:00:21 +08:00
parent e31a7e9bed
commit ef3fa6a06a
9 changed files with 468 additions and 89 deletions

View File

@@ -38,7 +38,7 @@ class ResourceValidator:
issues = []
location = f"幻灯片 {slide_index}, 元素 {elem_index}"
if not hasattr(element, 'src'):
if not hasattr(element, "src"):
return issues
src = element.src
@@ -52,12 +52,14 @@ class ResourceValidator:
# 检查文件是否存在
if not src_path.exists():
issues.append(ValidationIssue(
level="ERROR",
message=f"图片文件不存在: {src}",
location=location,
code="IMAGE_FILE_NOT_FOUND"
))
issues.append(
ValidationIssue(
level="ERROR",
message=f"图片文件不存在: {src}",
location=location,
code="IMAGE_FILE_NOT_FOUND",
)
)
return issues
@@ -75,36 +77,40 @@ class ResourceValidator:
issues = []
location = f"幻灯片 {slide_index}"
if 'template' not in slide_data:
if "template" not in slide_data:
return issues
template_name = slide_data['template']
template_name = slide_data["template"]
if not template_name:
return issues
# 检查是否提供了模板目录
if not self.template_dir:
issues.append(ValidationIssue(
level="ERROR",
message=f"使用了模板但未指定模板目录: {template_name}",
location=location,
code="TEMPLATE_DIR_NOT_SPECIFIED"
))
issues.append(
ValidationIssue(
level="ERROR",
message=f"使用了模板但未指定模板目录: {template_name}",
location=location,
code="TEMPLATE_DIR_NOT_SPECIFIED",
)
)
return issues
# 解析模板文件路径
template_path = self.template_dir / template_name
if not template_path.suffix:
template_path = template_path.with_suffix('.yaml')
template_path = template_path.with_suffix(".yaml")
# 检查模板文件是否存在
if not template_path.exists():
issues.append(ValidationIssue(
level="ERROR",
message=f"模板文件不存在: {template_name}",
location=location,
code="TEMPLATE_FILE_NOT_FOUND"
))
issues.append(
ValidationIssue(
level="ERROR",
message=f"模板文件不存在: {template_name}",
location=location,
code="TEMPLATE_FILE_NOT_FOUND",
)
)
return issues
# 验证模板文件结构
@@ -112,11 +118,71 @@ class ResourceValidator:
template_data = load_yaml_file(template_path)
validate_template_yaml(template_data, str(template_path))
except Exception as e:
issues.append(ValidationIssue(
level="ERROR",
message=f"模板文件结构错误: {template_name} - {str(e)}",
location=location,
code="TEMPLATE_STRUCTURE_ERROR"
))
issues.append(
ValidationIssue(
level="ERROR",
message=f"模板文件结构错误: {template_name} - {str(e)}",
location=location,
code="TEMPLATE_STRUCTURE_ERROR",
)
)
return issues
def validate_template_vars(self, slide_data: dict, slide_index: int) -> list:
"""
验证模板变量是否满足要求
Args:
slide_data: 幻灯片数据字典
slide_index: 幻灯片索引(从 1 开始)
Returns:
验证问题列表
"""
issues = []
location = f"幻灯片 {slide_index}"
if "template" not in slide_data:
return issues
template_name = slide_data["template"]
if not template_name:
return issues
if not self.template_dir:
return issues
template_path = self.template_dir / template_name
if not template_path.suffix:
template_path = template_path.with_suffix(".yaml")
if not template_path.exists():
return issues
try:
template_data = load_yaml_file(template_path)
validate_template_yaml(template_data, str(template_path))
except Exception:
return issues
template_vars = template_data.get("vars", [])
required_vars = [v["name"] for v in template_vars if v.get("required", False)]
if not required_vars:
return issues
user_vars = slide_data.get("vars", {})
missing_vars = [v for v in required_vars if v not in user_vars]
if missing_vars:
issues.append(
ValidationIssue(
level="ERROR",
message=f"缺少模板必需变量: {', '.join(missing_vars)}",
location=location,
code="MISSING_TEMPLATE_REQUIRED_VARS",
)
)
return issues