fix(workflow): require typed evidence before blocking formula-like mappings
This commit is contained in:
1 parent
fac81b08e8
commit
55c3521c48
5 files changed
+37
-9
No files matched your search
@@ -232,7 +232,7 @@ function findUnguardedDatalinkEdits(
|
||||
}
|
||||
|
||||
const FORMULA_REGEX =
|
||||
/\b(AddMonths|dateDIF|DATEADD|DATEDIF|IF|SUM|ROUND|INT|TEXT|YEAR|MONTH|DAY)\s*\(/i;
|
||||
/^(?:=\s*)?(AddMonths|dateDIF|DATEADD|DATEDIF|IF|SUM|ROUND|INT|TEXT|YEAR|MONTH|DAY)\s*\([\s\S]*\)$/i;
|
||||
|
||||
function checkMisplacedFormulasInDatalink(
|
||||
task: WorkflowTask,
|
||||
@@ -241,13 +241,13 @@ function checkMisplacedFormulasInDatalink(
|
||||
): void {
|
||||
if (!task.type.includes("datalink")) return;
|
||||
const raw = task as Record<string, unknown>;
|
||||
const inspectTargets: Array<{ key: string; value: unknown }> = [];
|
||||
const inspectTargets: Array<{ key: string; value: unknown; type?: unknown }> = [];
|
||||
|
||||
// 1. inspect task.inputs
|
||||
if (Array.isArray(task.inputs)) {
|
||||
for (const inp of task.inputs) {
|
||||
if (inp && typeof inp === "object") {
|
||||
inspectTargets.push({ key: inp.key || (inp as any).name || "input", value: inp.value });
|
||||
inspectTargets.push({ key: inp.key || (inp as any).name || "input", value: inp.value, type: (inp as any).type });
|
||||
}
|
||||
}
|
||||
} else if (raw.inputs && typeof raw.inputs === "object") {
|
||||
@@ -260,7 +260,7 @@ function checkMisplacedFormulasInDatalink(
|
||||
const formEntries = getTaskFormEntries(task);
|
||||
for (const entry of formEntries) {
|
||||
if (entry.key) {
|
||||
inspectTargets.push({ key: entry.key, value: entry.value });
|
||||
inspectTargets.push({ key: entry.key, value: entry.value, type: entry.type });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,6 +272,7 @@ function checkMisplacedFormulasInDatalink(
|
||||
inspectTargets.push({
|
||||
key: String(item.key || item.target || "field"),
|
||||
value: item.value ?? item.source,
|
||||
type: item.type,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -286,6 +287,7 @@ function checkMisplacedFormulasInDatalink(
|
||||
inspectTargets.push({
|
||||
key: String(item.key || item.label || "field"),
|
||||
value: item.value,
|
||||
type: item.type,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -293,13 +295,18 @@ function checkMisplacedFormulasInDatalink(
|
||||
|
||||
for (const target of inspectTargets) {
|
||||
if (typeof target.value === "string") {
|
||||
const match = target.value.match(FORMULA_REGEX);
|
||||
const match = target.value.trim().match(FORMULA_REGEX);
|
||||
const fieldType = typeof target.type === "string" ? target.type.toLowerCase() : "";
|
||||
if (["text", "string", "textarea", "multiline", "richtext"].includes(fieldType)) continue;
|
||||
const strongType = ["date", "datetime", "timestamp", "number", "numeric", "integer", "decimal", "float", "money"].includes(fieldType);
|
||||
if (match) {
|
||||
addDiagnostic(diagnostics, {
|
||||
severity: "error",
|
||||
severity: strongType ? "error" : "warning",
|
||||
code: "MISPLACED_FORMULA_IN_DATALINK_MAPPING",
|
||||
path,
|
||||
message: `DataLink 任务 '${task.label || task.name}' 的字段 '${target.key}' 配置了表单公式 '${match[0]}...'。合思 DataLink 属于底层数据写入节点,不支持前端表单公式求值,传入公式字符串会导致后端强类型解析崩溃 (如 ParseException)。请前置添加【代码节点】计算该值并在此处引用。`,
|
||||
message: strongType
|
||||
? `DataLink 任务 '${task.label || task.name}' 的 ${fieldType} 字段 '${target.key}' 配置了表单公式 '${match[0]}'。底层数据写入不求值表单公式,强类型字段不能接受该表达式;请前置添加【代码节点】计算该值并在此处引用。`
|
||||
: `DataLink 任务 '${task.label || task.name}' 的字段 '${target.key}' 包含疑似公式 '${match[0]}',但缺少可验证的字段类型,仅作为建议。若需要计算,请前置添加【代码节点】;若为文本,请保留字面量。`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { analyzeWorkflow } from "./analyzer.js";
|
||||
function analyze(value: string, type?: string) {
|
||||
return analyzeWorkflow({ id: "wf", label: "wf", state: 1, version: "1", tasks: [{ name: "write", type: "ekuaibao-datalink-create", fields: [{ key: "field", value, type }] }] } as any);
|
||||
}
|
||||
test("ordinary text and explicit text formulas never cause hard blocks", () => {
|
||||
for (const value of ["Annual ROUND(1) review", "IF(required) then contact us", '"ROUND(1)"']) expect(analyze(value).diagnostics).toHaveLength(0);
|
||||
expect(analyze("ROUND(1)", "text").diagnostics).toHaveLength(0);
|
||||
});
|
||||
test("only a complete formula in a known strong type causes a hard block", () => {
|
||||
const unknown = analyze("AddMonths(date, 1)");
|
||||
expect(unknown.summary.passed).toBe(true);
|
||||
expect(unknown.diagnostics[0].category).toBe("soft_advice");
|
||||
const date = analyze("AddMonths(date, 1)", "date");
|
||||
expect(date.summary.passed).toBe(false);
|
||||
expect(date.diagnostics[0].category).toBe("hard_block");
|
||||
expect(analyze("ROUND(1)", "NUMBER").summary.hardBlocks).toBe(1);
|
||||
});
|
||||
@@ -206,6 +206,7 @@ describe("Workflow static analysis", () => {
|
||||
mapping: [
|
||||
{
|
||||
key: "amortizationMonth",
|
||||
type: "date",
|
||||
value: "AddMonths(date, 1)",
|
||||
},
|
||||
],
|
||||
|
||||
Reference in new issue
Block a user