Class: Lutaml::Xsd::Validation::ValidationError
- Inherits:
-
Object
- Object
- Lutaml::Xsd::Validation::ValidationError
- 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.
Constant Summary collapse
- SEVERITIES =
Valid severity levels
%i[error warning info].freeze
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#severity ⇒ Object
readonly
Returns the value of attribute severity.
-
#suggestion ⇒ Object
readonly
Returns the value of attribute suggestion.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compare errors for equality.
-
#detailed_message ⇒ String
Get detailed error message including location and suggestion.
-
#error? ⇒ Boolean
Check if this is an error-level issue.
-
#formatted_location ⇒ String
Get formatted location string.
-
#has_location? ⇒ Boolean
Check if error has location information.
-
#has_suggestion? ⇒ Boolean
Check if error has a suggestion.
-
#hash ⇒ Integer
Generate hash code for use in hashes and sets.
-
#info? ⇒ Boolean
Check if this is an info-level issue.
-
#initialize(code:, message:, severity: :error, location: nil, line_number: nil, context: nil, suggestion: nil) ⇒ ValidationError
constructor
Initialize a new ValidationError.
-
#inspect ⇒ String
Detailed string representation with all information.
-
#to_h ⇒ Hash
Convert error to hash representation.
-
#to_json ⇒ String
Convert error to JSON.
-
#to_s ⇒ String
String representation.
-
#warning? ⇒ Boolean
Check if this is a warning-level issue.
Constructor Details
#initialize(code:, message:, severity: :error, location: nil, line_number: nil, context: nil, suggestion: nil) ⇒ ValidationError
Initialize a new ValidationError
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 = @severity = severity @location = location @line_number = line_number @context = context || {} @suggestion = suggestion end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
27 28 29 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27 def code @code end |
#context ⇒ Object (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_number ⇒ Object (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 |
#location ⇒ Object (readonly)
Returns the value of attribute location.
27 28 29 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27 def location @location end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
27 28 29 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27 def @message end |
#severity ⇒ Object (readonly)
Returns the value of attribute severity.
27 28 29 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 27 def severity @severity end |
#suggestion ⇒ Object (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
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. && @severity == other.severity && @location == other.location end |
#detailed_message ⇒ String
Get detailed error message including location and suggestion
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 91 def 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
60 61 62 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 60 def error? @severity == :error end |
#formatted_location ⇒ String
Get formatted location 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
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
115 116 117 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 115 def has_suggestion? !@suggestion.nil? && !@suggestion.empty? end |
#hash ⇒ Integer
Generate hash code for use in hashes and sets
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
74 75 76 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 74 def info? @severity == :info end |
#inspect ⇒ String
Detailed string representation with all information
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_h ⇒ Hash
Convert error to hash representation
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_json ⇒ String
Convert error to JSON
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_s ⇒ String
String representation
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
67 68 69 |
# File 'lib/lutaml/xsd/validation/validation_error.rb', line 67 def warning? @severity == :warning end |