From 52a8e6eeb591cb8a703978e5868928aaf83fedc3 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Mon, 18 Sep 2023 22:27:08 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(reactflowUtils.ts):=20fix=20?= =?UTF-8?q?validation=20logic=20for=20nested=20dictionaries=20in=20validat?= =?UTF-8?q?eNode=20function=20=E2=9C=A8=20feat(reactflowUtils.ts):=20add?= =?UTF-8?q?=20hasEmptyKey=20function=20to=20check=20for=20empty=20keys=20i?= =?UTF-8?q?n=20dictionaries=20in=20validateNode=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/utils/reactflowUtils.ts | 64 +++++++++++++++--------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/src/frontend/src/utils/reactflowUtils.ts b/src/frontend/src/utils/reactflowUtils.ts index 0e0e07155..5e4ac5f49 100644 --- a/src/frontend/src/utils/reactflowUtils.ts +++ b/src/frontend/src/utils/reactflowUtils.ts @@ -204,29 +204,36 @@ export function validateNode( node: { template }, } = node.data; - return Object.keys(template).reduce( - (errors: Array, t) => - errors.concat( - template[t].required && - template[t].show && - (template[t].value === undefined || - template[t].value === null || - template[t].value === "") && - !reactFlowInstance - .getEdges() - .some( - (edge) => - edge.targetHandle?.split("|")[1] === t && - edge.targetHandle.split("|")[2] === node.id - ) - ? [ - `${type} is missing ${template.display_name || toNormalCase(template[t].name) - }.`, - ] - : [] - ), - [] as string[] - ); + return Object.keys(template).reduce((errors: Array, t) => { + if ( + template[t].required && + template[t].show && + (template[t].value === undefined || + template[t].value === null || + template[t].value === "") && + !reactFlowInstance + .getEdges() + .some( + (edge) => + edge.targetHandle?.split("|")[1] === t && + edge.targetHandle.split("|")[2] === node.id + ) + ) { + errors.push(`${type} is missing ${template.display_name || toNormalCase(template[t].name)}.`); + } else if ( + (template[t].type === "dict" || template[t].type === "NestedDict") && + template[t].required && + template[t].show && + (template[t].value !== undefined || + template[t].value !== null || + template[t].value !== "") + ) { + if (hasDuplicateKeys(template[t].value)) errors.push(`${type} (${template.display_name || template[t].name}) contains duplicate keys with the same values.`); + if (hasEmptyKey(template[t].value)) errors.push(`${type} (${template.display_name || template[t].name}) contains keys with empty values.`); + } + return errors; + }, [] as string[]); + } export function validateNodes(reactFlowInstance: ReactFlowInstance) { @@ -319,6 +326,17 @@ export function hasDuplicateKeys(array) { return false; } +export function hasEmptyKey(objArray) { + for (const obj of objArray) { + for (const key in obj) { + if (obj.hasOwnProperty(key) && key === '') { + return true; // Found an empty key + } + } + } + return false; // No empty keys found +} + export function convertValuesToNumbers(arr) { return arr.map((obj) => { const newObj = {};