File: ban_disposable_email_validator.rb

package info (click to toggle)
ruby-valid-email 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 288 kB
  • sloc: ruby: 631; makefile: 4
file content (33 lines) | stat: -rw-r--r-- 943 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'active_model'
require 'active_model/validations'
require 'valid_email/validate_email'
class BanDisposableEmailValidator < ActiveModel::EachValidator
  # A list of disposable email domains
  def self.config=(options)
    @@config = options
  end

  # Required to use config outside
  def self.config
    @@config = [] unless defined? @@config

    @@config
  end

  def validate_each(record, attribute, value)
    # Check if part of domain is in disposable_email_services yml list
    if options[:partial]
      r = ValidateEmail.ban_partial_disposable_email?(value)
      record.errors.add attribute, (options[:message] ||
        I18n.t(:invalid, :scope => "valid_email.validations.email")) unless r

      r
    else
      r = ValidateEmail.ban_disposable_email?(value)
      record.errors.add attribute, (options[:message] ||
        I18n.t(:invalid, :scope => "valid_email.validations.email")) unless r

      r
    end
  end
end