Sha256: 9a05d0255866915cb763086a573f6587398c6ec131a6f2655f576328974c51b6

Contents?: true

Size: 1.94 KB

Versions: 7

Compression:

Stored size: 1.94 KB

Contents

module SoberSwag
  module Reporting
    module Report
      ##
      # Base class for SoberSwag reports.
      #
      # These reports are what make these serializers and parsers *reporting*: they provide errors.
      # For outputs, these are errors encountered during serialization, IE,
      # places where we lied about what type we were going to serialize.
      # This is mostly used for testing.
      #
      # For parsers, these are encountered during *parsing*.
      # This can be easily converted into a hash of JSON path objects to individual errors,
      # enabling developers to more easily see what's gone wrong.
      class Base
        ##
        # @return [Array<[String]>]
        #   An array of error paths and error components, in the form of:
        #
        #   ```ruby
        #     [
        #       'foo.bar: was bad',
        #       'foo.bar: was even worse'
        #     ]
        #   ```
        def full_errors
          each_error.map do |k, v|
            [k, v].reject(&:blank?).join(': ')
          end
        end

        ##
        # Get a hash where each key is a JSON path, and each value is an array of errors for that path.
        # @return [Hash<String,Array<String>>] hash of JSON path to errors
        def path_hash
          Hash.new { |h, k| h[k] = [] }.tap do |hash|
            each_error do |k, v|
              hash["$#{k}"] << v
            end
          end
        end

        ##
        # @overload each_error() { |path, val| nil }
        #   Yields each error to the block.
        #   @yield [path, val] the JSON path to the error, and an error string
        #   @yieldparam [String, String]
        # @overload each_error()
        #   @return [Enumerable<String, String>] an enum of two values: error keys and error values.
        #     Note: the same key can potentially occur more than once!
        def each_error
          return enum_for(:each_error) unless block_given?
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sober_swag-0.25.2 lib/sober_swag/reporting/report/base.rb
sober_swag-0.25.1 lib/sober_swag/reporting/report/base.rb
sober_swag-0.25.0 lib/sober_swag/reporting/report/base.rb
sober_swag-0.24.1 lib/sober_swag/reporting/report/base.rb
sober_swag-0.24.0 lib/sober_swag/reporting/report/base.rb
sober_swag-0.23.0 lib/sober_swag/reporting/report/base.rb
sober_swag-0.22.0 lib/sober_swag/reporting/report/base.rb