diff options
author | hellekin <hellekin@cepheide.org> | 2020-11-12 01:10:28 +0100 |
---|---|---|
committer | hellekin <hellekin@cepheide.org> | 2020-11-12 01:10:28 +0100 |
commit | 8d9387cf64929b6467b6ba52f22ca0aa5ed35782 (patch) | |
tree | 37533e75476680ebad4cd1d47e3e578b879726ba /app/validators/url_validator.rb | |
parent | b6aeac4d9274f17e748efb3715aa08f4b7c361f2 (diff) | |
parent | 757decefafb5d82557a8b7fa9691f94f19c3207e (diff) | |
download | incommon-map-8d9387cf64929b6467b6ba52f22ca0aa5ed35782.tar.gz |
Merged master
Diffstat (limited to 'app/validators/url_validator.rb')
-rw-r--r-- | app/validators/url_validator.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/app/validators/url_validator.rb b/app/validators/url_validator.rb new file mode 100644 index 0000000..5f790b3 --- /dev/null +++ b/app/validators/url_validator.rb @@ -0,0 +1,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 |