Skip to content

Refactor formatErrorContext to centralize schema guidance and remove duplicated addDetail logic#6797

Merged
lpcox merged 3 commits into
mainfrom
copilot/duplicate-codefix-adddetail-calls
May 31, 2026
Merged

Refactor formatErrorContext to centralize schema guidance and remove duplicated addDetail logic#6797
lpcox merged 3 commits into
mainfrom
copilot/duplicate-codefix-adddetail-calls

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 31, 2026

formatErrorContext duplicated the same addDetail() guidance across both keyword-based and message-fallback paths, creating drift risk when updating validation help text. This change centralizes keyword-to-guidance mapping so both paths use one source of truth while preserving existing fallback behavior.

  • Centralize detail text in a single helper

    • Added detailForKeyword(keyword string) (string, []string) to own all guidance strings for:
      • additionalProperties, type, enum, required, pattern, range, oneOf
    • Unknown keywords return empty key / nil lines.
  • Unify primary and fallback classification paths

    • Added local addFromKeyword in formatErrorContext to route through detailForKeyword.
    • Replaced duplicated inline addDetail(...) blocks in:
      • keyword-location switch (primary path)
      • message-based strings.Contains(...) checks (fallback path)
    • Range keywords (minimum, maximum, exclusiveMinimum, exclusiveMaximum) now map through the shared "range" entry.
  • Expand focused coverage around the new abstraction

    • Added a TestFormatErrorContext case for range inference from keyword location (.../minimum).
    • Added TestDetailForKeyword to verify:
      • known keyword returns expected key/details
      • unknown keyword returns empty/nil.
func detailForKeyword(keyword string) (string, []string) {
	switch keyword {
	case "additionalProperties":
		return "additionalProperties", []string{
			"Details: Configuration contains field(s) that are not defined in the schema",
			"  → Check for typos in field names or remove unsupported fields",
		}
	// ...
	default:
		return "", nil
	}
}

// Used by both primary and fallback paths:
if key, lines := detailForKeyword(keyword); key != "" {
	addDetail(key, lines...)
}

Copilot AI changed the title [WIP] Refactor duplicate addDetail calls in formatErrorContext Refactor formatErrorContext to centralize schema guidance and remove duplicated addDetail logic May 31, 2026
Copilot finished work on behalf of lpcox May 31, 2026 13:28
Copilot AI requested a review from lpcox May 31, 2026 13:28
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 31, 2026

@copilot resolve the merge conflicts in this pull request

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 31, 2026

@copilot resolve the merge conflicts in this pull request

Resolved the merge conflicts and updated the branch in commit b477fe2b.

Copilot finished work on behalf of lpcox May 31, 2026 13:52
@lpcox lpcox marked this pull request as ready for review May 31, 2026 13:54
Copilot AI review requested due to automatic review settings May 31, 2026 13:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 JSON Schema validation error formatting to centralize keyword-to-guidance strings so both the keyword-location path and the message-fallback path use the same source of truth, reducing drift risk in user-facing diagnostics.

Changes:

  • Introduces detailForKeyword(keyword string) (string, []string) to own all guidance text for supported JSON Schema keywords (including shared "range" guidance).
  • Updates formatErrorContext to route both its primary (keyword-location) and fallback (message-based) classification paths through the shared helper.
  • Adds focused tests for range inference via keyword location and for detailForKeyword behavior on known/unknown keywords.
Show a summary per file
File Description
internal/config/validation_schema.go Centralizes validation guidance text and reuses it across both keyword-location and message-fallback error classification paths.
internal/config/validation_schema_error_format_test.go Adds coverage for the new helper and for range inference via keyword-location (e.g., /minimum).

Copilot's findings

Tip

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

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

@lpcox lpcox merged commit 1a07f85 into main May 31, 2026
17 checks passed
@lpcox lpcox deleted the copilot/duplicate-codefix-adddetail-calls branch May 31, 2026 14:19
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: Duplicated addDetail() calls in formatErrorContext

3 participants