mirror of
https://github.com/internetee/registry.git
synced 2025-05-16 17:37:17 +02:00
Add ISO standard validators
This commit is contained in:
parent
937ce9b082
commit
dfca1d4e69
5 changed files with 60 additions and 0 deletions
|
@ -235,6 +235,8 @@ en:
|
|||
unimplemented_command: 'Unimplemented command'
|
||||
domain_exists_but_belongs_to_other_registrar: 'Domain exists but belongs to other registrar'
|
||||
required_ident_attribute_missing: "Required ident attribute missing: %{key}"
|
||||
invalid_iso31661_alpha2: does not conform to ISO 3166-1 alpha-2 standard
|
||||
invalid_iso8601: does not conform to ISO 8601 standard
|
||||
|
||||
code: 'Code'
|
||||
value: 'Value'
|
||||
|
|
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
|
17
spec/lib/validators/iso31661_alpha2.rb
Normal file
17
spec/lib/validators/iso31661_alpha2.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
# https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
|
||||
|
||||
RSpec.shared_examples 'iso31661_alpha2' do
|
||||
describe 'validation' do
|
||||
it 'rejects invalid' do
|
||||
model.send("#{attribute}=", 'invalid')
|
||||
model.validate
|
||||
expect(model.errors).to be_added(attribute, :invalid_iso31661_alpha2)
|
||||
end
|
||||
|
||||
it 'accepts valid' do
|
||||
model.send("#{attribute}=", 'US')
|
||||
model.validate
|
||||
expect(model.errors).to_not be_added(attribute, :invalid_iso31661_alpha2)
|
||||
end
|
||||
end
|
||||
end
|
17
spec/lib/validators/iso8601.rb
Normal file
17
spec/lib/validators/iso8601.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
# https://en.wikipedia.org/wiki/ISO_8601
|
||||
|
||||
RSpec.shared_examples 'iso8601' do
|
||||
describe 'validation' do
|
||||
it 'rejects invalid' do
|
||||
model.send("#{attribute}=", '2010-07-0')
|
||||
model.validate
|
||||
expect(model.errors).to be_added(attribute, :invalid_iso8601)
|
||||
end
|
||||
|
||||
it 'accepts valid' do
|
||||
model.send("#{attribute}=", '2010-07-05')
|
||||
model.validate
|
||||
expect(model.errors).to_not be_added(attribute, :invalid_iso8601)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue