Class: SpecForge::Documentation::OpenAPI::V30::ErrorFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_forge/documentation/openapi/v3_0/error_formatter.rb

Overview

Formats OpenAPI validation errors into human-readable messages

Takes validation errors from OpenAPI parsers and transforms them into structured, easy-to-understand error messages with context information and suggestions for resolution.

Examples:

Formatting validation errors

errors = openapi_parser.errors
formatted = ErrorFormatter.format(errors)
puts formatted

Constant Summary collapse

PATHS_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regular expression for matching path-related validation errors

Captures path, HTTP method, and response code from OpenAPI error contexts to provide meaningful location information in error messages.

%r{#/paths/(.+?)/(get|post|put|patch|delete|head|options)/responses/(.+)}i
SCHEMA_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regular expression for matching schema-related validation errors

Captures schema name and field path from OpenAPI error contexts to identify specific schema validation failures.

%r{#/components/schemas/(.+?)/(.+)}i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors) ⇒ ErrorFormatter

Creates a new error formatter

Parameters:

  • errors (Array)

    Array of validation error objects to format



58
59
60
# File 'lib/spec_forge/documentation/openapi/v3_0/error_formatter.rb', line 58

def initialize(errors)
  @errors = errors
end

Class Method Details

.format(errors) ⇒ String?

Formats an array of validation errors into a readable string

Parameters:

  • errors (Array)

    Array of validation error objects

Returns:

  • (String, nil)

    Formatted error message or nil if no errors



47
48
49
# File 'lib/spec_forge/documentation/openapi/v3_0/error_formatter.rb', line 47

def self.format(errors)
  new(errors).format
end

Instance Method Details

#formatString?

Formats the errors into a structured, readable message

Groups errors by type (unexpected vs validation), formats each error with context and location information, and returns a comprehensive error report with resolution guidance.

Returns:

  • (String, nil)

    Formatted error message or nil if no errors



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/spec_forge/documentation/openapi/v3_0/error_formatter.rb', line 71

def format
  return if @errors.blank?

  unexpected_errors, errors = @errors.partition { |e| e.message.include?("Unexpected") }

  unexpected_errors = format_errors(unexpected_errors)
  errors = format_errors(errors, start_index: unexpected_errors.size)

  if unexpected_errors.size > 0
    unexpected_message = <<~STRING

      Field errors (resolve these first):

      #{unexpected_errors.join("\n\n")}

      -------

      Other validation errors:
    STRING
  end

  <<~STRING
    ========================================
    🚨 Validation Errors
    ========================================
    #{unexpected_message}
    #{errors.join("\n\n")}

    Total errors: #{errors.size}
  STRING
end