Sha256: 1e9c04d8a2576398e2f609dc9978d258a200e6b5a05ee5ef5382c5b94cd1cbc7
Contents?: true
Size: 1.18 KB
Versions: 1
Compression:
Stored size: 1.18 KB
Contents
require 'uri' # Allows to check if the value of a specific attribute is a valid URL. # # @example Validate that the user's blog URL is valid. # class User << ActiveRecord::Base # attr_accessor :blog, :name # validates :blog, url: true # end class UrlValidator < ActiveModel::EachValidator # Checks if an attribute value is a valid URL. # # @param [Object] record object to validate # @param [String] attribute name of the object attribute to validate # @param [Object] value attribute value def validate_each(record, attribute, value) uri = URI.parse(value) raise URI::InvalidURIError unless uri.kind_of?(URI::HTTP) domains = Array.wrap(options[:domain]) host = uri.host.downcase in_valid_domain = domains.empty? || domains.any? { |domain| host.end_with?(".#{domain.downcase}") } raise URI::InvalidURIError unless in_valid_domain must_be_domain_root = options[:root] || false if must_be_domain_root && (!['/', ''].include?(uri.path) || uri.query.present? || uri.fragment.present?) raise URI::InvalidURIError end rescue URI::InvalidURIError record.errors[attribute] << (options[:message] || I18n.t('errors.messages.url')) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
missing_validators-0.5.2 | lib/missing_validators/validators/url_validator.rb |