1
0

feat: 重构配置生命周期为 Authoring/Normalized/Resolved 三层

将变量替换和 expect 简写展开统一放入 Normalized 阶段,
运行时 AJV 使用 Normalized schema,导出 schema 面向 Authoring Config。

主要变更:
- 新增 normalizer.ts 实现 normalizeAuthoringConfig()
- 拆分 Authoring/Normalized 双 schema,checker 接口支持 authoring/normalized 片段
- config-loader 流程:normalize → Normalized AJV → semantic → resolve
- validator 兼容层自动分派 raw/normalized expect 形态
- 删除 rawExpect,store.expect 列写入 null
- Authoring schema 对 integer/boolean/enum 字段接受变量引用
- 修复 DB/HTTP validate 入口守卫和 LLM options integer 变量引用
- 优化 compact() 避免 undefined 覆盖隐患
- 移除 content.ts 恒为 true 的前置条件
- 同步 5 个主规范并归档 change
This commit is contained in:
2026-05-22 14:00:47 +08:00
parent 6e53c8130d
commit cf847ccd7a
56 changed files with 1717 additions and 656 deletions

View File

@@ -58,6 +58,7 @@ export function validateRawKeyedExpectations(
targetName?: string,
options?: { caseInsensitive?: boolean },
): ConfigValidationIssue[] {
if (Array.isArray(value)) return validateNormalizedKeyedExpectations(value, path, targetName, options);
if (!isPlainRecord(value)) return [issue("invalid-type", path, "必须为对象", targetName)];
const issues: ConfigValidationIssue[] = [];
@@ -196,12 +197,76 @@ function validateMatcherValue(key: string, value: unknown, path: string, targetN
}
}
function validateNormalizedContentExpectation(
expectation: Record<string, unknown>,
path: string,
targetName?: string,
): ConfigValidationIssue[] {
const kind = expectation["kind"];
const matcherPath = joinPath(path, "matcher");
const issues = validateRawValueExpectation(expectation["matcher"], matcherPath, targetName);
switch (kind) {
case "css":
if (!isString(expectation["selector"]) || expectation["selector"].trim() === "") {
issues.push(issue("invalid-type", joinPath(path, "selector"), "必须为非空字符串", targetName));
}
if ("attr" in expectation && !isString(expectation["attr"])) {
issues.push(issue("invalid-type", joinPath(path, "attr"), "必须为字符串", targetName));
}
return issues;
case "json":
return isString(expectation["path"])
? [...issues, ...validateJsonPath(expectation["path"], path, targetName)]
: [...issues, issue("invalid-type", joinPath(path, "path"), "必须为字符串", targetName)];
case "value":
return issues;
case "xpath":
return isString(expectation["path"])
? [...issues, ...validateXpathExpectation({ path: expectation["path"] }, path, targetName)]
: [...issues, issue("invalid-type", joinPath(path, "path"), "必须为非空字符串", targetName)];
default:
return [...issues, issue("invalid-type", joinPath(path, "kind"), "必须为 value、json、css 或 xpath", targetName)];
}
}
function validateNormalizedKeyedExpectations(
value: unknown[],
path: string,
targetName?: string,
options?: { caseInsensitive?: boolean },
): ConfigValidationIssue[] {
const issues: ConfigValidationIssue[] = [];
const seen = new Map<string, string>();
for (let i = 0; i < value.length; i++) {
const itemPath = `${path}[${i}]`;
const item = value[i];
if (!isPlainRecord(item)) {
issues.push(issue("invalid-type", itemPath, "必须为对象", targetName));
continue;
}
if (!isString(item["key"])) {
issues.push(issue("invalid-type", joinPath(itemPath, "key"), "必须为字符串", targetName));
} else if (options?.caseInsensitive) {
const normalized = item["key"].toLowerCase();
const prev = seen.get(normalized);
if (prev !== undefined) {
issues.push(issue("duplicate-key", joinPath(itemPath, "key"), `与 "${prev}" 大小写归一化后重复`, targetName));
} else {
seen.set(normalized, item["key"]);
}
}
issues.push(...validateRawValueExpectation(item["matcher"], joinPath(itemPath, "matcher"), targetName));
}
return issues;
}
function validateRawContentExpectation(
expectation: unknown,
path: string,
targetName?: string,
): ConfigValidationIssue[] {
if (!isPlainRecord(expectation)) return [issue("invalid-type", path, "必须为对象", targetName)];
if (isString(expectation["kind"])) return validateNormalizedContentExpectation(expectation, path, targetName);
const issues: ConfigValidationIssue[] = [];
const extractors = Object.keys(expectation).filter((key) => CONTENT_EXTRACTOR_KEY_SET.has(key));