class Seahorse::Client::ParamValidator

Public Class Methods

new(shape, options = {}) click to toggle source

@param [Model::Shapes::Shape] shape @option options [Boolean] :validate_required (true)

# File lib/seahorse/client/param_validator.rb, line 14
def initialize(shape, options = {})
  @shape = shape || Seahorse::Model::Shapes::Structure.new
  @validate_required = options[:validate_required] != false
end
validate!(shape, params) click to toggle source

@param [Model::Shapes::Shape] shape @param [Hash] params @return [void]

# File lib/seahorse/client/param_validator.rb, line 8
def self.validate!(shape, params)
  new(shape).validate!(params)
end

Public Instance Methods

validate!(params) click to toggle source

@param [Hash] params @return [void]

# File lib/seahorse/client/param_validator.rb, line 21
def validate!(params)
  errors = []
  shape(@shape, params, errors, context = 'params')
  raise ArgumentError, error_messages(errors) unless errors.empty?
end

Private Instance Methods

error_messages(errors) click to toggle source
# File lib/seahorse/client/param_validator.rb, line 127
def error_messages(errors)
  if errors.size == 1
    errors.first
  else
    prefix = "\n  - "
    "parameter validator found #{errors.size} errors:" +
      prefix + errors.join(prefix)
  end
end
hash?(value, errors, context) click to toggle source
# File lib/seahorse/client/param_validator.rb, line 112
def hash?(value, errors, context)
  if value.is_a?(Hash)
    true
  else
    errors << "expected #{context} to be a hash"
    false
  end
end
io_like?(value) click to toggle source
# File lib/seahorse/client/param_validator.rb, line 121
def io_like?(value)
  value.respond_to?(:read) &&
  value.respond_to?(:rewind) &&
  value.respond_to?(:size)
end
list(list, values, errors, context) click to toggle source
# File lib/seahorse/client/param_validator.rb, line 56
def list(list, values, errors, context)
  # ensure the value is an array
  unless values.is_a?(Array)
    errors << "expected #{context} to be an array"
    return
  end

  # validate members
  values.each.with_index do |value, index|
    shape(list.member, value, errors, context + "[#{index}]")
  end
end
map(map, values, errors, context) click to toggle source
# File lib/seahorse/client/param_validator.rb, line 69
def map(map, values, errors, context)
  return unless hash?(values, errors, context)
  values.each do |key, value|
    shape(map.key, key, errors, "#{context} #{key.inspect} key")
    shape(map.value, value, errors, context + "[#{key.inspect}]")
  end
end
shape(shape, value, errors, context) click to toggle source
# File lib/seahorse/client/param_validator.rb, line 77
def shape(shape, value, errors, context)
  case shape
  when Model::Shapes::Structure
    structure(shape, value, errors, context)
  when Model::Shapes::List
    list(shape, value, errors, context)
  when Model::Shapes::Map
    map(shape, value, errors, context)
  when Model::Shapes::String
    unless value.is_a?(String)
      errors << "expected #{context} to be a string"
    end
  when Model::Shapes::Integer
    unless value.is_a?(Integer)
      errors << "expected #{context} to be an integer"
    end
  when Model::Shapes::Float
    unless value.is_a?(Float)
      errors << "expected #{context} to be a float"
    end
  when Model::Shapes::Timestamp
    unless value.is_a?(Time)
      errors << "expected #{context} to be a Time object"
    end
  when Model::Shapes::Boolean
    unless [true, false].include?(value)
      errors << "expected #{context} to be true or false"
    end
  when Model::Shapes::Blob
    unless io_like?(value) or value.is_a?(String)
      errors << "expected #{context} to be a string or IO object"
    end
  end
end
structure(structure, values, errors, context) click to toggle source
# File lib/seahorse/client/param_validator.rb, line 29
def structure(structure, values, errors, context)
  # ensure the value is hash like
  return unless hash?(values, errors, context)

  # ensure required members are present
  if @validate_required
    structure.required.each do |member_name|
      if values[member_name].nil?
        param = "#{context}[#{member_name.inspect}]"
        errors << "missing required parameter #{param}"
      end
    end
  end

  # validate non-nil members
  values.each do |name, value|
    unless value.nil?
      if structure.member?(name)
        member_shape = structure.member(name)
        shape(member_shape, value, errors, context + "[#{name.inspect}]")
      else
        errors << "unexpected value at #{context}[#{name.inspect}]"
      end
    end
  end
end