class Apipie::Validator::ArrayValidator

arguments value must be an array

Public Class Methods

build(param_description, argument, options, block) click to toggle source
# File lib/apipie/validator.rb, line 182
def self.build(param_description, argument, options, block)
  if argument == Array && !block.is_a?(Proc)
    self.new(param_description, argument, options)
  end
end
new(param_description, argument, options={}) click to toggle source
Calls superclass method Apipie::Validator::BaseValidator.new
# File lib/apipie/validator.rb, line 158
def initialize(param_description, argument, options={})
  super(param_description)
  @type = argument
  @items_type = options[:of]
  @items_enum = options[:in]
end

Public Instance Methods

description() click to toggle source
# File lib/apipie/validator.rb, line 174
def description
  "Must be an array of #{items}"
end
expected_type() click to toggle source
# File lib/apipie/validator.rb, line 178
def expected_type
  "array"
end
process_value(values) click to toggle source
# File lib/apipie/validator.rb, line 170
def process_value(values)
  values || []
end
validate(values) click to toggle source
# File lib/apipie/validator.rb, line 165
def validate(values)
  return false unless process_value(values).respond_to?(:each) && !process_value(values).is_a?(String)
  process_value(values).all? { |v| validate_item(v)}
end

Private Instance Methods

enum() click to toggle source
# File lib/apipie/validator.rb, line 190
def enum
  if @items_enum.kind_of?(Proc)
    @items_enum = Array(@items_enum.call)
  end
  @items_enum
end
has_valid_type?(value) click to toggle source
# File lib/apipie/validator.rb, line 202
def has_valid_type?(value)
  if @items_type
    value.kind_of?(@items_type)
  else
    true
  end
end
is_valid_value?(value) click to toggle source
# File lib/apipie/validator.rb, line 210
def is_valid_value?(value)
  if enum
    enum.include?(value)
  else
    true
  end
end
items() click to toggle source
# File lib/apipie/validator.rb, line 218
def items
  unless enum
    @items_type || "any type"
  else
    enum.inspect
  end
end
validate_item(value) click to toggle source
# File lib/apipie/validator.rb, line 197
def validate_item(value)
  has_valid_type?(value) &&
    is_valid_value?(value)
end