- 在 ResourceValidator 中添加 validate_template_vars 方法 - 在验证阶段检查用户是否提供了模板所需的必需变量 - 缺少必需变量时返回 ERROR 级别错误 - 添加 9 个单元测试用例验证功能 - 同步更新 OpenSpec 规格文档
189 lines
5.4 KiB
Python
189 lines
5.4 KiB
Python
"""
|
|
资源验证器
|
|
|
|
验证 YAML 文件引用的外部资源是否存在。
|
|
"""
|
|
|
|
from pathlib import Path
|
|
from validators.result import ValidationIssue
|
|
from loaders.yaml_loader import load_yaml_file, validate_template_yaml
|
|
|
|
|
|
class ResourceValidator:
|
|
"""资源验证器"""
|
|
|
|
def __init__(self, yaml_dir: Path, template_dir: Path = None):
|
|
"""
|
|
初始化资源验证器
|
|
|
|
Args:
|
|
yaml_dir: YAML 文件所在目录
|
|
template_dir: 模板文件目录(可选)
|
|
"""
|
|
self.yaml_dir = yaml_dir
|
|
self.template_dir = template_dir
|
|
|
|
def validate_image(self, element, slide_index: int, elem_index: int) -> list:
|
|
"""
|
|
验证图片文件是否存在
|
|
|
|
Args:
|
|
element: 图片元素对象
|
|
slide_index: 幻灯片索引(从 1 开始)
|
|
elem_index: 元素索引(从 1 开始)
|
|
|
|
Returns:
|
|
验证问题列表
|
|
"""
|
|
issues = []
|
|
location = f"幻灯片 {slide_index}, 元素 {elem_index}"
|
|
|
|
if not hasattr(element, "src"):
|
|
return issues
|
|
|
|
src = element.src
|
|
if not src:
|
|
return issues
|
|
|
|
# 解析路径(支持相对路径和绝对路径)
|
|
src_path = Path(src)
|
|
if not src_path.is_absolute():
|
|
src_path = self.yaml_dir / src
|
|
|
|
# 检查文件是否存在
|
|
if not src_path.exists():
|
|
issues.append(
|
|
ValidationIssue(
|
|
level="ERROR",
|
|
message=f"图片文件不存在: {src}",
|
|
location=location,
|
|
code="IMAGE_FILE_NOT_FOUND",
|
|
)
|
|
)
|
|
|
|
return issues
|
|
|
|
def validate_template(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:
|
|
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")
|
|
|
|
# 检查模板文件是否存在
|
|
if not template_path.exists():
|
|
issues.append(
|
|
ValidationIssue(
|
|
level="ERROR",
|
|
message=f"模板文件不存在: {template_name}",
|
|
location=location,
|
|
code="TEMPLATE_FILE_NOT_FOUND",
|
|
)
|
|
)
|
|
return issues
|
|
|
|
# 验证模板文件结构
|
|
try:
|
|
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",
|
|
)
|
|
)
|
|
|
|
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
|