Part of duplicate code analysis: #1824
Summary
internal/config/validation_schema.go constructs rules.ValidationError structs with an identical 4-field pattern (Field, Message, JSONPath, Suggestion) at least 7 times. This creates significant boilerplate and makes it easy to accidentally omit a field.
Duplication Details
Pattern: Repeated 4-field rules.ValidationError struct literal construction
- Severity: High
- Occurrences: 7+ instances in a single file
- Locations:
internal/config/validation_schema.go (lines ~209–214, ~222–227, ~241–246, ~250–255, ~261–266, ~278–283, ~288–293)
Representative example (occurs 7+ times with minor message variation):
return &rules.ValidationError{
Field: "type",
Message: fmt.Sprintf("failed to fetch custom schema for server type '%s': %v", server.Type, err),
JSONPath: jsonPath,
Suggestion: fmt.Sprintf("Ensure the schema URL '%s' is accessible and returns a valid JSON Schema", schemaURL),
}
Another instance (compile error):
return &rules.ValidationError{
Field: "type",
Message: fmt.Sprintf("failed to compile custom schema for server type '%s': %v", server.Type, err),
JSONPath: jsonPath,
Suggestion: fmt.Sprintf("The schema at '%s' must be a valid JSON Schema Draft 7 document", schemaURL),
}
The same 4-field struct literal appears identically again at lines ~261–266, differing only in the error message text.
Impact Analysis
- Maintainability: If a new field is added to
ValidationError (e.g., an error code), all 7+ sites must be updated
- Bug Risk: Medium — easy to forget a field in one of the sites during a refactor
- Code Bloat: ~40 lines that could be reduced to ~7 one-liner calls
Refactoring Recommendations
-
Add a constructor helper in internal/config/validation_schema.go or internal/config/rules/rules.go:
func newSchemaValidationError(field, message, jsonPath, suggestion string) *ValidationError {
return &ValidationError{
Field: field,
Message: message,
JSONPath: jsonPath,
Suggestion: suggestion,
}
}
-
Optionally add scenario-specific helpers for the most common cases:
func schemaFetchError(serverType, schemaURL, jsonPath string, err error) *ValidationError {
return newSchemaValidationError("type",
fmt.Sprintf("failed to fetch custom schema for server type '%s': %v", serverType, err),
jsonPath,
fmt.Sprintf("Ensure the schema URL '%s' is accessible and returns a valid JSON Schema", schemaURL),
)
}
func schemaCompileError(serverType, schemaURL, jsonPath string, err error) *ValidationError {
return newSchemaValidationError("type",
fmt.Sprintf("failed to compile custom schema for server type '%s': %v", serverType, err),
jsonPath,
fmt.Sprintf("The schema at '%s' must be a valid JSON Schema Draft 7 document", schemaURL),
)
}
Implementation Checklist
Parent Issue
See parent analysis report: #1824
Related to #1824
Generated by Duplicate Code Detector · ◷
Part of duplicate code analysis: #1824
Summary
internal/config/validation_schema.goconstructsrules.ValidationErrorstructs with an identical 4-field pattern (Field,Message,JSONPath,Suggestion) at least 7 times. This creates significant boilerplate and makes it easy to accidentally omit a field.Duplication Details
Pattern: Repeated 4-field
rules.ValidationErrorstruct literal constructioninternal/config/validation_schema.go(lines ~209–214, ~222–227, ~241–246, ~250–255, ~261–266, ~278–283, ~288–293)Representative example (occurs 7+ times with minor message variation):
Another instance (compile error):
The same 4-field struct literal appears identically again at lines ~261–266, differing only in the error message text.
Impact Analysis
ValidationError(e.g., an error code), all 7+ sites must be updatedRefactoring Recommendations
Add a constructor helper in
internal/config/validation_schema.goorinternal/config/rules/rules.go:Optionally add scenario-specific helpers for the most common cases:
Implementation Checklist
rules.ValidationErrorconstruction sites invalidation_schema.gonewSchemaValidationErrorhelper (or place inrulespackage)schemaFetchError/schemaCompileErrorhelpers for the two most-repeated patternsmake testto verify no regressionsmake agent-finishedbefore completingParent Issue
See parent analysis report: #1824
Related to #1824