show error when node data is invalid

This commit is contained in:
codetypes committed 2024-04-18 00:37:40 +08:00
1 parent 0e6aff2855
commit 7ceaf45971
9 files changed
+68 -20

No files matched your search

+4 -2
View File
@@ -1,5 +1,6 @@
import { useWorkspace } from "@/contexts/workspace-context";
import { toBreakWord } from "@/misc/b3util";
import { TreeGraphData } from "@/misc/b3type";
import { checkTreeData, toBreakWord } from "@/misc/b3util";
import i18n from "@/misc/i18n";
import G6 from "@antv/g6";
@@ -76,7 +77,8 @@ G6.registerNode(
let color = nodeDef.color || NODE_COLORS[classify] || NODE_COLORS["Other"];
if (
!workspace.hasNodeDef(cfg.name as string) ||
(cfg.path && (!cfg.children || (cfg.children as []).length == 0))
(cfg.path && (!cfg.children || (cfg.children as []).length == 0)) ||
!checkTreeData(cfg as TreeGraphData)
) {
classify = "Error";
color = NODE_COLORS[classify];
+48
View File
@@ -147,6 +147,54 @@ export const calcTreeDataSize = (data: TreeGraphData) => {
return [200, height];
};
export const checkTreeData = (data: TreeGraphData) => {
const conf = data.def;
if (conf.input) {
for (let i = 0; i < conf.input.length; i++) {
if (conf.input[i].indexOf("?") === -1 && !data.input?.[i]) {
return false;
}
}
}
if (conf.output) {
for (let i = 0; i < conf.output.length; i++) {
if (conf.output[i].indexOf("?") === -1 && !data.output?.[i]) {
return false;
}
}
}
if (conf.args) {
for (let i = 0; i < conf.args.length; i++) {
const arg = conf.args[i];
if (arg.type.indexOf("?") === -1) {
const value = data.args?.[arg.name];
if (arg.type === "float") {
if (typeof value !== "number") {
return false;
}
} else if (arg.type === "int") {
if (typeof value !== "number" || value !== Math.floor(value)) {
return false;
}
} else if (arg.type === "string") {
if (!value || typeof value !== "string") {
return false;
}
} else if (arg.type === "enum") {
if (!arg.options?.find((option) => option.value === value)) {
return false;
}
} else if (arg.type == "code") {
if (!value || typeof value !== "string") {
return false;
}
}
}
}
}
return true;
};
export const createTreeData = (node: NodeModel, parent?: string) => {
const workspace = useWorkspace.getState();
let treeData: TreeGraphData = {