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 211
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 187
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 203
def description
  "Must be an array of #{items}"
end
expected_type() click to toggle source
# File lib/apipie/validator.rb, line 207
def expected_type
  "array"
end
process_value(values) click to toggle source
# File lib/apipie/validator.rb, line 199
def process_value(values)
  values || []
end
validate(values) click to toggle source
# File lib/apipie/validator.rb, line 194
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 219
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 231
def has_valid_type?(value)
  if @items_type
    item_validator = BaseValidator.find('', @items_type, nil, nil)

    if item_validator
      item_validator.valid?(value)
    else
      value.kind_of?(@items_type)
    end
  else
    true
  end
end
is_valid_value?(value) click to toggle source
# File lib/apipie/validator.rb, line 245
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 253
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 226
def validate_item(value)
  has_valid_type?(value) &&
    is_valid_value?(value)
end