Sha256: f20d7880ae5b2a20e1ea8a6e713c23fcbf1534c7b05875bb4d0379199e7e2875

Contents?: true

Size: 1.09 KB

Versions: 14

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module ActiveModel
  module Validations
    class HostnameValidator < EachValidator
      # Rules taken from http://www.zytrax.com/books/dns/apa/names.html
      def validate_each(record, attribute, value)
        return if value.blank? && options[:allow_blank]
        return if value.nil? && options[:allow_nil]
        return if Validators::Hostname.valid?(value.to_s)

        record.errors.add(
          attribute,
          :invalid_hostname,
          message: options[:message],
          value: value
        )
      end
    end

    module ClassMethods
      # Validates whether or not the specified URL is valid.
      #
      #   class User < ActiveRecord::Base
      #     validates_hostname_format_of :site
      #
      #     # Validates against a list of valid TLD.
      #     validates_hostname_format_of :site, tld: true
      #   end
      #
      def validates_hostname_format_of(*attr_names)
        validates_with HostnameValidator, _merge_attributes(attr_names)
      end

      alias_method :validates_hostname, :validates_hostname_format_of
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
validators-3.4.2 lib/validators/validates_hostname_format_of.rb
validators-3.4.1 lib/validators/validates_hostname_format_of.rb
validators-3.4.0 lib/validators/validates_hostname_format_of.rb
validators-3.3.0 lib/validators/validates_hostname_format_of.rb
validators-3.2.1 lib/validators/validates_hostname_format_of.rb
validators-3.2.0 lib/validators/validates_hostname_format_of.rb
validators-3.1.1 lib/validators/validates_hostname_format_of.rb
validators-3.1.0 lib/validators/validates_hostname_format_of.rb
validators-3.0.5 lib/validators/validates_hostname_format_of.rb
validators-3.0.4 lib/validators/validates_hostname_format_of.rb
validators-3.0.3 lib/validators/validates_hostname_format_of.rb
validators-3.0.2 lib/validators/validates_hostname_format_of.rb
validators-3.0.1 lib/validators/validates_hostname_format_of.rb
validators-3.0.0 lib/validators/validates_hostname_format_of.rb