Domain name validator

This commit is contained in:
Martin Lensment 2014-06-30 13:40:58 +03:00
parent 8f131c2fe4
commit b670331b1a
4 changed files with 25 additions and 0 deletions

View file

@ -5,6 +5,8 @@ class Domain < ActiveRecord::Base
belongs_to :technical_contact, class_name: 'Contact' belongs_to :technical_contact, class_name: 'Contact'
belongs_to :admin_contact, class_name: 'Contact' belongs_to :admin_contact, class_name: 'Contact'
validates :name, domain_name: true
class << self class << self
def check_availability(domains) def check_availability(domains)
res = [] res = []

View file

@ -0,0 +1,10 @@
class DomainNameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
ok = value =~ /\A[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\.ee\z/
ok &&= !(value[2] == '-' && value[3] == '-')
unless ok
record.errors[attribute] << (options[:message] || 'invalid format')
end
end
end

View file

@ -0,0 +1 @@
Dir[File.join(Rails.root, "app", "validators", "*.rb")].each {|x| require x }

View file

@ -10,5 +10,17 @@ describe Domain do
it 'creates a resource' do it 'creates a resource' do
d = Fabricate(:domain) d = Fabricate(:domain)
expect(d.name).to_not be_nil expect(d.name).to_not be_nil
invalid = ['a.ee', "#{'a' * 64}.ee", 'ab.eu', 'test.ab.ee', '-test.ee', '-test-.ee', 'test-.ee', 'te--st.ee']
invalid.each do |x|
expect(Fabricate.build(:domain, name: x).valid?).to be false
end
valid = ['ab.ee', "#{'a' * 63}.ee", 'te-s-t.ee']
valid.each do |x|
expect(Fabricate.build(:domain, name: x).valid?).to be true
end
end end
end end