Class: PAK::ValidatesHostname::DomainnameValidator
- Inherits:
-
HostnameValidator
- Object
- ActiveModel::EachValidator
- HostnameValidator
- PAK::ValidatesHostname::DomainnameValidator
- Defined in:
- lib/validates_hostname.rb
Instance Method Summary collapse
- #add_error(record, attr_name, message, *interpolators) ⇒ Object
-
#initialize(options) ⇒ DomainnameValidator
constructor
A new instance of DomainnameValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ DomainnameValidator
Returns a new instance of DomainnameValidator.
246 247 248 249 250 251 252 |
# File 'lib/validates_hostname.rb', line 246 def initialize() opts = { :require_valid_tld => true, :allow_numeric_hostname => true }.merge() super(opts) end |
Instance Method Details
#add_error(record, attr_name, message, *interpolators) ⇒ Object
272 273 274 275 276 277 278 |
# File 'lib/validates_hostname.rb', line 272 def add_error(record, attr_name, , *interpolators) args = { :default => [DEFAULT_ERROR_MSG[], [:message]], :scope => [:errors, :messages] }.merge(interpolators.last.is_a?(Hash) ? interpolators.pop : {}) record.errors.add(attr_name, I18n.t( , args )) end |
#validate_each(record, attribute, value) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/validates_hostname.rb', line 254 def validate_each(record, attribute, value) super if value.is_a?(String) labels = value.split '.' labels.each do |label| # CHECK 1: if there is only one label it cannot be numeric even # though numeric hostnames are allowed if [:allow_numeric_hostname] == true is_numeric_only = labels[0] =~ /\A\d+\z/ if is_numeric_only and labels.size == 1 add_error(record, attribute, :single_numeric_hostname_label) end end end end end |