Sha256: e049e01ec604eb657214c1573152d40cbafe0e47d37b8a936facf0cca956975a
Contents?: true
Size: 603 Bytes
Versions: 6
Compression:
Stored size: 603 Bytes
Contents
# frozen_string_literal: true # A custom validator to check that the field value is a URL. # # validates :my_url, url: true # class UrlValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) record.errors[attribute] << (options[:message] || "must be a valid URL") unless url_valid?(value) end # a URL may be technically well-formed but may # not actually be valid, so this checks for both. def url_valid?(url) url = URI.parse(url) (url.is_a?(URI::HTTP) || url.is_a?(URI::HTTPS)) && url.host.present? rescue URI::InvalidURIError false end end
Version data entries
6 entries across 6 versions & 1 rubygems