Module: JSONSchemer::Format::Email

Included in:
JSONSchemer::Format
Defined in:
lib/json_schemer/format/email.rb

Constant Summary collapse

UTF8_NON_ASCII =
'[^[:ascii:]]'
A_TEXT =
"([\\w!#$%&'*+\\-/=?\\^`{|}~]|#{UTF8_NON_ASCII})"
Q_TEXT_SMTP =

atext = ALPHA / DIGIT / ; Printable US-ASCII

"!" / "#" /        ;  characters not including
"$" / "%" /        ;  specials.  Used for atoms.
"&" / "'" /
"*" / "+" /
"-" / "/" /
"=" / "?" /
"^" / "_" /
"`" / "{" /
"|" / "}" /
"~"
"([\\x20-\\x21\\x23-\\x5B\\x5D-\\x7E]|#{UTF8_NON_ASCII})"
QUOTED_PAIR_SMTP =

qtextSMTP = %d32-33 / %d35-91 / %d93-126

; i.e., within a quoted string, any
; ASCII graphic or space is permitted
; without blackslash-quoting except
; double-quote and the backslash itself.
'\x5C[\x20-\x7E]'
Q_CONTENT_SMTP =

quoted-pairSMTP = %d92 %d32-126

; i.e., backslash followed by any ASCII
; graphic (including itself) or SPace
"#{Q_TEXT_SMTP}|#{QUOTED_PAIR_SMTP}"
QUOTED_STRING =

Quoted-string = DQUOTE *QcontentSMTP DQUOTE

"\"(#{Q_CONTENT_SMTP})*\""
ATOM =

Atom = 1*atext

"#{A_TEXT}+"
DOT_STRING =

Dot-string = Atom *(“.” Atom)

"#{ATOM}(\\.#{ATOM})*"
LOCAL_PART =
"#{DOT_STRING}|#{QUOTED_STRING}"
ADDRESS_LITERAL =

using ‘valid_id?` to check ip addresses because it’s complicated. # IPv6-address-literal = “IPv6:” IPv6-addr

'\[(IPv6:(?<ipv6>[\h:]+)|(?<ipv4>[\d.]+))\]'
MAILBOX =

using ‘valid_hostname?` to check domain because it’s complicated

"(#{LOCAL_PART})@(#{ADDRESS_LITERAL}|(?<domain>.+))"
EMAIL_REGEX =
/\A#{MAILBOX}\z/

Instance Method Summary collapse

Instance Method Details

#valid_email?(data) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
# File 'lib/json_schemer/format/email.rb', line 44

def valid_email?(data)
  return false unless match = EMAIL_REGEX.match(data)
  if ipv4 = match.named_captures.fetch('ipv4')
    valid_ip?(ipv4, Socket::AF_INET)
  elsif ipv6 = match.named_captures.fetch('ipv6')
    valid_ip?(ipv6, Socket::AF_INET6)
  else
    valid_hostname?(match.named_captures.fetch('domain'))
  end
end