Sha256: 47a3f48e92a52681809d2b8a345509220d144501ab8bdbceff3548d5228b3650
Contents?: true
Size: 1.21 KB
Versions: 11
Compression:
Stored size: 1.21 KB
Contents
require "valid_email2/address" require "active_model" require "active_model/validations" module ValidEmail2 class EmailValidator < ActiveModel::EachValidator def default_options { regex: true, disposable: false, mx: false, disallow_subaddressing: false } end def validate_each(record, attribute, value) return unless value.present? options = default_options.merge(self.options) address = ValidEmail2::Address.new(value) error(record, attribute) && return unless address.valid? if options[:disallow_subaddressing] error(record, attribute) && return if address.subaddressed? end if options[:disposable] error(record, attribute) && return if address.disposable? end if options[:disposable_with_whitelist] error(record, attribute) && return if address.disposable? && !address.whitelisted? end if options[:blacklist] error(record, attribute) && return if address.blacklisted? end if options[:mx] error(record, attribute) && return unless address.valid_mx? end end def error(record, attribute) record.errors.add(attribute, options[:message] || :invalid) end end end
Version data entries
11 entries across 11 versions & 1 rubygems