retrospring/app/validators/valid_url_validator.rb
Georg Gadinger 606629577a make URI.parse part of the validation for the sharing URL
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
2023-02-10 20:48:15 +01:00

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