Skip to content

Add schema validation for BranchNode gRPC objects #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions core/taskengine/vm_runner_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ func (r *BranchProcessor) Validate(node *avsproto.BranchNode) error {
return fmt.Errorf("the first condition need to be an if but got :%s", node.Conditions[0].Type)
}

for i, condition := range node.Conditions {
if condition == nil {
return fmt.Errorf("condition at index %d is nil", i)
}

if condition.Id == "" {
return fmt.Errorf("condition at index %d has empty ID", i)
}

if condition.Type == "" {
return fmt.Errorf("condition at index %d has empty type", i)
}

if condition.Type != "if" && condition.Type != "else" {
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description mentions schema validation for the condition expression, but no validation is implemented for the 'expression' field. Please either add validation for 'expression' or update the PR description to accurately reflect the implemented behavior.

Copilot uses AI. Check for mistakes.

return fmt.Errorf("condition at index %d has invalid type: %s (must be 'if' or 'else')", i, condition.Type)
}

if condition.Type == "else" && i < len(node.Conditions)-1 {
if r.vm.logger != nil {
r.vm.logger.Warn("'else' condition is not the last one, subsequent conditions will be ignored")
}
}
}

return nil
}

Expand Down
96 changes: 96 additions & 0 deletions core/taskengine/vm_runner_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,99 @@ func TestBranchNodeNoElseSkip(t *testing.T) {
})
}
}

func TestBranchNodeMalformedData(t *testing.T) {
testCases := []struct {
name string
conditions []*avsproto.Condition
expectError bool
errorMsg string
}{
{
name: "nil condition",
conditions: []*avsproto.Condition{
{Id: "condition1", Type: "if", Expression: "a > 10"},
nil,
},
expectError: true,
errorMsg: "condition at index 1 is nil",
},
{
name: "empty condition ID",
conditions: []*avsproto.Condition{
{Id: "condition1", Type: "if", Expression: "a > 10"},
{Id: "", Type: "if", Expression: "a > 5"},
},
expectError: true,
errorMsg: "condition at index 1 has empty ID",
},
{
name: "empty condition type",
conditions: []*avsproto.Condition{
{Id: "condition1", Type: "if", Expression: "a > 10"},
{Id: "condition2", Type: "", Expression: "a > 5"},
},
expectError: true,
errorMsg: "condition at index 1 has empty type",
},
{
name: "invalid condition type",
conditions: []*avsproto.Condition{
{Id: "condition1", Type: "if", Expression: "a > 10"},
{Id: "condition2", Type: "invalid", Expression: "a > 5"},
},
expectError: true,
errorMsg: "condition at index 1 has invalid type",
},
{
name: "empty if expression",
conditions: []*avsproto.Condition{
{Id: "condition1", Type: "if", Expression: "a > 10"},
{Id: "condition2", Type: "if", Expression: ""},
},
expectError: false,
},
{
name: "whitespace if expression",
conditions: []*avsproto.Condition{
{Id: "condition1", Type: "if", Expression: "a > 10"},
{Id: "condition2", Type: "if", Expression: " \t\n "},
},
expectError: false,
},
{
name: "valid conditions",
conditions: []*avsproto.Condition{
{Id: "condition1", Type: "if", Expression: "a > 10"},
{Id: "condition2", Type: "if", Expression: "a > 5"},
{Id: "condition3", Type: "else"},
},
expectError: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
vm := NewVM()
processor := NewBranchProcessor(vm)

err := processor.Validate(&avsproto.BranchNode{
Conditions: tc.conditions,
})

if tc.expectError {
if err == nil {
t.Errorf("expected error but got none")
return
}
if !strings.Contains(err.Error(), tc.errorMsg) {
t.Errorf("expected error message to contain '%s', but got: '%s'", tc.errorMsg, err.Error())
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
})
}
}