mirror of
https://github.com/internetee/registry.git
synced 2025-05-30 09:30:03 +02:00
Domain name validator
This commit is contained in:
parent
8f131c2fe4
commit
b670331b1a
4 changed files with 25 additions and 0 deletions
|
@ -5,6 +5,8 @@ class Domain < ActiveRecord::Base
|
|||
belongs_to :technical_contact, class_name: 'Contact'
|
||||
belongs_to :admin_contact, class_name: 'Contact'
|
||||
|
||||
validates :name, domain_name: true
|
||||
|
||||
class << self
|
||||
def check_availability(domains)
|
||||
res = []
|
||||
|
|
10
app/validators/domain_name_validator.rb
Normal file
10
app/validators/domain_name_validator.rb
Normal 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
|
1
config/initializers/load_validators.rb
Normal file
1
config/initializers/load_validators.rb
Normal file
|
@ -0,0 +1 @@
|
|||
Dir[File.join(Rails.root, "app", "validators", "*.rb")].each {|x| require x }
|
|
@ -10,5 +10,17 @@ describe Domain do
|
|||
it 'creates a resource' do
|
||||
d = Fabricate(:domain)
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue