mirror of
https://github.com/Retrospring/retrospring.git
synced 2024-11-20 18:29:52 +01:00
606629577a
the regexp alone and web browsers allows URLs to contain non-ASCII characters, which `URI.parse` does not like -- resulting in the inbox page to suddenly break. also changed the `redirect_to` in the controller to a `render :edit` so that validation errors are shown properly
21 lines
465 B
Ruby
21 lines
465 B
Ruby
# frozen_string_literal: true
|
|
|
|
class ValidUrlValidator < ActiveModel::EachValidator
|
|
URI_REGEXP = URI::DEFAULT_PARSER.make_regexp(%w[http https]).freeze
|
|
|
|
def validate_each(record, attribute, value)
|
|
return if valid?(value)
|
|
|
|
record.errors.add(attribute, :invalid_url)
|
|
end
|
|
|
|
def valid?(value)
|
|
return false unless URI_REGEXP.match?(value)
|
|
|
|
URI.parse(value) # raises URI::InvalidURIError
|
|
|
|
true
|
|
rescue URI::InvalidURIError
|
|
false
|
|
end
|
|
end
|