Skip to content

Commit 5e9d7f7

Browse files
refactor(config): extract shared non-empty string slice validator
Consolidates validateAgentIDs and validateTrustedBots onto a single validateNonEmptyStringSlice helper in validation_rules.go, removing duplicated 'non-empty array of non-empty strings' logic identified in issue #12143. ValidateStringArrayField in guard_policy_validation.go was left unchanged since it operates on []interface{} (not []string) and its distinct error-message wording is pinned by existing tests. Closes #12143 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 34ed844 commit 5e9d7f7

2 files changed

Lines changed: 22 additions & 24 deletions

File tree

internal/config/validation_gateway.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,7 @@ func validateGatewayConfig(gateway *StdinGatewayConfig) error {
104104
}
105105

106106
func validateAgentIDs(agentIDs []string, defined bool, fieldName string) error {
107-
if !defined {
108-
return nil
109-
}
110-
if len(agentIDs) == 0 {
111-
return fmt.Errorf("%s must be a non-empty array when present", fieldName)
112-
}
113-
for i, agentID := range agentIDs {
114-
if strings.TrimSpace(agentID) == "" {
115-
return fmt.Errorf("%s[%d] must be a non-empty string", fieldName, i)
116-
}
117-
}
118-
return nil
107+
return validateNonEmptyStringSlice(agentIDs, defined, fieldName, "")
119108
}
120109

121110
func validateGatewayPayloadSizeThreshold(value int, fieldName, jsonPath string) error {
@@ -136,18 +125,7 @@ func validateContainerRuntimeCommandNotBlank(command, fieldName, jsonPath string
136125
// validateTrustedBots checks that the trusted_bots/trustedBots list conforms to spec §4.1.3.4:
137126
// when present, it must be a non-empty array of non-empty strings.
138127
func validateTrustedBots(bots []string) error {
139-
if bots == nil {
140-
return nil
141-
}
142-
if len(bots) == 0 {
143-
return fmt.Errorf("trusted_bots must be a non-empty array when present (spec §4.1.3.4)")
144-
}
145-
for i, bot := range bots {
146-
if strings.TrimSpace(bot) == "" {
147-
return fmt.Errorf("trusted_bots[%d] must be a non-empty string", i)
148-
}
149-
}
150-
return nil
128+
return validateNonEmptyStringSlice(bots, bots != nil, "trusted_bots", " (spec §4.1.3.4)")
151129
}
152130

153131
// validateTOMLStdioContainerization validates that TOML stdio servers use the selected container runtime command.

internal/config/validation_rules.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ func PortRange(port int, jsonPath string) *ValidationError {
3232
return nil
3333
}
3434

35+
// validateNonEmptyStringSlice validates that, when defined is true, values is a non-empty
36+
// slice containing only non-empty (non-whitespace) strings. When defined is false, the field
37+
// is treated as absent and validation passes trivially. fieldName is used for both the
38+
// top-level error and per-element errors; specSuffix (e.g. " (spec §4.1.3.4)") is appended
39+
// verbatim after "when present" in the top-level error, or may be empty.
40+
func validateNonEmptyStringSlice(values []string, defined bool, fieldName, specSuffix string) error {
41+
if !defined {
42+
return nil
43+
}
44+
if len(values) == 0 {
45+
return fmt.Errorf("%s must be a non-empty array when present%s", fieldName, specSuffix)
46+
}
47+
for i, v := range values {
48+
if strings.TrimSpace(v) == "" {
49+
return fmt.Errorf("%s[%d] must be a non-empty string", fieldName, i)
50+
}
51+
}
52+
return nil
53+
}
54+
3555
func validatePositiveIntegerRule(value int, fieldName, jsonPath, logLabel, failureLabel, suggestion string) *ValidationError {
3656
logValidation.Printf("Validating %s: field=%s, value=%d, jsonPath=%s", logLabel, fieldName, value, jsonPath)
3757
if value < 1 {

0 commit comments

Comments
 (0)