~cytrogen/masto-fe

ref: a5b62e56d02cf7bbe43f30396475c41ff0628513 masto-fe/app/validators/url_validator.rb -rw-r--r-- 421 bytes
a5b62e56 — Daniel M Brasil Migrate to request specs in `/api/v1/apps/verify_credentials` (#25404) 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# frozen_string_literal: true

class URLValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors.add(attribute, :invalid) unless compliant?(value)
  end

  private

  def compliant?(url)
    parsed_url = Addressable::URI.parse(url)
    parsed_url && %w(http https).include?(parsed_url.scheme) && parsed_url.host
  rescue Addressable::URI::InvalidURIError
    false
  end
end