Nemo
Nemo

Reputation: 187

Make an exception when validating emails

I currently have an email validation as follows:

validates :email, presence: true, length: { maximum: 255 },
            format: { with: VALID_EMAIL_REGEX, message: "Must be your 
            <domain name> email address." },
            uniqueness: { case_sensitive: false }

This will work for all users of my application except one, an admin who does not have an email belonging to the required domain, and due to circumstances, I cannot create one for him.

How can I go about allowing this admin to register an account with his email, bypassing the regular email validation?

Upvotes: 2

Views: 84

Answers (4)

Hieu Pham
Hieu Pham

Reputation: 6707

Basically, there are 2 things required for your validation as you mentioned:

  • Validate: uniqueness, length, this applies to both admin or normal user
  • Validate: format, but except Admin

So my idea is create a whitelist for Admin:

validates :email, presence: true, length: { maximum: 255 }, uniqueness: { case_sensitive: false }

validates format: { with: VALID_EMAIL_REGEX, message: "Must be your 
            <domain name> email address." }, unless: :in_admin_whitelist?

def in_admin_whitelist?
   email.in? ADMIN_WHITELIST
end

And ADMIN_WHITE_LIST should be loaded from environment variables

config/initializers/admin.rb

ADMIN_WHITELIST = ENV['ADMIN_WHITELIST'].to_s.split(/,/)

So your admin whitelist value format you can set in environment variable will be like:

[email protected],[email protected]

This will be safe and flexible enough!

Upvotes: 0

Pavel Bulanov
Pavel Bulanov

Reputation: 953

Can you try save method with validate: false parameter? See here

save(options={})

The validation process on save can be skipped by passing validate: false. The regular #save method is replaced with this when the validations module is mixed in, which it is by default.

save!(options={})

Attempts to save the record just like #save but will raise a RecordInvalid exception instead of returning false if the record is not valid.

You can also check this answer

UPD. As indicated in comments, this is good for few (manual) creations of admin accounts with turning off all validations.

Upvotes: 1

mlovic
mlovic

Reputation: 864

You can pass a condition to the validation using :if or unless. So something like:

validates :email, presence: true, length: { maximum: 255 },
  format: { with: VALID_EMAIL_REGEX, message: "Must be your 
  <domain name> email address." },
  uniqueness: { case_sensitive: false }, 
  unless: :admin?

You would have to define the method admin? to check whether the user is admin or not.

Upvotes: 0

Sergii K
Sergii K

Reputation: 845

I'll do something like this:

validates :email, presence: true, length: { maximum: 255 },
            format: { with: VALID_EMAIL_REGEX, message: "Must be your 
            <domain name> email address." },
            uniqueness: { case_sensitive: false },
            unless: ->(user) { user.admin? }

Upvotes: 0

Related Questions