Skip to content

Commit 6f889eb

Browse files
committed
chore: validate for-each params to data in
1 parent cb4ce97 commit 6f889eb

File tree

1 file changed

+36
-0
lines changed
  • packages/frontend/src/components/FlowStepTestController

1 file changed

+36
-0
lines changed

packages/frontend/src/components/FlowStepTestController/utils.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import { Variable } from '@/helpers/variables'
77

88
import { simpleSubstitute, VariableInfoMap } from '../RichTextEditor/utils'
99

10+
interface TableData {
11+
columns?: { name: string }[]
12+
rows?: unknown[]
13+
}
14+
15+
const STEP_ID_REGEX =
16+
/step\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i
17+
18+
const getTableData = (data: unknown): TableData => data as TableData
19+
1020
const deepCompare = (a: any, b: any, varInfoMap: VariableInfoMap): boolean => {
1121
if (a === b) {
1222
return true
@@ -98,6 +108,32 @@ export const matchParamsToDataIn = (
98108
)
99109
}
100110

111+
// NOTE: special handling for for-each step
112+
if (key === 'items') {
113+
const match = String(paramValue).match(STEP_ID_REGEX)
114+
const searchKey = match?.[0]
115+
if (!searchKey) {
116+
return false
117+
}
118+
119+
const tableData = getTableData(lastTest)
120+
const varRowsFound = varInfoMap.get(
121+
`{{${searchKey}.rowsFound}}`,
122+
)?.testRunValue
123+
124+
if (Number(varRowsFound) !== Number(tableData.rows?.length)) {
125+
return false
126+
}
127+
128+
const lastTestColumns = tableData.columns?.map((c) => c.name) ?? []
129+
const varInfo = Array.from(varInfoMap.entries())
130+
.filter(([key]) => key.includes(`${searchKey}.data`))
131+
.map(([, value]) => value)
132+
const varColumns = new Set(varInfo.map((item) => item.label))
133+
134+
return lastTestColumns.every((label) => varColumns.has(label))
135+
}
136+
101137
// Handle arrays and objects using deep comparison
102138
if (Array.isArray(paramValue) || typeof paramValue === 'object') {
103139
return deepCompare(paramValue, lastTest, varInfoMap)

0 commit comments

Comments
 (0)