Skip to content

Commit

Permalink
Implement new type email that validates data against regex based on R…
Browse files Browse the repository at this point in the history
…FC 5322 Official Standard taken from https://emailregex.com/
  • Loading branch information
Grokzen committed Oct 19, 2019
1 parent 5174499 commit a39c512
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Next release
- Dropped support for pyyaml
- Added new cli argument "--encoding ENCODING" that specifies what encoding to open data and schema files with
- Enum error strings now output all possible values for easier debugging
- Implement new type email that uses a relative simple regex to validate email addresses according to RFC 5322 Official Standard


1.7.0 (October 3, 2018)
Expand Down
4 changes: 4 additions & 0 deletions docs/validation-rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ The following types are available:
- **timestamp**
- Validates for basic timestamp formats

- **email**
- Validates data is a valid Email address based on RFC 5322 Official Standard
^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$


Example

Expand Down
11 changes: 10 additions & 1 deletion pykwalify/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# python stdlib
import datetime
import re
from pykwalify.compat import basestring, bytes

DEFAULT_TYPE = "str"
Expand Down Expand Up @@ -33,7 +34,8 @@ class text(object):
"text": text,
"any": object,
"enum": str,
"none": None
"none": None,
"email": str,
}


Expand Down Expand Up @@ -144,6 +146,12 @@ def is_date(obj):
return isinstance(obj, basestring) or isinstance(obj, datetime.date)


def is_email(obj):
"""
"""
return re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", obj)


tt = {
"str": is_string,
"int": is_int,
Expand All @@ -157,4 +165,5 @@ def is_date(obj):
"timestamp": is_timestamp,
"scalar": is_scalar,
"date": is_date,
"email": is_email,
}
10 changes: 10 additions & 0 deletions tests/files/fail/test_type_email.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
name: email-1
desc: basic email type validation. Fails if not conforms with RFC 5322 Official Standard
#
schema:
type: email
data:
"foobar|gmail.com"
errors:
- "Value 'foobar|gmail.com' is not of type 'email'. Path: ''"
8 changes: 8 additions & 0 deletions tests/files/success/test_type_email.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
name: email-1
desc: basic email type validation. Fails if not conforms with RFC 5322 Official Standard
#
schema:
type: email
data:
"foobar@gmail.com"
4 changes: 4 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ def test_core_files(self):
"test_type_text.yaml",
# All tests for TYPE: timestamp
"test_type_timestamp.yaml",
# All tests for TYPE: email
"test_type_email.yaml",
]

_fail_tests = [
Expand Down Expand Up @@ -518,6 +520,8 @@ def test_core_files(self):
("test_type_text.yaml", SchemaError),
# All tests for TYPE: timestamp
("test_type_timestamp.yaml", SchemaError),
# All tests for TYPE: email
("test_type_email.yaml", SchemaError),
]

# Add override magic to make it easier to test a specific file
Expand Down

0 comments on commit a39c512

Please sign in to comment.