Browse Source

better validations

pull/1/head
Nicolae Claudius 13 years ago
parent
commit
3bc003f0b9
  1. 4
      app/models/a.rb
  2. 1
      app/models/cname.rb
  3. 5
      app/models/mx.rb
  4. 1
      app/models/ns.rb
  5. 3
      app/models/record.rb
  6. 10
      app/models/soa.rb
  7. 3
      app/models/txt.rb

4
app/models/a.rb

@ -11,7 +11,7 @@
# Obtained from http://www.zytrax.com/books/dns/ch8/a.html
#
class A < Record
# Only accept valid IPv4 addresses
validates :content, :presence => true, :ip => true
validates :name, :hostname => {:allow_underscore => true, :allow_wildcard_hostname => true}
validates :content, :presence => true, :ip => {:ip_type => :v4} # Only accept valid IPv4 addresses
end

1
app/models/cname.rb

@ -8,6 +8,7 @@
# Obtained from http://www.zytrax.com/books/dns/ch8/cname.html
#
class CNAME < Record
validates :name, :hostname => {:allow_underscore => true, :allow_wildcard_hostname => true}
validates :content, :presence => true, :hostname => true
end

5
app/models/mx.rb

@ -7,12 +7,13 @@
# Obtained from http://www.zytrax.com/books/dns/ch8/mx.html
#
class MX < Record
validates :prio, :numericality => {
validates :name, :hostname => {:allow_underscore => true, :allow_wildcard_hostname => true}
validates :content, :presence => true, :hostname => true
validates :prio, :presence => true, :numericality => {
:greater_than_or_equal_to => 0,
:less_than_or_equal_to => 65535,
:only_integer => true
}
validates :content, :presence => true, :hostname => true
def supports_priority?; true end
end

1
app/models/ns.rb

@ -20,6 +20,7 @@
# Obtained from http://www.zytrax.com/books/dns/ch8/ns.html
#
class NS < Record
validates :name, :hostname => {:allow_underscore => true}
validates :content, :presence => true, :hostname => true, :inclusion => {:in => Settings.ns}
def to_label; "#{content}" end

3
app/models/record.rb

@ -11,7 +11,8 @@ class Record < ActiveRecord::Base
validates :ttl, :numericality => {
# :greater_than_or_equal_to => 0,
:greater_than_or_equal_to => Settings.min_ttl.to_i,
:less_than => 2**31
:less_than => 2**31,
:only_integer => true
}, :allow_blank => true
before_validation :prepare_name!

10
app/models/soa.rb

@ -7,8 +7,9 @@
# Obtained from http://www.zytrax.com/books/dns/ch8/soa.html
#
class SOA < Record
validates :domain_id, :uniqueness => true # one SOA per domain
validates :domain_id, :presence => true, :uniqueness => true # one SOA per domain
validates :name, :presence => true, :hostname => true
validate :name_equals_dmain_name?
validates :content, :presence => true
validates :primary_ns, :presence => true
CONTACT_FORMAT = /\A[a-zA-Z0-9\-\.]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,6}\z/
@ -72,6 +73,13 @@ class SOA < Record
@serial = @serial.to_i unless @serial.nil?
update_serial if @serial.nil? || @serial.zero?
end
def name_equals_dmain_name?
unless name == domain.name
errors.add :name, "must be equal to domain's name"
end
end
end
Soa = SOA

3
app/models/txt.rb

@ -9,7 +9,8 @@
#
# Obtained from http://www.zytrax.com/books/dns/ch8/txt.html
class TXT < Record
validate :content, :presence => true
validates :name, :hostname => {:allow_underscore => true, :allow_wildcard_hostname => true}
validates :content, :presence => true, :length => { :maximum => 255 }
end

Loading…
Cancel
Save