diff --git a/app/models/account.rb b/app/models/account.rb index ee10b05..4b853e3 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -5,5 +5,5 @@ class Account < ApplicationRecord validates :username, presence: true validates :password, presence: true validates :home_folder, presence: true - validates :contact_email, presence: true + validates :contact_email, presence: true, email_format: true end diff --git a/app/validators/email_format_validator.rb b/app/validators/email_format_validator.rb new file mode 100644 index 0000000..2336815 --- /dev/null +++ b/app/validators/email_format_validator.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true +class EmailFormatValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + # EMAIL regex test + # (comma seperated) [any word combo] AT [any word combo] DOT [2 or more] + # me@no.yes.x == invalid + # some.thing+two@sub.domain.name == valid + results = value.to_s.split(',').map do |v| + (v.strip =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i) || v.strip.blank? + end + record.errors[attribute] << (options[:message] || "is not formatted properly") if results.include?(false) + end +end diff --git a/test/test_helpers/email_validatable.rb b/test/test_helpers/email_validatable.rb new file mode 100644 index 0000000..475d157 --- /dev/null +++ b/test/test_helpers/email_validatable.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +class EmailValidatable + include ActiveModel::Validations + attr_accessor :email + validates :email, email_format: true +end diff --git a/test/validators/email_format_validator_test.rb b/test/validators/email_format_validator_test.rb new file mode 100644 index 0000000..48accf9 --- /dev/null +++ b/test/validators/email_format_validator_test.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true +require 'test_helper' +class EmailFormatValidatorTest < ActiveSupport::TestCase + test "tld length" do + obj = EmailValidatable.new + obj.email = "me@no.yes.x" + refute obj.valid?, 'allowed single length tld' + obj.email = "me@no.yes.co" + assert obj.valid?, 'did not allow tld length 2' + obj.email = "me@no.yes.com" + assert obj.valid?, 'did not allow tld length 3' + obj.email = "me@no.yes.commets" + assert obj.valid?, 'did not allow tld length > 3' + end + test "can handle comma seperated addresses" do + obj = EmailValidatable.new + obj.email = "me@no.yes, me@yes.no" + assert obj.valid?, 'did not allow multiple address [comma seperated]' + end + test "provides proper error message" do + obj = EmailValidatable.new + obj.email = "this is a bad email address" + obj.valid? + refute obj.errors.messages.empty?, 'needs an error message' + assert_match(/not formatted properly/, obj.errors.messages[:email].join) + end +end