class HashParams::HashValidator

Public Instance Methods

key(hash_key, type, opts={}) click to toggle source
# File lib/hash_params/hash_validator.rb, line 54
def key(hash_key, type, opts={})
  value = @incoming[hash_key] || @incoming[hash_key.to_s]
  # if a block is given to the param then it's a recursive call
  # recursive calls can only be done with a hash
  new_value = if value.is_a?(Hash)
                if block_given?
                  #if the param is a hash then the validations are actually options
                  HashParams::HashValidator.new.validate_hash(value, @options, &Proc.new)
                else
                  HashParams::HashValidator.new.validate_hash(value, opts)
                end
              else
                HashParams.validate value, type, opts
              end
  hash_key = opts[:as] if opts[:as]
  @outgoing.set_key_value(hash_key, new_value, @options[:symbolize_keys], @options[:make_methods])
  new_value
rescue => e
  @outgoing.validation_errors << "Error processing key '#{hash_key}': #{e}" # [e.to_s, e.backtrace].join("\n")
  raise e if @options[:raise_errors]
  nil
end
Also aliased as: param
param(hash_key, type, opts={})
Alias for: key
validate_hash(h, options={}) click to toggle source
# File lib/hash_params/hash_validator.rb, line 31
def validate_hash(h, options={})
  #Hash Validation has to be stateful

  @incoming = h
  @outgoing = HPHash.new
  @options = options

  if block_given?
    instance_eval(&Proc.new)
  else
    #no proc was given this means just pass the hash back as is
    if @options.empty?
      @outgoing = @incoming
    else
      h.each do |k, v|
        key k, v.class, @options
      end
    end

  end
  @outgoing
end