Skip to content

refactor: extract newValidationError helper to deduplicate log+return pattern in validation functions#8503

Merged
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-fix-validation-functions
Jul 2, 2026
Merged

refactor: extract newValidationError helper to deduplicate log+return pattern in validation functions#8503
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-fix-validation-functions

Conversation

Copilot AI commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

14 validation functions across validation_errors.go and validation_rules.go independently called logValidation.Printf then returned a &ValidationError{...} literal — meaning any log format change required editing every call site.

Changes

internal/config/validation_errors.go — new private helper:

func newValidationError(logMsg, field, message, jsonPath, suggestion string) *ValidationError {
    logValidation.Printf("%s", logMsg)
    return &ValidationError{Field: field, Message: message, JSONPath: jsonPath, Suggestion: suggestion}
}
  • Pattern B (UnsupportedType, UndefinedVariable, MissingRequired, InvalidPattern, SchemaValidationError): each had an identical log-then-return body; all five now delegate to the helper.

internal/config/validation_rules.go

  • Pattern A (PortRange, PositiveInteger, TimeoutMinimum, TimeoutRange, AbsolutePath): per-function entry logs are retained as-is (they vary by parameters), but each failure-log + &ValidationError{} block is replaced with a single newValidationError(...) call.

No validation error messages, field values, or public API changed.

GitHub Advanced Security started work on behalf of lpcox July 2, 2026 22:40 View session
GitHub Advanced Security finished work on behalf of lpcox July 2, 2026 22:41
…ication

Add a private newValidationError(logMsg, field, message, jsonPath, suggestion)
helper in validation_errors.go that centralises the repeated
logValidation.Printf + return &ValidationError{...} pattern.

Pattern B (validation_errors.go): UnsupportedType, UndefinedVariable,
MissingRequired, InvalidPattern, SchemaValidationError each had an identical
log+return body; all five now delegate to the helper.

Pattern A (validation_rules.go): PortRange, PositiveInteger, TimeoutMinimum,
TimeoutRange, AbsolutePath kept their per-function entry-log line but their
failure-log + return block now uses the same helper, eliminating the repeated
pair of logValidation.Printf + &ValidationError{} on every failure path.

No validation error messages, field values, or public API changed.
Closes #8477
GitHub Advanced Security started work on behalf of lpcox July 2, 2026 22:47 View session
Copilot AI changed the title [WIP] Refactor duplicated validation functions to reduce code repetition refactor: extract newValidationError helper to deduplicate log+return pattern in validation functions Jul 2, 2026
Copilot finished work on behalf of lpcox July 2, 2026 22:47
Copilot AI requested a review from lpcox July 2, 2026 22:47
@lpcox lpcox marked this pull request as ready for review July 2, 2026 22:48
Copilot AI review requested due to automatic review settings July 2, 2026 22:48
GitHub Advanced Security finished work on behalf of lpcox July 2, 2026 22:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors config validation code by extracting a shared newValidationError helper to centralize the repeated “log + construct *ValidationError” pattern, reducing duplication across validation helpers in internal/config.

Changes:

  • Added a private newValidationError(logMsg, field, message, jsonPath, suggestion) helper to log and construct *ValidationError.
  • Updated several validation error constructors in validation_errors.go to delegate to the helper.
  • Updated several rule validators in validation_rules.go to replace inline “log + &ValidationError{...}” blocks with newValidationError(...) calls while preserving entry logs.
Show a summary per file
File Description
internal/config/validation_errors.go Introduces newValidationError and migrates multiple error constructors to use it.
internal/config/validation_rules.go Replaces repeated failure “log + return ValidationError” blocks with newValidationError(...) calls in several validators.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Low

Comment thread internal/config/validation_errors.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox July 2, 2026 22:52 View session
GitHub Advanced Security finished work on behalf of lpcox July 2, 2026 22:53
@lpcox lpcox merged commit a84f6b5 into main Jul 2, 2026
24 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-fix-validation-functions branch July 2, 2026 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Pattern: Validation Function Log-Check-Return Structure

3 participants