Skip to content

Instantly share code, notes, and snippets.

@wpeterson
Created February 2, 2013 18:09
Show Gist options
  • Save wpeterson/4698635 to your computer and use it in GitHub Desktop.
Save wpeterson/4698635 to your computer and use it in GitHub Desktop.
class CoercedValueValidator < ActiveModel::Validator
def validate(record)
coercible_attrs = record.keys.select { |k,v| [Integer, Float, Date, Time].include?(v.type) }.map(&:first)
coercible_attrs.each do |attr|
record.errors.add(attr, 'is invalid') if record.send("#{attr}_before_type_cast").present? && record.send(attr).nil?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment