Add registrar email validation

This commit is contained in:
Alex Sherman 2020-06-03 17:25:33 +05:00
parent ba4e256662
commit e56162f37d
3 changed files with 21 additions and 12 deletions

View file

@ -18,7 +18,6 @@ gem 'ransack', '~> 2.3'
gem 'truemail', '~> 1.7' # validates email by regexp, mail server existence and address existence
gem 'validates_email_format_of', '1.6.3' # validates email against RFC 2822 and RFC 3696
# 0.7.3 is the latest for Rails 4.2, however, it is absent on Rubygems server
# https://github.com/huacnlee/rails-settings-cached/issues/165
gem 'rails-settings-cached', '0.7.2'

View file

@ -29,14 +29,11 @@ class Registrar < ApplicationRecord
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 },
allow_nil: true
validate :forbid_special_code
attribute :vat_rate, ::Type::VATRate.new
after_initialize :set_defaults
validates :email, email_format: { message: :invalid },
allow_blank: true, if: proc { |c| c.will_save_change_to_email? }
validates :billing_email, email_format: { message: :invalid }, allow_blank: true
validate :correct_email_format, if: proc { |c| c.will_save_change_to_email? }
validate :correct_billing_email_format
alias_attribute :contact_email, :email
@ -196,4 +193,18 @@ class Registrar < ApplicationRecord
def vat_liable_in_foreign_country?
!vat_liable_locally?
end
def verify_email_mx_smtp(field:, email:)
errors.add(field, :invalid) unless email.blank? || Truemail.valid?(email)
end
def correct_email_format
verify_email_mx_smtp(field: :email, email: email)
end
def correct_billing_email_format
return if self[:billing_email].blank?
verify_email_mx_smtp(field: :billing_email, email: billing_email)
end
end

View file

@ -28,7 +28,11 @@ Truemail.configure do |config|
# Optional parameter. You can predefine default validation type for
# Truemail.validate('email@email.com') call without with-parameter
# Available validation types: :regex, :mx, :smtp
config.default_validation_type = :smtp
if Rails.env.production?
config.default_validation_type = :smtp
else
config.default_validation_type = :regex
end
# Optional parameter. You can predefine which type of validation will be used for domains.
# Also you can skip validation by domain. Available validation types: :regex, :mx, :smtp
@ -43,11 +47,6 @@ Truemail.configure do |config|
# It is equal to empty array by default.
# config.whitelisted_domains = []
unless Rails.env.production?
config.whitelisted_domains = %w[bestnames.test goodnames.test example.com inbox.test mail.test
outlook.test invalid.test email.test]
end
# Optional parameter. With this option Truemail will validate email which contains whitelisted
# domain only, i.e. if domain whitelisted, validation will passed to Regex, MX or SMTP validators.
# Validation of email which not contains whitelisted domain always will return false.