Skip to content

Switch to a MIT-licensed dependency #4

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

Merged
merged 3 commits into from
Feb 27, 2021
Merged
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
12 changes: 12 additions & 0 deletions openapi_schema_validator/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from jsonschema.exceptions import FormatError
from six import binary_type, text_type, integer_types

DATETIME_HAS_RFC3339_VALIDATOR = False
DATETIME_HAS_STRICT_RFC3339 = False
DATETIME_HAS_ISODATE = False
DATETIME_RAISES = ()
Expand All @@ -19,6 +20,14 @@
DATETIME_HAS_ISODATE = True
DATETIME_RAISES += (ValueError, isodate.ISO8601Error)

try:
from rfc3339_validator import validate_rfc3339
except ImportError:
pass
else:
DATETIME_HAS_RFC3339_VALIDATOR = True
DATETIME_RAISES += (ValueError, TypeError)

try:
import strict_rfc3339
except ImportError:
Expand Down Expand Up @@ -64,6 +73,9 @@ def is_datetime(instance):
if not isinstance(instance, (binary_type, text_type)):
return False

if DATETIME_HAS_RFC3339_VALIDATOR:
return validate_rfc3339(instance)

if DATETIME_HAS_STRICT_RFC3339:
return strict_rfc3339.validate_rfc3339(instance)

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
isodate
jsonschema
six
strict_rfc3339
strict-rfc3339
rfc3339-validator
9 changes: 8 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ install_requires =
isodate
jsonschema>=3.0.0
six
strict_rfc3339
tests_require =
mock; python_version<"3.0"
pytest
pytest-flake8
pytest-cov

[options.extras_require]
rfc3339-validator =
rfc3339-validator
strict-rfc3339 =
strict-rfc3339
isodate =
isodate

[options.packages.find]
exclude =
tests
Expand Down
58 changes: 58 additions & 0 deletions tests/integration/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,60 @@ def test_nullable(self, schema_type):
u('1989-01-02T00:00:00Z'),
u('2018-01-02T23:59:59Z'),
])
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_RFC3339_VALIDATOR', False
)
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_STRICT_RFC3339', False
)
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_ISODATE', False
)
def test_string_format_no_datetime_validator(self, value):
schema = {"type": 'string', "format": 'date-time'}
validator = OAS30Validator(
schema, format_checker=oas30_format_checker)

result = validator.validate(value)

assert result is None

@pytest.mark.parametrize('value', [
u('1989-01-02T00:00:00Z'),
u('2018-01-02T23:59:59Z'),
])
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_RFC3339_VALIDATOR', True
)
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_STRICT_RFC3339', False
)
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_ISODATE', False
)
def test_string_format_datetime_rfc3339_validator(self, value):
schema = {"type": 'string', "format": 'date-time'}
validator = OAS30Validator(
schema, format_checker=oas30_format_checker)

result = validator.validate(value)

assert result is None

@pytest.mark.parametrize('value', [
u('1989-01-02T00:00:00Z'),
u('2018-01-02T23:59:59Z'),
])
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_RFC3339_VALIDATOR', False
)
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_STRICT_RFC3339', True
Expand All @@ -56,6 +110,10 @@ def test_string_format_datetime_strict_rfc3339(self, value):
u('1989-01-02T00:00:00Z'),
u('2018-01-02T23:59:59Z'),
])
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_RFC3339_VALIDATOR', False
)
@mock.patch(
'openapi_schema_validator._format.'
'DATETIME_HAS_STRICT_RFC3339', False
Expand Down