blob: 5f790b3e6238ee76bc321881610aa5a96790f5b3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# SPDX-FileCopyrightText: 2020 IN COMMON Collective <collective@incommon.cc>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# frozen_string_literal: true
# = URL Validator =
#
# In order to be considered valid, value must be:
#
# - parsable as an URI
# - use the http or https scheme
# - have a hostname
# - have a valid public TLD
#
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless valid_web_address?(value)
record.errors[attribute] << (options[:message] || 'is an invalid Web URL')
end
end
private
def valid_web_address?(value)
uri = URI.parse(value)
uri.is_a?(URI::HTTP) &&
uri.host.present? &&
IANA::TLD.valid?(uri.host.split('.').compact.last)
rescue URI::InvalidURIError
false
end
end
|