Refactoring Validation logic into separate function#6272
Refactoring Validation logic into separate function#6272abdullah-workday merged 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the flow validation logic by extracting the core validation rules into a pure function, validateFlowData, and introduces a comprehensive unit test suite. The refactoring improves testability and separates data retrieval from validation logic. Feedback identifies a performance bottleneck in the hanging edges check that could be optimized using a Set for node lookups. Additionally, a logic issue was found where the regex for normalizing array index conditions is hardcoded to a specific parameter name, which may cause validation to fail for other array-type parameters.
| for (const edge of edges) { | ||
| const sourceExists = nodes.some((node: IReactFlowNode) => node.id === edge.source) | ||
| const targetExists = nodes.some((node: IReactFlowNode) => node.id === edge.target) | ||
|
|
There was a problem hiding this comment.
The hanging edges check has nodes.some (an Set for node ID lookups reduces this to
// Check for hanging edges
const nodeIds = new Set(nodes.map((node) => node.id))
for (const edge of edges) {
const sourceExists = nodeIds.has(edge.source)
const targetExists = nodeIds.has(edge.target)|
|
||
| if (isIndexCondition) { | ||
| // Replace $index with actual index and evaluate | ||
| const normalizedKey = conditionKey.replace(/conditions\[\$index\]\.(\w+)/, '$1') |
There was a problem hiding this comment.
The regex for $index conditions is hardcoded to the string conditions. If an array parameter has a different name (e.g., actions), this validation logic will fail to correctly normalize the key. This issue also exists on line 135. Consider using the actual parameter name param.name to build the regex dynamically.
Overview
packages/server/src/services/validation/index.tsto extract the validation logic into a separate function.What is Changed
packages/server/src/services/validation/index.ts-> Code Refactoringpackages/server/src/services/validation/validateFlowData.test.ts-> Adding unit test