Class: Lutaml::Xsd::Validation::ResultCollector

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

Overview

ResultCollector aggregates validation results

This class collects validation errors, warnings, and info messages during the validation process and provides methods to aggregate them into a final ValidationResult.

Examples:

Collect validation results

collector = ResultCollector.new(config)
collector.add_error(error1)
collector.add_warning(warning1)
result = collector.to_result

Defined Under Namespace

Classes: StopValidationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ResultCollector

Initialize a new ResultCollector

Parameters:



36
37
38
39
40
41
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 36

def initialize(config)
  @config = config
  @errors = []
  @warnings = []
  @infos = []
end

Instance Attribute Details

#configValidationConfiguration (readonly)

Returns The validation configuration.

Returns:



22
23
24
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 22

def config
  @config
end

#errorsArray<ValidationError> (readonly)

Returns Collection of errors.

Returns:



25
26
27
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 25

def errors
  @errors
end

#infosArray<ValidationError> (readonly)

Returns Collection of info messages.

Returns:



31
32
33
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 31

def infos
  @infos
end

#warningsArray<ValidationError> (readonly)

Returns Collection of warnings.

Returns:



28
29
30
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 28

def warnings
  @warnings
end

Instance Method Details

#add_error(error) ⇒ void

This method returns an undefined value.

Add an error to the collection

Parameters:

Raises:

  • (ArgumentError)

    if error is not a ValidationError



49
50
51
52
53
54
55
56
57
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 49

def add_error(error)
  validate_error!(error)
  return if should_skip_error?(error)

  @errors << error

  # Stop collecting if configured to stop on first error
  raise StopValidationError if stop_on_first_error? && has_errors?
end

#add_info(info) ⇒ void

This method returns an undefined value.

Add an info message to the collection

Parameters:

Raises:

  • (ArgumentError)

    if info is not a ValidationError



78
79
80
81
82
83
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 78

def add_info(info)
  validate_error!(info)
  return if should_skip_error?(info)

  @infos << info
end

#add_issue(issue) ⇒ void

This method returns an undefined value.

Add an issue based on its severity

Automatically routes the issue to the appropriate collection based on its severity level.

Parameters:



92
93
94
95
96
97
98
99
100
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 92

def add_issue(issue)
  case issue.severity
  when :error then add_error(issue)
  when :warning then add_warning(issue)
  when :info then add_info(issue)
  else
    raise ArgumentError, "Invalid severity: #{issue.severity}"
  end
end

#add_warning(warning) ⇒ void

This method returns an undefined value.

Add a warning to the collection

Parameters:

Raises:

  • (ArgumentError)

    if warning is not a ValidationError



65
66
67
68
69
70
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 65

def add_warning(warning)
  validate_error!(warning)
  return if should_skip_error?(warning)

  @warnings << warning
end

#all_issuesArray<ValidationError>

Get all issues (errors + warnings + infos)

Returns:



172
173
174
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 172

def all_issues
  @errors + @warnings + @infos
end

#clearvoid

This method returns an undefined value.

Clear all collected results



163
164
165
166
167
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 163

def clear
  @errors.clear
  @warnings.clear
  @infos.clear
end

#count_by_severityHash<Symbol, Integer>

Get count by severity

Returns:

  • (Hash<Symbol, Integer>)


140
141
142
143
144
145
146
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 140

def count_by_severity
  {
    errors: @errors.size,
    warnings: @warnings.size,
    infos: @infos.size,
  }
end

#has_errors?Boolean

Check if there are any errors

Returns:

  • (Boolean)


105
106
107
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 105

def has_errors?
  @errors.any?
end

#has_infos?Boolean

Check if there are any info messages

Returns:

  • (Boolean)


119
120
121
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 119

def has_infos?
  @infos.any?
end

#has_issues?Boolean

Check if there are any issues at all

Returns:

  • (Boolean)


126
127
128
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 126

def has_issues?
  has_errors? || has_warnings? || has_infos?
end

#has_warnings?Boolean

Check if there are any warnings

Returns:

  • (Boolean)


112
113
114
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 112

def has_warnings?
  @warnings.any?
end

#issues_at_location(location) ⇒ Array<ValidationError>

Filter issues by location

Parameters:

  • location (String)

    Location to filter by

Returns:



188
189
190
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 188

def issues_at_location(location)
  all_issues.select { |issue| issue.location == location }
end

#issues_with_code(code) ⇒ Array<ValidationError>

Filter issues by code

Parameters:

  • code (String)

    Error code to filter by

Returns:



180
181
182
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 180

def issues_with_code(code)
  all_issues.select { |issue| issue.code == code }
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)


195
196
197
198
199
200
201
202
203
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 195

def to_h
  {
    errors: @errors.map(&:to_h),
    warnings: @warnings.map(&:to_h),
    infos: @infos.map(&:to_h),
    counts: count_by_severity,
    valid: !has_errors?,
  }
end

#to_resultValidationResult

Convert collected results to a ValidationResult

Returns:



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

def to_result
  ValidationResult.new(
    valid: !has_errors?,
    errors: @errors.dup,
    warnings: @warnings.dup,
    infos: @infos.dup,
  )
end

#total_countInteger

Get total count of all issues

Returns:

  • (Integer)


133
134
135
# File 'lib/lutaml/xsd/validation/result_collector.rb', line 133

def total_count
  @errors.size + @warnings.size + @infos.size
end