cartodb/app/models/carto/helpers/url_validator.rb

11 lines
329 B
Ruby
Raw Normal View History

2020-06-15 10:58:47 +08:00
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
def url_valid?(url)
url = URI.parse(url) rescue false
url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
end
end