Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate built-in String formats #32

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ readme = "README.md"

[tool.poetry.dependencies]
python = "^3.8"
rfc3987 = { version = "^1.3.8", optional = true }


[tool.poetry.dev-dependencies]
pytest = "^7.2.0"
Expand All @@ -17,6 +19,10 @@ vulture = "^2.6"
flake8 = { version = "^6.0.0", python = ">=3.8.1" }
pytest-cov = "^4.0.0"


[tool.poetry.extras]
strings = ["rfc3987"]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
43 changes: 43 additions & 0 deletions schemamodels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import sys
from dataclasses import make_dataclass, field, fields as fs
from re import sub
import re
import importlib
import ipaddress
from operator import gt, ge, lt, le, mod, xor, not_
from typing import Callable
from urllib.parse import urlparse

from functools import partial, reduce

from schemamodels import exceptions as e, bases

from datetime import datetime


JSON_TYPE_MAP = {
'string': lambda d: isinstance(d, str),
Expand All @@ -21,13 +26,46 @@

PORCELINE_KEYWORDS = ['value', 'default', 'anyOf', 'allOf', 'oneOf', 'not']

# Source: https://uibakery.io/regex-library/email-regex-python
EMAIL_REGEX = re.compile(r"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$")

HOSTNAME_REGEX = re.compile(r'^(?P<scheme>(?:\w+(?:\+\w+)?:)//)?//(?P<reminder>.*)')


def hostname_process(e):
results = HOSTNAME_REGEX.match(e)
if results:
if all(results.group('scheme'), results.group('remainder')):
return urlparse(e).hostname is not None
return False


def ifraises(func, value, exception_class=ValueError):
try:
func(value)
except exception_class:
return False
return True


STRING_FORMATS = {
'email': lambda e: EMAIL_REGEX.match(e) is not None,
'date-time': lambda e: sys.version_info >= (3, 11) and datetime.fromisoformat(e),
'date': lambda e: sys.version_info >= (3, 11) and datetime.fromisoformat(e),
'time': lambda e: sys.version_info >= (3, 11) and datetime.fromisoformat(e),
'ipv4': lambda e: ifraises(ipaddress.IPv4Address, e, exception_class=ipaddress.AddressValueError),
'ipv6': lambda e: ifraises(ipaddress.IPv6Address, e, exception_class=ipaddress.AddressValueError),
'hostname': lambda e: hostname_process(e),
}

COMPARISONS = {
'type': lambda d: JSON_TYPE_MAP[d],
'anyOf': lambda d: partial(lambda struct: generate_functors(struct), d),
'allOf': lambda d: partial(lambda struct: generate_functors(struct), d),
'oneOf': lambda d: partial(lambda struct: generate_functors(struct), d),
'not': lambda d: not_(d),
'string': lambda d: isinstance(d, str),
'format': lambda d: partial(lambda bound, v: STRING_FORMATS[bound](v), d),
'integer': lambda d: isinstance(d, int),
'number': lambda d: isinstance(d, (float, int)),
'null': lambda d: d is None,
Expand Down Expand Up @@ -107,6 +145,11 @@ def constraints(dataclass_instance):
raise e.SubSchemaFailureViolation("at least one subschema failed")
if len([n for n in nodes if not n.get('type', True)]) > 0:
raise e.ValueTypeViolation("incorrect type assigned to JSON property")
if len([n for n in nodes if not n.get('format', True)]) > 0:
if sys.version_info >= (3, 11):
raise e.StringFormatViolation("violates string format constraint")
else:
raise e.StringFormatViolation("Python versions < 3.11 does not support native ISO8601 datetime parsing")
if len([n for n in nodes if not n.get('maximum', True)]) > 0:
raise e.RangeConstraintViolation("violates range contraint")
if len([n for n in nodes if not n.get('exclusiveMaximum', True)]) > 0:
Expand Down
3 changes: 3 additions & 0 deletions schemamodels/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ class ValueTypeViolation(SchemaViolation): pass


class SubSchemaFailureViolation(SchemaViolation): pass


class StringFormatViolation(SchemaViolation): pass
113 changes: 113 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from jsonschema import validators
import json
import importlib
Expand Down Expand Up @@ -523,3 +524,115 @@ def test_functor_generator():
assert next(iter(fn.values()))(1)
assert any(list(next(iter(fn.values()))(1.0)))
assert all(list(next(iter(fn.values()))("e")))


@pytest.mark.string
@pytest.mark.format
def test_string_email_format_support():
schemadoc = '''
{
"title": "email-format",
"description": "Blue Blah",
"type": "object",
"properties": {
"user_email": {
"type": "string",
"format": "email"
}
}
}
'''
stringformat = json.loads(schemadoc)
sm = SchemaModelFactory()
sm.register(stringformat)

from schemamodels.dynamic import EmailFormat

fs = EmailFormat(user_email="[email protected]")
with pytest.raises(exceptions.StringFormatViolation):
fs = EmailFormat(user_email="abcdefgh")


@pytest.mark.string
@pytest.mark.format
def test_string_datetimes_format_support():
schemadoc = '''
{
"title": "date-format",
"description": "Blue Blah",
"type": "object",
"properties": {
"event_time": {
"type": "string",
"format": "date-time"
}
}
}
'''
stringformat = json.loads(schemadoc)
sm = SchemaModelFactory()
sm.register(stringformat)

from schemamodels.dynamic import DateFormat

if sys.version_info >= (3, 11):
fs = DateFormat(event_time="2018-11-13T20:20:39+00:00")
with pytest.raises(exceptions.StringFormatViolation):
fs = DateFormat(event_time="abcdefgh")
else:
with pytest.raises(exceptions.StringFormatViolation):
fs = DateFormat(event_time="2018-11-13T20:20:39+00:00")


@pytest.mark.string
@pytest.mark.format
def test_string_ipaddress_format_support():
schemadoc = '''
{
"title": "internet-format",
"description": "Blue Blah",
"type": "object",
"properties": {
"event_location": {
"type": "string",
"format": "ipv4"
}
}
}
'''
stringformat = json.loads(schemadoc)
sm = SchemaModelFactory()
sm.register(stringformat)

from schemamodels.dynamic import InternetFormat

fs = InternetFormat(event_location='127.0.0.1')
with pytest.raises(exceptions.StringFormatViolation):
fs = InternetFormat(event_location='welpwelp')


@pytest.mark.string
@pytest.mark.format
def test_string_hostname_format_support():
schemadoc = '''
{
"title": "internet-format",
"description": "Blue Blah",
"type": "object",
"properties": {
"webhost": {
"type": "string",
"format": "hostname"
}
}
}
'''
stringformat = json.loads(schemadoc)
sm = SchemaModelFactory()
sm.register(stringformat)

from schemamodels.dynamic import InternetFormat

fs = InternetFormat(webhost='127.0.0.1')
with pytest.raises(exceptions.StringFormatViolation):
fs = InternetFormat(webhost='qwdqwd//qwdqwdwd.com')