From 9f07296b5e820c3d26d56074ee15c8b8d1c29311 Mon Sep 17 00:00:00 2001 From: "Daniil.Glazko" Date: Wed, 18 Mar 2020 13:38:03 -0400 Subject: [PATCH 1/2] Switch to a MIL-licensed dependency --- .bumpversion.cfg | 2 +- openapi_schema_validator/__init__.py | 2 +- openapi_schema_validator/_format.py | 10 +++++----- requirements.txt | 2 +- tests/integration/test_validators.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 15667a0..53019e2 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.1.1 +current_version = 0.1.2 tag = True tag_name = {new_version} commit = True diff --git a/openapi_schema_validator/__init__.py b/openapi_schema_validator/__init__.py index 57d473e..2cbbac8 100644 --- a/openapi_schema_validator/__init__.py +++ b/openapi_schema_validator/__init__.py @@ -4,7 +4,7 @@ __author__ = 'Artur Maciag' __email__ = 'maciag.artur@gmail.com' -__version__ = '0.1.1' +__version__ = '0.1.2' __url__ = 'https://github.com/p1c2u/openapi-schema-validator' __license__ = 'BSD 3-Clause License' diff --git a/openapi_schema_validator/_format.py b/openapi_schema_validator/_format.py index 35f0653..647e065 100644 --- a/openapi_schema_validator/_format.py +++ b/openapi_schema_validator/_format.py @@ -7,7 +7,7 @@ from jsonschema.exceptions import FormatError from six import binary_type, text_type, integer_types -DATETIME_HAS_STRICT_RFC3339 = False +DATETIME_HAS_RFC3339_VALIDATOR = False DATETIME_HAS_ISODATE = False DATETIME_RAISES = () @@ -20,11 +20,11 @@ DATETIME_RAISES += (ValueError, isodate.ISO8601Error) try: - import strict_rfc3339 + from rfc3339_validator import validate_rfc3339 except ImportError: pass else: - DATETIME_HAS_STRICT_RFC3339 = True + DATETIME_HAS_RFC3339_VALIDATOR = True DATETIME_RAISES += (ValueError, TypeError) @@ -64,8 +64,8 @@ def is_datetime(instance): if not isinstance(instance, (binary_type, text_type)): return False - if DATETIME_HAS_STRICT_RFC3339: - return strict_rfc3339.validate_rfc3339(instance) + if DATETIME_HAS_RFC3339_VALIDATOR: + return validate_rfc3339(instance) if DATETIME_HAS_ISODATE: return isodate.parse_datetime(instance) diff --git a/requirements.txt b/requirements.txt index b66d10e..82cb369 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ isodate jsonschema six -strict_rfc3339 +rfc3339-validator diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py index e977235..02747a3 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/test_validators.py @@ -37,7 +37,7 @@ def test_nullable(self, schema_type): ]) @mock.patch( 'openapi_schema_validator._format.' - 'DATETIME_HAS_STRICT_RFC3339', True + 'DATETIME_HAS_RFC3339_VALIDATOR', True ) @mock.patch( 'openapi_schema_validator._format.' @@ -58,7 +58,7 @@ def test_string_format_datetime_strict_rfc3339(self, value): ]) @mock.patch( 'openapi_schema_validator._format.' - 'DATETIME_HAS_STRICT_RFC3339', False + 'DATETIME_HAS_RFC3339_VALIDATOR', False ) @mock.patch( 'openapi_schema_validator._format.' From 48ffd98fdc7903ed9daf7a52c64cea6a20be410d Mon Sep 17 00:00:00 2001 From: p1c2u Date: Sat, 27 Feb 2021 15:03:18 +0000 Subject: [PATCH 2/2] datetime validators as an extra options --- openapi_schema_validator/_format.py | 12 ++++++ requirements.txt | 1 + setup.cfg | 9 ++++- tests/integration/test_validators.py | 58 ++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/openapi_schema_validator/_format.py b/openapi_schema_validator/_format.py index 5668395..0fb9e97 100644 --- a/openapi_schema_validator/_format.py +++ b/openapi_schema_validator/_format.py @@ -8,6 +8,7 @@ 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 = () @@ -27,6 +28,14 @@ DATETIME_HAS_RFC3339_VALIDATOR = True DATETIME_RAISES += (ValueError, TypeError) +try: + import strict_rfc3339 +except ImportError: + pass +else: + DATETIME_HAS_STRICT_RFC3339 = True + DATETIME_RAISES += (ValueError, TypeError) + def is_int32(instance): return isinstance(instance, integer_types) @@ -67,6 +76,9 @@ def is_datetime(instance): if DATETIME_HAS_RFC3339_VALIDATOR: return validate_rfc3339(instance) + if DATETIME_HAS_STRICT_RFC3339: + return strict_rfc3339.validate_rfc3339(instance) + if DATETIME_HAS_ISODATE: return isodate.parse_datetime(instance) diff --git a/requirements.txt b/requirements.txt index 82cb369..6643606 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ isodate jsonschema six +strict-rfc3339 rfc3339-validator diff --git a/setup.cfg b/setup.cfg index b6efdbe..0755699 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py index 0e04396..6ab8f8c 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/test_validators.py @@ -31,6 +31,31 @@ def test_nullable(self, schema_type): 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', 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'), @@ -39,6 +64,35 @@ def test_nullable(self, schema_type): '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 + ) @mock.patch( 'openapi_schema_validator._format.' 'DATETIME_HAS_ISODATE', False @@ -60,6 +114,10 @@ def test_string_format_datetime_strict_rfc3339(self, value): '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', True