aboutsummaryrefslogtreecommitdiff
path: root/app/validators/email_validator.rb
diff options
context:
space:
mode:
authorhellekin <hellekin@cepheide.org>2020-10-29 22:40:35 +0100
committerhellekin <hellekin@cepheide.org>2020-10-29 22:40:35 +0100
commit3d4f9f2c280f9d85722951fa5850ca00a7e80aac (patch)
tree476e5100981e9507aeb5e41a784705e1c8bb1a0b /app/validators/email_validator.rb
parent1cda1e3525b180778fb0ba864d63698941eb7360 (diff)
downloadincommon-map-3d4f9f2c280f9d85722951fa5850ca00a7e80aac.tar.gz
REUSE: Add AGPL-3.0-or-later to app/*
Diffstat (limited to 'app/validators/email_validator.rb')
-rw-r--r--app/validators/email_validator.rb33
1 files changed, 33 insertions, 0 deletions
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 <collective@incommon.cc>
+#
+# 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