Add ISO standard validators

This commit is contained in:
Artur Beljajev 2017-08-18 13:02:54 +03:00
parent 937ce9b082
commit dfca1d4e69
5 changed files with 60 additions and 0 deletions

View file

@ -0,0 +1,11 @@
class Iso31661Alpha2Validator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors.add(attribute, :invalid_iso31661_alpha2) unless valid_country_code?(value)
end
private
def valid_country_code?(country_code)
Country.new(country_code)
end
end

View file

@ -0,0 +1,13 @@
class Iso8601Validator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if options[:date_only]
record.errors.add(attribute, :invalid_iso8601) unless value =~ date_format
end
end
private
def date_format
/\d{4}-\d{2}-\d{2}/
end
end