Class: EmailValidation

Inherits:
Validation show all
Defined in:
lib/json_patterns.rb

Constant Summary collapse

@@email_regexp =

TODO: Replace this with a conjunction of RegexpValidations with customized errors?

Regexp::new("^" +
  # local name
  "(?:" +
  "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  "|" +
  "(?:\"[^\"]+\")" +
  ")" +
  "@" +
  # host name
  "(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
  # domain name
  "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
  # TLD identifier
  "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
  "$", true
)

Constants included from Inspectable

Inspectable::INSPECTING_KEY

Instance Method Summary collapse

Methods inherited from Validation

#as_object_members, #expects_an_object?, memoized_new_from_pattern, new_from_pattern, #shallow_describe, #shallow_match?, #validate_from_root

Methods included from HashInitialized

#initialize

Methods included from DeepEquality

#==

Methods included from Inspectable

#inspect

Instance Method Details

#to_sObject



1022
1023
1024
# File 'lib/json_patterns.rb', line 1022

def to_s
  'email'
end

#validate(path, data) ⇒ Object



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/json_patterns.rb', line 1002

def validate(path, data)
  if data.is_a? String
    if data =~ @@email_regexp
      return []
    else
      return [ValidationUnexpected.new(
        path: path,
        expected: "email",
        found: data.inspect,
      )]
    end
  else
    return [ValidationUnexpected.new(
      path: path,
      expected: 'string',
      found: json_type_name(data),
    )]
  end
end