feat: enhance condition branch validation by adding port edge checks and updating related functions
This commit is contained in:
@@ -285,7 +285,7 @@ func (v *definitionValidator) validateConditions() {
|
||||
} else if _, ok := v.nodesByID[targetNodeID]; !ok {
|
||||
v.addError(branchField+".targetNodeId", "condition branch target node does not exist: "+targetNodeID)
|
||||
}
|
||||
if !v.hasEdgeTo(strings.TrimSpace(node.ID), targetNodeID) {
|
||||
if !v.hasConditionBranchEdge(strings.TrimSpace(node.ID), targetNodeID, branchID) {
|
||||
v.addError(branchField+".targetNodeId", "condition branch target must have an outgoing edge: "+targetNodeID)
|
||||
}
|
||||
if branch.Default {
|
||||
@@ -441,12 +441,14 @@ func (v *definitionValidator) hasPath(sourceID string, targetID string, visiting
|
||||
return false
|
||||
}
|
||||
|
||||
func (v *definitionValidator) hasEdgeTo(sourceID string, targetID string) bool {
|
||||
if sourceID == "" || targetID == "" {
|
||||
func (v *definitionValidator) hasConditionBranchEdge(sourceID string, targetID string, sourcePortID string) bool {
|
||||
if sourceID == "" || targetID == "" || sourcePortID == "" {
|
||||
return true
|
||||
}
|
||||
for _, edge := range v.def.Edges {
|
||||
if strings.TrimSpace(edge.SourceNodeID) == sourceID && strings.TrimSpace(edge.TargetNodeID) == targetID {
|
||||
if strings.TrimSpace(edge.SourceNodeID) == sourceID &&
|
||||
strings.TrimSpace(edge.TargetNodeID) == targetID &&
|
||||
strings.TrimSpace(edge.SourcePortID) == sourcePortID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,6 +176,24 @@ func TestValidateDefinitionRejectsConditionBranchTargetWithoutEdge(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDefinitionRejectsConditionBranchTargetWithoutPortEdge(t *testing.T) {
|
||||
def := conditionDefinition()
|
||||
def.Edges = []dsl.Edge{
|
||||
edge("start_1", "condition_1"),
|
||||
edge("condition_1", "end_1"),
|
||||
portEdge("condition_1", "end_1", "default"),
|
||||
}
|
||||
|
||||
result := validator.ValidateDefinition(def, registry.DefaultRegistry())
|
||||
|
||||
if result.Valid {
|
||||
t.Fatalf("expected condition branch target without matching port edge to be invalid")
|
||||
}
|
||||
if !hasValidationMessage(result, "condition branch target must have an outgoing edge") {
|
||||
t.Fatalf("expected branch port edge error, got %#v", result.Errors)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDefinitionRejectsUnknownConditionVariable(t *testing.T) {
|
||||
def := conditionDefinition()
|
||||
var config dsl.ConditionConfig
|
||||
@@ -240,8 +258,8 @@ func conditionDefinition() dsl.Definition {
|
||||
},
|
||||
Edges: []dsl.Edge{
|
||||
edge("start_1", "condition_1"),
|
||||
edge("condition_1", "end_1"),
|
||||
edge("condition_1", "end_1"),
|
||||
portEdge("condition_1", "end_1", "hello"),
|
||||
portEdge("condition_1", "end_1", "default"),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -263,6 +281,10 @@ func edge(source string, target string) dsl.Edge {
|
||||
return dsl.Edge{SourceNodeID: source, TargetNodeID: target}
|
||||
}
|
||||
|
||||
func portEdge(source string, target string, sourcePortID string) dsl.Edge {
|
||||
return dsl.Edge{SourceNodeID: source, TargetNodeID: target, SourcePortID: sourcePortID}
|
||||
}
|
||||
|
||||
func inputs(name string, value dsl.Value) map[string]dsl.Value {
|
||||
return map[string]dsl.Value{name: value}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user