Class: Lutaml::Xsd::Validation::ValidationError

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xsd/validation/validation_error.rb

Overview

ValidationError represents a single validation error or warning

This class encapsulates all information about a validation issue, including its code, severity, location, message, and optional suggestions for fixing the issue.

Examples:

Create a validation error

error = ValidationError.new(
  code: "type_mismatch",
  message: "Expected integer, got string",
  severity: :error,
  location: "/root/element[1]",
  context: { expected: "integer", actual: "string" }
)

Access error details

puts error.code          # => "type_mismatch"
puts error.message       # => "Expected integer, got string"
puts error.severity      # => :error
puts error.location      # => "/root/element[1]"

Constant Summary collapse

SEVERITIES =

Valid severity levels

%i[error warning info].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, message:, severity: :error, location: nil, line_number: nil, context: nil, suggestion: nil) ⇒ ValidationError

Initialize a new ValidationError

Parameters:

  • code (String)

    Error code identifying the type of error

  • message (String)

    Human-readable error message

  • severity (Symbol) (defaults to: :error)

    Error severity (:error, :warning, :info)

  • location (String, nil) (defaults to: nil)

    XPath or location in the document

  • line_number (Integer, nil) (defaults to: nil)

    Line number if available

  • context (Hash, nil) (defaults to: nil)

    Additional context about the error

  • suggestion (String, nil) (defaults to: nil)

    Suggested fix for the error

Raises:

  • (ArgumentError)

    if severity is invalid



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 44

def initialize(code:, message:, severity: :error, location: nil,
               line_number: nil, context: nil, suggestion: nil)
  validate_severity!(severity)

  @code = code
  @message = message
  @severity = severity
  @location = location
  @line_number = line_number
  @context = context || {}
  @suggestion = suggestion
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



27
28
29
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27

def code
  @code
end

#contextObject (readonly)

Returns the value of attribute context.



27
28
29
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27

def context
  @context
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



27
28
29
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27

def line_number
  @line_number
end

#locationObject (readonly)

Returns the value of attribute location.



27
28
29
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27

def location
  @location
end

#messageObject (readonly)

Returns the value of attribute message.



27
28
29
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27

def message
  @message
end

#severityObject (readonly)

Returns the value of attribute severity.



27
28
29
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27

def severity
  @severity
end

#suggestionObject (readonly)

Returns the value of attribute suggestion.



27
28
29
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27

def suggestion
  @suggestion
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare errors for equality

Parameters:

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 163

def ==(other)
  return false unless other.is_a?(ValidationError)

  @code == other.code &&
    @message == other.message &&
    @severity == other.severity &&
    @location == other.location
end

#detailed_messageString

Get detailed error message including location and suggestion

Returns:

  • (String)


91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 91

def detailed_message
  parts = []
  parts << "[#{@severity.to_s.upcase}]"
  parts << @code
  parts << "-"
  parts << @message

  result = parts.join(" ")
  result += "\n  Location: #{formatted_location}" if has_location?
  result += "\n  Context: #{format_context}" if @context.any?
  result += "\n  Suggestion: #{@suggestion}" if @suggestion
  result
end

#error?Boolean

Check if this is an error-level issue

Returns:

  • (Boolean)


60
61
62
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 60

def error?
  @severity == :error
end

#formatted_locationString

Get formatted location string

Returns:

  • (String)


81
82
83
84
85
86
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 81

def formatted_location
  parts = []
  parts << "Line #{@line_number}" if @line_number
  parts << @location if @location
  parts.empty? ? "(unknown location)" : parts.join(", ")
end

#has_location?Boolean

Check if error has location information

Returns:

  • (Boolean)


108
109
110
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 108

def has_location?
  !@location.nil? || !@line_number.nil?
end

#has_suggestion?Boolean

Check if error has a suggestion

Returns:

  • (Boolean)


115
116
117
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 115

def has_suggestion?
  !@suggestion.nil? && !@suggestion.empty?
end

#hashInteger

Generate hash code for use in hashes and sets

Returns:

  • (Integer)


177
178
179
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 177

def hash
  [@code, @message, @severity, @location].hash
end

#info?Boolean

Check if this is an info-level issue

Returns:

  • (Boolean)


74
75
76
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 74

def info?
  @severity == :info
end

#inspectString

Detailed string representation with all information

Returns:

  • (String)


152
153
154
155
156
157
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 152

def inspect
  "#<#{self.class.name} code=#{@code.inspect} " \
    "severity=#{@severity.inspect} " \
    "message=#{@message.inspect} " \
    "location=#{@location.inspect}>"
end

#to_hHash

Convert error to hash representation

Returns:

  • (Hash)


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 122

def to_h
  {
    code: @code,
    message: @message,
    severity: @severity,
    location: @location,
    line_number: @line_number,
    context: @context,
    suggestion: @suggestion,
  }.compact
end

#to_jsonString

Convert error to JSON

Returns:

  • (String)


137
138
139
140
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 137

def to_json(*)
  require "json"
  to_h.to_json(*)
end

#to_sString

String representation

Returns:

  • (String)


145
146
147
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 145

def to_s
  "#{@severity.to_s.upcase}: #{@message} (#{@code})"
end

#warning?Boolean

Check if this is a warning-level issue

Returns:

  • (Boolean)


67
68
69
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 67

def warning?
  @severity == :warning
end