Class: Openapi3Parser::Validation::ErrorCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/openapi3_parser/validation/error_collection.rb

Overview

An immutable collection of Validation::Error objects

Defined Under Namespace

Classes: LocationTypeGroup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors = []) ⇒ ErrorCollection

Returns a new instance of ErrorCollection.

Parameters:



22
23
24
# File 'lib/openapi3_parser/validation/error_collection.rb', line 22

def initialize(errors = [])
  @errors = errors.freeze
end

Instance Attribute Details

#errorsArray<Validation::Error> (readonly) Also known as: to_a

Returns the current value of errors.

Returns:



7
8
9
# File 'lib/openapi3_parser/validation/error_collection.rb', line 7

def errors
  @errors
end

Class Method Details

.combine(errors, other_errors) ⇒ ErrorCollection

Combines ErrorCollection objects or arrays of Validation::Error objects

Parameters:

Returns:



14
15
16
# File 'lib/openapi3_parser/validation/error_collection.rb', line 14

def self.combine(errors, other_errors)
  new(errors.to_a + other_errors.to_a)
end

Instance Method Details

#each(&block) ⇒ Object



30
31
32
# File 'lib/openapi3_parser/validation/error_collection.rb', line 30

def each(&block)
  errors.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/openapi3_parser/validation/error_collection.rb', line 26

def empty?
  errors.empty?
end

#group_errorsArray<LocationTypeGroup]

Group errors by those in the same location for the same node

Returns:



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/openapi3_parser/validation/error_collection.rb', line 37

def group_errors
  grouped = group_by do |e|
    [e.source_location.to_s, e.for_type]
  end

  grouped.map do |_, errors|
    LocationTypeGroup.new(errors[0].source_location,
                          errors[0].for_type,
                          errors)
  end
end

#inspectString

Returns:

  • (String)


65
66
67
# File 'lib/openapi3_parser/validation/error_collection.rb', line 65

def inspect
  "#{self.class.name}(errors: #{to_h})"
end

#to_hHash

Return a hash structure where the location is key and the errors are values

Returns:

  • (Hash)


53
54
55
56
57
58
59
60
61
62
# File 'lib/openapi3_parser/validation/error_collection.rb', line 53

def to_h
  grouped = group_errors.group_by { |g| g.source_location.to_s }

  grouped.each_with_object({}) do |(_, items), memo|
    items.each do |item|
      key = item.location_summary(with_type: items.count > 1)
      memo[key] = (memo[key] || []) + item.errors.map(&:to_s)
    end
  end
end