Class ActiveRecord::Base
In: lib/dsadmin/model_validators.rb
Parent: Object

Collection of high-level validation helpers for O/R mapping objects (Derivatives of ActiveRecord::Base

All methods are built around AcriveRecord::Base#validates_each

Methods

Constants

EIGHTBIT_REGEX_S = '(0*(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5]))'   FIXME: This is duplicated in ValueChecker!
IPV4_REGEX_S = "(#{EIGHTBIT_REGEX_S}\\.){3}#{EIGHTBIT_REGEX_S}"
HOSTNAME_REGEX_S = '([\w-]+\.)*([a-zA-Z]{2,})'
EMAIL_REGEX = /\A[^@\s]+@[\w-]+(\.[\w-]+)*?(\.\w\w+)\z/
HOST_REGEX = Regexp.new("(\\A#{IPV4_REGEX_S}\\z)|(\\A#{HOSTNAME_REGEX_S}\\z)")

Protected Class methods

Check whether the given attributes contain well-formed email adresses

[Source]

    # File lib/dsadmin/model_validators.rb, line 47
47:   def self.validates_email(*attr_names)
48:     conf = { :message => " is not a well-formed email adress", :on => :save, :allow_nil => false }
49:     conf.update(attr_names.pop) if attr_names.last.is_a?(Hash)
50:     
51:     validates_each(attr_names, conf) do |record, attr_name, value|
52:       # strip comments
53:       val = value.to_s
54:       val.gsub!(/\([^()]*\)/, '')
55:       val.gsub!(/"[^"]*"/, '')
56:       val.sub!(/^.*<(.*)>\s*$/, '\\1')
57:       
58:       # strip leading/trailing whitespace
59:       val.strip!
60:       
61:       record.errors.add(attr_name, conf[:message]) unless val =~ EMAIL_REGEX
62:     end
63:   end

Check whether the given attributes contain well-formed hostnames or IP addresses (ipv4 only for now)

[Source]

    # File lib/dsadmin/model_validators.rb, line 68
68:   def self.validates_host_or_ip(*attr_names)
69:     conf = { :message => 'is not a well-formed hostname or IPv4 address', :on => :save, :allow_nil => false }
70:     conf.update(attr_names.pop) if attr_names.last.is_a?(Hash)
71:     
72:     validates_each(attr_names, conf) do |record, attr_name, value|
73:       # well-formed hostname? => ok
74:       next if(value =~ Dsadmin::ValueChecker::HOSTNAME_REGEX)
75: 
76:       # otherwise check if it's an IP address
77:       ip = IPAddr.new(value, Socket::AF_INET) rescue record.errors.add(attr_name, conf[:message])
78:     end
79:   end

Check whether the given attributes contain well-formed hostnames

[Source]

    # File lib/dsadmin/model_validators.rb, line 83
83:   def self.validates_hostname(*attr_names)
84:     conf = { :message => 'is not a well-formed hostname', :on => :save, :allow_nil => false, :allow_unqualified => true }
85:     conf.update(attr_names.pop) if attr_names.last.is_a?(Hash)
86:     
87:     if(conf[:allow_unqualified])
88:       conf[:with] = Dsadmin::ValueChecker::HOSTNAME_REGEX
89:     else
90:       conf[:with] = Dsadmin::ValueChecker::Q_HOSTNAME_REGEX
91:     end
92:     
93:     validates_format_of(attr_names, conf)
94:   end

Check whether the given attributes contain valid identifiers

[Source]

    # File lib/dsadmin/model_validators.rb, line 37
37:   def self.validates_identifier(*attr_names)
38:     conf = { :message => 'is not a valid identifier', :on => :save, :allow_nil => false }
39:     conf.update(attr_names.pop) if attr_names.last.is_a?(Hash)
40:     conf[:with] = /\A[a-zA-Z_]\w*\z/
41:     
42:     validates_format_of(attr_names, conf)
43:   end

Check whether the given attributes contain IP addresses (IPv4 only for now)

[Source]

     # File lib/dsadmin/model_validators.rb, line 98
 98:   def self.validates_ipv4(*attr_names)
 99:     conf = { :message => 'is not a well-formed IPv4 address', :on => :save, :allow_nil => false }
100:     conf.update(attr_names.pop) if attr_names.last.is_a?(Hash)
101:     
102:     validates_each(attr_names, conf) do |record, attr_name, value|
103:       ip = IPAddr.new(value, Socket::AF_INET) rescue record.errors.add(attr_name, conf[:message])
104:     end
105:   end

Check whether the given attributes contain well-formed filesystem paths

Nonstandard Options:

 * :kind : One of "abs" (absolute paths only), "rel" (relative paths only) or anything else (don't care) [any]
 * :allow_up : (boolean) Whether the paths may contain ".." parts [false]
 * :depth :    (range) The number of elements the path may contain. Use together with allow_up=false! [no limit]

[Source]

     # File lib/dsadmin/model_validators.rb, line 114
114:   def self.validates_path(*attr_names)
115:     conf = { :message => " is not a well-formed path", :on => :save, :allow_nil => false, :allow_up => false }
116:     conf.update(attr_names.pop) if attr_names.last.is_a?(Hash)
117:     
118:     validates_each(attr_names, conf) do |record, attr_name, value|
119:       p = Pathname.new(value.to_s).cleanpath
120:       
121:       if((conf[:kind] == "abs") && p.relative?)
122:         record.errors.add(attr_name, "has to be an absolute path")
123:         next
124:       elsif((conf[:kind] == "rel") && p.absolute?)
125:         record.errors.add(attr_name, "has to be a relative path")
126:         next
127:       end
128:       
129:       count = 0
130:       p.each_filename { |part|
131:         if(!conf[:allow_up] && (part == '..'))
132:           record.errors.add(attr_name, "may not contain '..'")
133:           next
134:         end
135:         count += 1
136:       }
137:       
138:       if(conf[:depth] && (!conf[:depth].include?(count)))
139:         record.errors.add(attr_name, "must have a depth in (#{conf[:depth].first}..#{conf[:depth].last})")
140:         next
141:       end
142:     end
143:   end

Check if the given attributes contain well-formed Dsadmin-Cron timespecs

Nonstandard Options:

 * :kind : One of "abs" (absolute specs only), "rel" (relative specs only) or anything else (don't care) [any]

[Source]

     # File lib/dsadmin/model_validators.rb, line 167
167:   def self.validates_timespec(*attr_names)
168:     conf = { :message => " is not a well-formed internal cron timespec", :on => :save, :allow_nil => false }
169:     conf.update(attr_names.pop) if attr_names.last.is_a?(Hash)
170:     
171:     validates_each(attr_names, conf) do |record, attr_name, value|
172:       begin
173:         t = Dsadmin::TimeSpec.create(value.to_s)
174:         if((conf[:kind] == "abs") && t.relative?)
175:           record.errors.add(attr_name, "has to be an absolute timespec")
176:           next
177:         elsif((conf[:kind] == "rel") && t.absolute?)
178:           record.errors.add(attr_name, "has to be a relative timespec")
179:           next
180:         end
181:       rescue
182:         record.errors.add(attr_name, conf[:message])
183:       end
184:     end
185:   end

Check if the given attributes contain well-formed URLs

Nonstandard Options:

 * :schemes (array) List of allowed URL schemes ['http', 'https', 'ftp']

[Source]

     # File lib/dsadmin/model_validators.rb, line 150
150:   def self.validates_url(*attr_names)
151:     conf = { :message => "is not a well-formed URL", :on => :save, :allow_nil => false, :schemes => ['http', 'https', 'ftp'] }
152:     conf.update(attr_names.pop) if attr_names.last.is_a?(Hash)
153:     
154:     validates_each(attr_names, conf) do |record, attr_name, value|
155:       tmp = URI::parse(value) rescue record.errors.add(attr_name, conf[:message])
156:       unless(!tmp.kind_of?(URI) || conf[:schemes].include?(tmp.scheme))
157:         record.errors.add(attr_name, "has an invalid URL scheme (allowed are #{conf[:schemes].join(',')})")
158:       end
159:     end        
160:   end

[Validate]