mirror of
https://github.com/internetee/registry.git
synced 2025-06-12 07:34:45 +02:00
Add registrar email validation
This commit is contained in:
parent
ba4e256662
commit
e56162f37d
3 changed files with 21 additions and 12 deletions
1
Gemfile
1
Gemfile
|
@ -18,7 +18,6 @@ gem 'ransack', '~> 2.3'
|
||||||
gem 'truemail', '~> 1.7' # validates email by regexp, mail server existence and address existence
|
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
|
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
|
# 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
|
# https://github.com/huacnlee/rails-settings-cached/issues/165
|
||||||
gem 'rails-settings-cached', '0.7.2'
|
gem 'rails-settings-cached', '0.7.2'
|
||||||
|
|
|
@ -29,14 +29,11 @@ class Registrar < ApplicationRecord
|
||||||
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 },
|
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 },
|
||||||
allow_nil: true
|
allow_nil: true
|
||||||
|
|
||||||
validate :forbid_special_code
|
|
||||||
|
|
||||||
attribute :vat_rate, ::Type::VATRate.new
|
attribute :vat_rate, ::Type::VATRate.new
|
||||||
after_initialize :set_defaults
|
after_initialize :set_defaults
|
||||||
|
|
||||||
validates :email, email_format: { message: :invalid },
|
validate :correct_email_format, if: proc { |c| c.will_save_change_to_email? }
|
||||||
allow_blank: true, if: proc { |c| c.will_save_change_to_email? }
|
validate :correct_billing_email_format
|
||||||
validates :billing_email, email_format: { message: :invalid }, allow_blank: true
|
|
||||||
|
|
||||||
alias_attribute :contact_email, :email
|
alias_attribute :contact_email, :email
|
||||||
|
|
||||||
|
@ -196,4 +193,18 @@ class Registrar < ApplicationRecord
|
||||||
def vat_liable_in_foreign_country?
|
def vat_liable_in_foreign_country?
|
||||||
!vat_liable_locally?
|
!vat_liable_locally?
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -28,7 +28,11 @@ Truemail.configure do |config|
|
||||||
# Optional parameter. You can predefine default validation type for
|
# Optional parameter. You can predefine default validation type for
|
||||||
# Truemail.validate('email@email.com') call without with-parameter
|
# Truemail.validate('email@email.com') call without with-parameter
|
||||||
# Available validation types: :regex, :mx, :smtp
|
# 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.
|
# 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
|
# 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.
|
# It is equal to empty array by default.
|
||||||
# config.whitelisted_domains = []
|
# 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
|
# 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.
|
# 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.
|
# Validation of email which not contains whitelisted domain always will return false.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue