Skip to content

Commit 48ffd98

Browse files
committed
datetime validators as an extra options
1 parent bb502ed commit 48ffd98

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

openapi_schema_validator/_format.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from six import binary_type, text_type, integer_types
99

1010
DATETIME_HAS_RFC3339_VALIDATOR = False
11+
DATETIME_HAS_STRICT_RFC3339 = False
1112
DATETIME_HAS_ISODATE = False
1213
DATETIME_RAISES = ()
1314

@@ -27,6 +28,14 @@
2728
DATETIME_HAS_RFC3339_VALIDATOR = True
2829
DATETIME_RAISES += (ValueError, TypeError)
2930

31+
try:
32+
import strict_rfc3339
33+
except ImportError:
34+
pass
35+
else:
36+
DATETIME_HAS_STRICT_RFC3339 = True
37+
DATETIME_RAISES += (ValueError, TypeError)
38+
3039

3140
def is_int32(instance):
3241
return isinstance(instance, integer_types)
@@ -67,6 +76,9 @@ def is_datetime(instance):
6776
if DATETIME_HAS_RFC3339_VALIDATOR:
6877
return validate_rfc3339(instance)
6978

79+
if DATETIME_HAS_STRICT_RFC3339:
80+
return strict_rfc3339.validate_rfc3339(instance)
81+
7082
if DATETIME_HAS_ISODATE:
7183
return isodate.parse_datetime(instance)
7284

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
isodate
22
jsonschema
33
six
4+
strict-rfc3339
45
rfc3339-validator

setup.cfg

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@ install_requires =
2929
isodate
3030
jsonschema>=3.0.0
3131
six
32-
strict_rfc3339
3332
tests_require =
3433
mock; python_version<"3.0"
3534
pytest
3635
pytest-flake8
3736
pytest-cov
3837

38+
[options.extras_require]
39+
rfc3339-validator =
40+
rfc3339-validator
41+
strict-rfc3339 =
42+
strict-rfc3339
43+
isodate =
44+
isodate
45+
3946
[options.packages.find]
4047
exclude =
4148
tests

tests/integration/test_validators.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ def test_nullable(self, schema_type):
3131

3232
assert result is None
3333

34+
@pytest.mark.parametrize('value', [
35+
u('1989-01-02T00:00:00Z'),
36+
u('2018-01-02T23:59:59Z'),
37+
])
38+
@mock.patch(
39+
'openapi_schema_validator._format.'
40+
'DATETIME_HAS_RFC3339_VALIDATOR', False
41+
)
42+
@mock.patch(
43+
'openapi_schema_validator._format.'
44+
'DATETIME_HAS_STRICT_RFC3339', False
45+
)
46+
@mock.patch(
47+
'openapi_schema_validator._format.'
48+
'DATETIME_HAS_ISODATE', False
49+
)
50+
def test_string_format_no_datetime_validator(self, value):
51+
schema = {"type": 'string', "format": 'date-time'}
52+
validator = OAS30Validator(
53+
schema, format_checker=oas30_format_checker)
54+
55+
result = validator.validate(value)
56+
57+
assert result is None
58+
3459
@pytest.mark.parametrize('value', [
3560
u('1989-01-02T00:00:00Z'),
3661
u('2018-01-02T23:59:59Z'),
@@ -39,6 +64,35 @@ def test_nullable(self, schema_type):
3964
'openapi_schema_validator._format.'
4065
'DATETIME_HAS_RFC3339_VALIDATOR', True
4166
)
67+
@mock.patch(
68+
'openapi_schema_validator._format.'
69+
'DATETIME_HAS_STRICT_RFC3339', False
70+
)
71+
@mock.patch(
72+
'openapi_schema_validator._format.'
73+
'DATETIME_HAS_ISODATE', False
74+
)
75+
def test_string_format_datetime_rfc3339_validator(self, value):
76+
schema = {"type": 'string', "format": 'date-time'}
77+
validator = OAS30Validator(
78+
schema, format_checker=oas30_format_checker)
79+
80+
result = validator.validate(value)
81+
82+
assert result is None
83+
84+
@pytest.mark.parametrize('value', [
85+
u('1989-01-02T00:00:00Z'),
86+
u('2018-01-02T23:59:59Z'),
87+
])
88+
@mock.patch(
89+
'openapi_schema_validator._format.'
90+
'DATETIME_HAS_RFC3339_VALIDATOR', False
91+
)
92+
@mock.patch(
93+
'openapi_schema_validator._format.'
94+
'DATETIME_HAS_STRICT_RFC3339', True
95+
)
4296
@mock.patch(
4397
'openapi_schema_validator._format.'
4498
'DATETIME_HAS_ISODATE', False
@@ -60,6 +114,10 @@ def test_string_format_datetime_strict_rfc3339(self, value):
60114
'openapi_schema_validator._format.'
61115
'DATETIME_HAS_RFC3339_VALIDATOR', False
62116
)
117+
@mock.patch(
118+
'openapi_schema_validator._format.'
119+
'DATETIME_HAS_STRICT_RFC3339', False
120+
)
63121
@mock.patch(
64122
'openapi_schema_validator._format.'
65123
'DATETIME_HAS_ISODATE', True

0 commit comments

Comments
 (0)