mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 01:47:18 +02:00
Add ISO standard validators
This commit is contained in:
parent
937ce9b082
commit
dfca1d4e69
5 changed files with 60 additions and 0 deletions
11
lib/validators/iso31661_alpha2_validator.rb
Normal file
11
lib/validators/iso31661_alpha2_validator.rb
Normal 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
|
13
lib/validators/iso8601_validator.rb
Normal file
13
lib/validators/iso8601_validator.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue