From 3d4f9f2c280f9d85722951fa5850ca00a7e80aac Mon Sep 17 00:00:00 2001 From: hellekin Date: Thu, 29 Oct 2020 22:40:35 +0100 Subject: REUSE: Add AGPL-3.0-or-later to app/* --- app/validators/email_validator.rb | 33 +++++++++++++++++++++++++++++++++ app/validators/url_validator.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 app/validators/email_validator.rb create mode 100644 app/validators/url_validator.rb (limited to 'app/validators') diff --git a/app/validators/email_validator.rb b/app/validators/email_validator.rb new file mode 100644 index 0000000..4191907 --- /dev/null +++ b/app/validators/email_validator.rb @@ -0,0 +1,33 @@ +# SPDX-FileCopyrightText: 2020 IN COMMON Collective +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +# frozen_string_literal: true + +# = Email Validator = +# +# In order to be considered valid, value must be: +# +# - a valid Email URI +# - parsable as an URI +# - have a hostname +# - have a valid public TLD +# +class EmailValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + unless valid_email_address?(value) + record.errors[attribute] << (options[:message] || 'is an invalid email address') + end + end + + private + + def valid_email_address?(value) + uri = URI.parse("mailto:#{value}") + uri.is_a?(URI::MailTo) && + uri.to == value && + IANA::TLD.valid?(value.split('@', 2).last.split('.').compact.last) + rescue URI::InvalidURIError, URI::InvalidComponentError + false + end +end 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 +# +# 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 -- cgit v1.2.3