validate email format
This commit is contained in:
committed by
Pete Matsyburka
parent
178ee47093
commit
ab810be250
@@ -2,6 +2,8 @@
|
||||
|
||||
module Params
|
||||
class BaseValidator
|
||||
EMAIL_REGEXP = /\A[a-z0-9][\.']?(?:(?:[a-z0-9_-]+[\.\+'])*[a-z0-9_-]+)*@(?:[a-z0-9]+[\.-])*[a-z0-9]+\.[a-z]{2,}\z/i
|
||||
|
||||
InvalidParameterError = Class.new(StandardError)
|
||||
|
||||
def self.call(...)
|
||||
@@ -65,6 +67,14 @@ module Params
|
||||
raise_error(message || "#{key} must follow the #{regexp.source} format")
|
||||
end
|
||||
|
||||
def email_format(params, key, message: nil)
|
||||
return if params.blank?
|
||||
return if params[key].blank?
|
||||
return if params[key].to_s.strip.split(/\s*[;,]\s*/).all? { |email| email.match?(EMAIL_REGEXP) }
|
||||
|
||||
raise_error(message || "#{key} must follow the email format")
|
||||
end
|
||||
|
||||
def unique_value(params, key, message: nil)
|
||||
return unless params.is_a?(Array)
|
||||
return if params.none?
|
||||
|
||||
@@ -22,6 +22,7 @@ module Params
|
||||
required(params, %i[emails email])
|
||||
|
||||
type(params, :emails, String)
|
||||
email_format(params, :emails, message: 'emails are invalid')
|
||||
boolean(params, :send_email)
|
||||
type(params, :message, Hash)
|
||||
|
||||
@@ -43,8 +44,8 @@ module Params
|
||||
type(params, :completed_redirect_url, String)
|
||||
type(params, :bcc_completed, String)
|
||||
type(params, :reply_to, String)
|
||||
format(params, :bcc_completed, /@/, message: 'bcc_completed email is invalid')
|
||||
format(params, :reply_to, /@/, message: 'reply_to email is invalid')
|
||||
email_format(params, :bcc_completed, message: 'bcc_completed email is invalid')
|
||||
email_format(params, :reply_to, message: 'reply_to email is invalid')
|
||||
type(params, :message, Hash)
|
||||
type(params, :submitters, Array)
|
||||
|
||||
@@ -75,8 +76,8 @@ module Params
|
||||
type(submitter_params, :name, String)
|
||||
type(submitter_params, :reply_to, String)
|
||||
type(submitter_params, :email, String)
|
||||
format(submitter_params, :email, /@/, message: 'email is invalid')
|
||||
format(submitter_params, :reply_to, /@/, message: 'reply_to email is invalid')
|
||||
email_format(submitter_params, :email, message: 'email is invalid')
|
||||
email_format(submitter_params, :reply_to, message: 'reply_to email is invalid')
|
||||
type(submitter_params, :phone, String)
|
||||
format(submitter_params, :phone, /\A\+\d+\z/,
|
||||
message: 'phone should start with +<country code> and contain only digits')
|
||||
@@ -106,7 +107,7 @@ module Params
|
||||
type(params, :order, String)
|
||||
type(params, :completed_redirect_url, String)
|
||||
type(params, :bcc_completed, String)
|
||||
format(params, :bcc_completed, /@/, message: 'bcc_completed email is invalid')
|
||||
email_format(params, :bcc_completed, message: 'bcc_completed email is invalid')
|
||||
type(params, :message, Hash)
|
||||
|
||||
in_path(params, :message) do |message_params|
|
||||
|
||||
Reference in New Issue
Block a user