Class: Validation::Rule::Type

Inherits:
StretchyRule show all
Defined in:
lib/validation/rule/type.rb

Instance Method Summary collapse

Methods inherited from StretchyRule

#empty_ok?, #invalid_empty?, #is_empty?, #params, #required?

Constructor Details

#initialize(params = {}) ⇒ Type

Returns a new instance of Type.



5
6
7
8
9
10
11
12
# File 'lib/validation/rule/type.rb', line 5

def initialize(params = {})
  if params.is_a?(Hash)
    @params = params
    @params[:classes] = Array(params[:classes])
  else
    @params = { classes: Array(params) }
  end
end

Instance Method Details

#error_keyObject



14
15
16
# File 'lib/validation/rule/type.rb', line 14

def error_key
  :type_rule
end

#valid_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/validation/rule/type.rb', line 27

def valid_type?(value)
  params[:classes].any? {|type| value.is_a?(type) }
end

#valid_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/validation/rule/type.rb', line 18

def valid_value?(value)
  return true if value.nil? && !params[:required]
  if params[:array]
    value.all? {|v| valid_type?(v) }
  else
    valid_type?(value)
  end
end