Method: Eye::Dsl::Validation::ClassMethods#validate_param

Defined in:
lib/eye/dsl/validation.rb

#validate_param(param, value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/eye/dsl/validation.rb', line 78

def validate_param(param, value)
  param = param.to_sym
  types = validates[param]
  if !types && param != :type
    raise Error, "#{name} unknown param :#{param} value #{value.inspect}"
  end

  if variants[param] && value && !value.is_a?(Proc)
    if value.is_a?(Array)
      value = value.reject { |v| v.is_a?(Proc) }
      if (value - variants[param]).present?
        raise Error, "#{value.inspect} should be within #{variants[param].inspect}"
      end
    elsif !variants[param].include?(value)
      raise Error, "#{value.inspect} should be within #{variants[param].inspect}"
    end
  end

  if types.present?
    types = Array(types)
    good = types.any? { |type| value.is_a?(type) }
    raise Error, "#{name} bad param :#{param} value #{value.inspect}, type #{types.inspect}" unless good
  end
end