class Object

Public Instance Methods

is_valid(payload, t, name, *keys) click to toggle source

Validator method

# File lib/util/util.rb, line 5
def is_valid(payload, t, name, *keys)

  return {
    :ok=>false,
    :reason => 'nil payload is not accepted!',
    :err => Exception.new("payload is nil!")
  } if payload.nil?
  if keys.any?
    path = ''
    temp = nil
    keys.each {
      |key|
      if temp.nil?
        path += key
        return {
            :ok => false,
            :reason => 'Field does not exist',
            :err => Exception.new("Expected field <#{key}> can not be found in #{name}")
        } if payload[key.to_sym].nil? && payload[key].nil?
        temp = payload[key] || payload[key.to_sym]
      else
        path += '.'+key
        return {
            :ok => false,
            :reason => 'Field does not exist',
            :err => Exception.new("Expected field <#{path}> can not be found in #{name}")
        } if temp[key.to_sym].nil? && temp[key].nil?
        temp = temp[key] || temp[key.to_sym]
      end
    }

    temp.is_a?(t.class) ? {:ok => true} : {
        :ok => false,
        :reason => 'Unexpected format!',
        :err => Exception.new("Expected format for #{path} is #{t.class.name}, found #{temp.class.name}")
    }
  else
    payload.is_a?(t.class) ? {:ok => true} : {
        :ok => false,
        :reason => 'Unexpected format!',
        :err => Exception.new("Expected format for #{name} is #{t.class.name}, found #{payload.class.name}")
    }
  end
end