class SoberSwag::Reporting::Input::InRange

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+)

Attributes

input[R]

@return [Interface]

range[R]

@return [Range]

Public Class Methods

new(input, range) click to toggle source
# File lib/sober_swag/reporting/input/in_range.rb, line 10
def initialize(input, range)
  @input = input
  @range = range
end

Public Instance Methods

call(value) click to toggle source

@return [Range]

# File lib/sober_swag/reporting/input/in_range.rb, line 25
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
maximum_portion() click to toggle source
# File lib/sober_swag/reporting/input/in_range.rb, line 47
def maximum_portion
  return {} unless range.end

  { maximum: range.end, exclusiveMaximum: range.exclude_end? }
end
minimum_portion() click to toggle source
# File lib/sober_swag/reporting/input/in_range.rb, line 53
def minimum_portion
  return {} unless range.begin

  { minimum: range.begin, exclusiveMinimum: false }
end
swagger_schema() click to toggle source
# File lib/sober_swag/reporting/input/in_range.rb, line 34
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