class Apipie::Validator::HashValidator

Public Class Methods

build(param_description, argument, options, block) click to toggle source
# File lib/apipie/validator.rb, line 277
def self.build(param_description, argument, options, block)
  self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
end
new(param_description, argument, param_group) click to toggle source
Calls superclass method Apipie::Validator::BaseValidator.new
# File lib/apipie/validator.rb, line 281
def initialize(param_description, argument, param_group)
  super(param_description)
  @proc = argument
  @param_group = param_group
  self.instance_exec(&@proc)
  # specifying action_aware on Hash influences the child params,
  # not the hash param itself: assuming it's required when
  # updating as well
  if param_description.options[:action_aware] && param_description.options[:required]
    param_description.required = true
  end
  prepare_hash_params
end

Public Instance Methods

_default_param_group_scope() click to toggle source

where the group definition should be looked up when no scope given. This is expected to return a controller.

# File lib/apipie/validator.rb, line 338
def _default_param_group_scope
  @param_group && @param_group[:scope]
end
description() click to toggle source
# File lib/apipie/validator.rb, line 328
def description
  "Must be a Hash"
end
expected_type() click to toggle source
# File lib/apipie/validator.rb, line 332
def expected_type
  'hash'
end
merge_with(other_validator) click to toggle source
# File lib/apipie/validator.rb, line 342
def merge_with(other_validator)
  if other_validator.is_a? HashValidator
    @params_ordered = ParamDescription.unify(self.params_ordered + other_validator.params_ordered)
    prepare_hash_params
  else
    super
  end
end
params_ordered() click to toggle source
# File lib/apipie/validator.rb, line 295
def params_ordered
  @params_ordered ||= _apipie_dsl_data[:params].map do |args|
    options = args.find { |arg| arg.is_a? Hash }
    options[:parent] = self.param_description
    Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
  end
end
prepare_hash_params() click to toggle source
# File lib/apipie/validator.rb, line 351
def prepare_hash_params
  @hash_params = params_ordered.reduce({}) do |h, param|
    h.update(param.name.to_sym => param)
  end
end
process_value(value) click to toggle source
# File lib/apipie/validator.rb, line 318
def process_value(value)
  if @hash_params && value
    return @hash_params.each_with_object({}) do |(key, param), api_params|
      if value.has_key?(key)
        api_params[param.as] = param.process_value(value[key])
      end
    end
  end
end
validate(value) click to toggle source
# File lib/apipie/validator.rb, line 303
def validate(value)
  return false if !value.is_a? Hash
  if @hash_params
    @hash_params.each do |k, p|
      if Apipie.configuration.validate_presence?
        raise ParamMissing.new(p) if p.required && !value.has_key?(k)
      end
      if Apipie.configuration.validate_value?
        p.validate(value[k]) if value.has_key?(k)
      end
    end
  end
  return true
end