Sha256: 0630997f89257e6dc1d0beb5c2848c33d72a5d9ed5cb4cdb4f54121e7c7ed042

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

module SoberSwag
  module Reporting
    module Input
      ##
      # Specify that an item must be within a given range in ruby.
      # This gets translated to `minimum` and `maximum` keys in swagger.
      #
      # This works with endless ranges (Ruby 2.6+) and beginless ranges (Ruby 2.7+)
      class InRange < Base
        def initialize(input, range)
          @input = input
          @range = range
        end

        ##
        # @return [Interface]
        attr_reader :input

        ##
        # @return [Range]
        attr_reader :range

        ##
        # @return [Range]
        def call(value)
          res = input.call(value)

          return res if res.is_a?(Report::Base)
          return Report::Value.new(['was not in minimum/maximum range']) unless range.member?(res)

          res
        end

        def swagger_schema
          schema, found = input.swagger_schema

          merged =
            if schema.key?(:$ref)
              { allOf: [schema] }
            else
              schema
            end.merge(maximum_portion).merge(minimum_portion)

          [merged, found]
        end

        def maximum_portion
          return {} unless range.end

          { maximum: range.end, exclusiveMaximum: range.exclude_end? }
        end

        def minimum_portion
          return {} unless range.begin

          { minimum: range.begin, exclusiveMinimum: false }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sober_swag-0.25.2 lib/sober_swag/reporting/input/in_range.rb
sober_swag-0.25.1 lib/sober_swag/reporting/input/in_range.rb
sober_swag-0.25.0 lib/sober_swag/reporting/input/in_range.rb
sober_swag-0.24.1 lib/sober_swag/reporting/input/in_range.rb
sober_swag-0.24.0 lib/sober_swag/reporting/input/in_range.rb
sober_swag-0.23.0 lib/sober_swag/reporting/input/in_range.rb