Skip to content

[duplicate-code] Duplicate Code Pattern: Repeated ValidationError Struct Construction in validation_schema.go #1826

@github-actions

Description

@github-actions

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

  1. 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,
        }
    }
  2. 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

  • Review all rules.ValidationError construction sites in validation_schema.go
  • Add newSchemaValidationError helper (or place in rules package)
  • Optionally add schemaFetchError / schemaCompileError helpers for the two most-repeated patterns
  • Replace all 7+ struct literals with helper calls
  • Run make test to verify no regressions
  • Run make agent-finished before completing

Parent Issue

See parent analysis report: #1824
Related to #1824

Generated by Duplicate Code Detector ·

  • expires on Mar 20, 2026, 2:58 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions