Class: EmailValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- EmailValidator
- Defined in:
- lib/email_validator.rb
Overview
EmailValidator class
Defined Under Namespace
Classes: Error
Constant Summary collapse
- @@default_options =
rubocop:disable Style/ClassVars
{ :allow_nil => false, :domain => nil, :require_fqdn => nil, :mode => :loose }
Class Method Summary collapse
- .default_options ⇒ Object
- .invalid?(value, options = {}) ⇒ Boolean
-
.regexp(options = {}) ⇒ Object
Refs: tools.ietf.org/html/rfc2822 : 3.2.
- .valid?(value, options = {}) ⇒ Boolean
Instance Method Summary collapse
Class Method Details
.default_options ⇒ Object
22 23 24 |
# File 'lib/email_validator.rb', line 22 def @@default_options end |
.invalid?(value, options = {}) ⇒ Boolean
35 36 37 |
# File 'lib/email_validator.rb', line 35 def invalid?(value, = {}) !valid?(value, ) end |
.regexp(options = {}) ⇒ Object
Refs:
https://tools.ietf.org/html/rfc2822 : 3.2. Lexical Tokens, 3.4.1. Addr-spec specification
https://tools.ietf.org/html/rfc5321 : 4.1.2. Command Argument Syntax
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/email_validator.rb', line 42 def regexp( = {}) = () case [:mode] when :loose loose_regexp() when :rfc rfc_regexp() when :strict [:require_fqdn] = true strict_regexp() else fail EmailValidator::Error, "Validation mode '#{[:mode]}' is not supported by EmailValidator" end end |
.valid?(value, options = {}) ⇒ Boolean
26 27 28 29 30 31 32 33 |
# File 'lib/email_validator.rb', line 26 def valid?(value, = {}) = () return true if value.nil? && [:allow_nil] == true return false if value.nil? # quickly fail if domain is required but doesn't match return false unless [:domain].nil? || value[/^.*@#{regexp_safe_domain()}$/] !!(value =~ regexp()) end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
161 162 163 164 |
# File 'lib/email_validator.rb', line 161 def validate_each(record, attribute, value) = @@default_options.merge(self.) record.errors.add(attribute, [:message] || :invalid) unless self.class.valid?(value, ) end |