- 引入共享 ValueMatcher(equals/contains/regex/exists/empty/gt/gte/lt/lte) - 引入共享 ContentRules 数组(direct/json/css/xpath 提取器) - 引入共享 KeyValueExpect(动态键值断言,字面量等价 equals) - maxDurationMs → durationMs: ValueMatcher(所有 checker) - match → regex(固定无 flags) - Ping max* → packetLossPercent/avgLatencyMs/maxLatencyMs(ValueMatcher) - LLM finishReason/rawFinishReason → ValueMatcher - DB 新增 result: ContentRules - TCP banner → ContentRules 数组 - 删除旧模块:operator.ts、validate-operator.ts、duration.ts、body.ts、text.ts、output.ts - 更新全部 checker schema/validate/expect/execute - 更新 probe-config.schema.json、probes.example.yaml - 更新 README.md、DEVELOPMENT.md(含 expect 字段选择规范) - 同步 10 个 delta specs 到主 specs,归档 change
208 lines
7.5 KiB
TypeScript
208 lines
7.5 KiB
TypeScript
import { isNumber, isPlainObject, isString } from "es-toolkit";
|
|
|
|
import type { ConfigValidationIssue } from "../../schema/issues";
|
|
import type { CheckerValidationInput } from "../types";
|
|
|
|
import { validateContentRules, validateValueMatcher } from "../../expect/validate-matcher";
|
|
import { issue, joinPath } from "../../schema/issues";
|
|
|
|
const VALID_ENCODINGS = new Set(["base64", "hex", "text"]);
|
|
|
|
export function validateUdpConfig(input: CheckerValidationInput): ConfigValidationIssue[] {
|
|
const issues: ConfigValidationIssue[] = [];
|
|
|
|
issues.push(...validateUdpDefaults(input));
|
|
|
|
for (let i = 0; i < input.targets.length; i++) {
|
|
const target = input.targets[i] as unknown;
|
|
if (!isPlainObject(target)) continue;
|
|
if (target["type"] !== "udp") continue;
|
|
issues.push(...validateUdpTarget(target, `targets[${i}]`));
|
|
}
|
|
|
|
return issues;
|
|
}
|
|
|
|
function getTargetName(target: Record<string, unknown>): string | undefined {
|
|
if (isString(target["name"])) return target["name"];
|
|
return isString(target["id"]) ? target["id"] : undefined;
|
|
}
|
|
|
|
function validateEncoding(value: unknown, path: string, targetName: string | undefined): ConfigValidationIssue[] {
|
|
if (value === undefined) return [];
|
|
if (!isString(value) || !VALID_ENCODINGS.has(value)) {
|
|
return [issue("invalid-value", path, "必须为 text、hex 或 base64", targetName)];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
function validateSize(value: unknown, path: string, targetName: string | undefined): ConfigValidationIssue[] {
|
|
if (value === undefined) return [];
|
|
if (!isString(value) && !(isNumber(value) && Number.isFinite(value) && value >= 0)) {
|
|
return [issue("invalid-value", path, "必须为合法 size 值", targetName)];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
function validateUdpDefaults(input: CheckerValidationInput): ConfigValidationIssue[] {
|
|
const issues: ConfigValidationIssue[] = [];
|
|
const defaults = input.defaults["udp"];
|
|
if (defaults === undefined || defaults === null || !isPlainObject(defaults)) return issues;
|
|
|
|
const targetName = "defaults.udp";
|
|
|
|
issues.push(...validateEncoding(defaults["encoding"], joinPath("defaults.udp", "encoding"), targetName));
|
|
issues.push(
|
|
...validateEncoding(defaults["responseEncoding"], joinPath("defaults.udp", "responseEncoding"), targetName),
|
|
);
|
|
issues.push(...validateSize(defaults["maxResponseBytes"], joinPath("defaults.udp", "maxResponseBytes"), targetName));
|
|
|
|
const allowedKeys = new Set(["encoding", "maxResponseBytes", "responseEncoding"]);
|
|
for (const key of Object.keys(defaults)) {
|
|
if (!allowedKeys.has(key)) {
|
|
issues.push(issue("unknown-field", joinPath("defaults.udp", key), "是未知字段", targetName));
|
|
}
|
|
}
|
|
|
|
return issues;
|
|
}
|
|
|
|
function validateUdpExpect(target: Record<string, unknown>, path: string): ConfigValidationIssue[] {
|
|
const targetName = getTargetName(target);
|
|
const expect = target["expect"];
|
|
if (expect === undefined || expect === null || !isPlainObject(expect)) return [];
|
|
const issues: ConfigValidationIssue[] = [];
|
|
const expectPath = joinPath(path, "expect");
|
|
const responded: unknown = expect["responded"];
|
|
|
|
if (responded !== undefined && typeof responded !== "boolean") {
|
|
issues.push(issue("invalid-type", joinPath(expectPath, "responded"), "必须为布尔值", targetName));
|
|
}
|
|
|
|
if (expect["durationMs"] !== undefined) {
|
|
issues.push(...validateValueMatcher(expect["durationMs"], joinPath(expectPath, "durationMs"), targetName));
|
|
}
|
|
|
|
if (expect["response"] !== undefined) {
|
|
issues.push(...validateContentRules(expect["response"], joinPath(expectPath, "response"), targetName));
|
|
}
|
|
|
|
if (expect["responseSize"] !== undefined) {
|
|
issues.push(...validateValueMatcher(expect["responseSize"], joinPath(expectPath, "responseSize"), targetName));
|
|
}
|
|
|
|
if (expect["sourceHost"] !== undefined) {
|
|
issues.push(...validateValueMatcher(expect["sourceHost"], joinPath(expectPath, "sourceHost"), targetName));
|
|
}
|
|
|
|
if (expect["sourcePort"] !== undefined) {
|
|
issues.push(...validateValueMatcher(expect["sourcePort"], joinPath(expectPath, "sourcePort"), targetName));
|
|
}
|
|
|
|
const respondedFalse = responded === false;
|
|
if (respondedFalse) {
|
|
if (expect["response"] !== undefined || expect["responseSize"] !== undefined) {
|
|
issues.push(
|
|
issue(
|
|
"invalid-value",
|
|
joinPath(expectPath, "responded"),
|
|
"响应内容或大小断言需要 expect.responded 为 true",
|
|
targetName,
|
|
),
|
|
);
|
|
}
|
|
if (expect["sourceHost"] !== undefined || expect["sourcePort"] !== undefined) {
|
|
issues.push(
|
|
issue(
|
|
"invalid-value",
|
|
joinPath(expectPath, "responded"),
|
|
"响应来源断言需要 expect.responded 为 true",
|
|
targetName,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
const allowedKeys = new Set(["durationMs", "responded", "response", "responseSize", "sourceHost", "sourcePort"]);
|
|
for (const key of Object.keys(expect)) {
|
|
if (!allowedKeys.has(key)) {
|
|
issues.push(issue("unknown-field", joinPath(expectPath, key), "是未知字段", targetName));
|
|
}
|
|
}
|
|
|
|
return issues;
|
|
}
|
|
|
|
function validateUdpTarget(target: Record<string, unknown>, path: string): ConfigValidationIssue[] {
|
|
const issues: ConfigValidationIssue[] = [];
|
|
const targetName = getTargetName(target);
|
|
const udp = target["udp"];
|
|
|
|
if (!isPlainObject(udp)) {
|
|
issues.push(issue("required", joinPath(path, "udp"), "缺少 udp 配置分组", targetName));
|
|
issues.push(...validateUdpExpect(target, path));
|
|
return issues;
|
|
}
|
|
|
|
if (!isString(udp["host"]) || udp["host"].trim() === "") {
|
|
issues.push(issue("required", joinPath(joinPath(path, "udp"), "host"), "缺少 udp.host 字段", targetName));
|
|
}
|
|
|
|
if (udp["port"] === undefined) {
|
|
issues.push(issue("required", joinPath(joinPath(path, "udp"), "port"), "缺少 udp.port 字段", targetName));
|
|
} else if (!isNumber(udp["port"]) || !Number.isInteger(udp["port"]) || udp["port"] < 1 || udp["port"] > 65535) {
|
|
issues.push(
|
|
issue("invalid-value", joinPath(joinPath(path, "udp"), "port"), "必须为 1-65535 之间的整数", targetName),
|
|
);
|
|
}
|
|
|
|
const encoding: unknown = udp["encoding"];
|
|
issues.push(...validateEncoding(encoding, joinPath(joinPath(path, "udp"), "encoding"), targetName));
|
|
|
|
if (encoding === "hex" && isString(udp["payload"])) {
|
|
const hexPattern = /^[0-9a-fA-F]*$/;
|
|
if (!hexPattern.test(udp["payload"])) {
|
|
issues.push(
|
|
issue(
|
|
"invalid-value",
|
|
joinPath(joinPath(path, "udp"), "payload"),
|
|
"udp.payload 与 udp.encoding 不匹配",
|
|
targetName,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
if (encoding === "base64" && isString(udp["payload"])) {
|
|
const base64Pattern = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
if (!base64Pattern.test(udp["payload"])) {
|
|
issues.push(
|
|
issue(
|
|
"invalid-value",
|
|
joinPath(joinPath(path, "udp"), "payload"),
|
|
"udp.payload 与 udp.encoding 不匹配",
|
|
targetName,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
const responseEncoding: unknown = udp["responseEncoding"];
|
|
issues.push(...validateEncoding(responseEncoding, joinPath(joinPath(path, "udp"), "responseEncoding"), targetName));
|
|
|
|
issues.push(
|
|
...validateSize(udp["maxResponseBytes"], joinPath(joinPath(path, "udp"), "maxResponseBytes"), targetName),
|
|
);
|
|
|
|
const allowedUdpKeys = new Set(["encoding", "host", "maxResponseBytes", "payload", "port", "responseEncoding"]);
|
|
for (const key of Object.keys(udp)) {
|
|
if (!allowedUdpKeys.has(key)) {
|
|
issues.push(issue("unknown-field", joinPath(joinPath(path, "udp"), key), "是未知字段", targetName));
|
|
}
|
|
}
|
|
|
|
issues.push(...validateUdpExpect(target, path));
|
|
|
|
return issues;
|
|
}
|