forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_address.py
More file actions
57 lines (42 loc) · 2.06 KB
/
Copy pathemail_address.py
File metadata and controls
57 lines (42 loc) · 2.06 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from re import compile
from graphene.types import Scalar
from graphql.language import ast
from graphql import GraphQLError
from functions.error_messages import *
EMAIL_ADDRESS_REGEX = r"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[
\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[
a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][
0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[
\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"""
# GC_EMAIL_ADDRESS_REGEX = r"""(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[
# \x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[
# a-z0-9])?\.)+(gc|canada)\.(ca))"""
EMAIL_ADDRESS_REGEX = compile(EMAIL_ADDRESS_REGEX)
class EmailAddress(Scalar):
"""
A field whose value conforms to the standard internet email address format as specified in RFC822:
https://www.w3.org/Protocols/rfc822/.
"""
@staticmethod
def serialize(value):
if not isinstance(value, str):
raise GraphQLError(scalar_error_type("String", value))
if not EMAIL_ADDRESS_REGEX.search(value):
raise GraphQLError(scalar_error_type("email address", value))
return value
@staticmethod
def parse_value(value):
if not isinstance(value, str):
raise GraphQLError(scalar_error_type("String", value))
if not EMAIL_ADDRESS_REGEX.search(value):
raise GraphQLError(scalar_error_type("email address", value))
return value
@staticmethod
def parse_literal(node):
if not isinstance(node, ast.StringValue):
raise GraphQLError(
scalar_error_only_types("strings", "email address", str(type(node)))
)
if not EMAIL_ADDRESS_REGEX.search(node.value):
raise GraphQLError(scalar_error_type("email address", node.value))
return node.value