Class: TypeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/type_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/validators/type_validator.rb', line 2

def validate_each(record, attribute, value)
  required_type = options[:with]

  if value.is_a?(Array)
    unless value.all? { |i| i.instance_of?(required_type) }
      record.errors.add attribute, (options[:message] || "is not of class #{required_type}")
    end
  else
    unless value.instance_of?(required_type)
      record.errors.add attribute, (options[:message] || "is not of class #{required_type}")
    end
  end
end