Skip to content

Commit 8ac41f1

Browse files
committed
python-openapi/openapi-core#296: Implements OpenAPI 3.1 validator
1 parent 185112b commit 8ac41f1

File tree

10 files changed

+276
-19
lines changed

10 files changed

+276
-19
lines changed

.github/workflows/python-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9]
16+
python-version: [3.6, 3.7, 3.8, 3.9]
1717
fail-fast: false
1818
steps:
1919
- uses: actions/checkout@v2

README.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ openapi-schema-validator
1818
About
1919
#####
2020

21-
Openapi-schema-validator is a Python library that validates schema against the `OpenAPI Schema Specification v3.0 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject>`__ which is an extended subset of the `JSON Schema Specification Wright Draft 00 <http://json-schema.org/>`__.
21+
Openapi-schema-validator is a Python library that validates schema against:
22+
23+
* `OpenAPI Schema Specification v3.0 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject>`__ which is an extended subset of the `JSON Schema Specification Wright Draft 00 <http://json-schema.org/>`__.
24+
* `OpenAPI Schema Specification v3.1 <https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#schemaObject>`__ which is an extended superset of the `JSON Schema Specification Draft 2020-12 <http://json-schema.org/>`__.
2225

2326
Installation
2427
############
@@ -47,7 +50,7 @@ Simple usage
4750
4851
# A sample schema
4952
schema = {
50-
"type" : "object",
53+
"type": "object",
5154
"required": [
5255
"name"
5356
],
@@ -82,9 +85,9 @@ You can also check format for primitive types
8285

8386
.. code-block:: python
8487
85-
from openapi_schema_validator import oas30_format_checker
88+
from openapi_schema_validator import oas31_format_checker
8689
87-
validate({"name": "John", "birth-date": "-12"}, schema, format_checker=oas30_format_checker)
90+
validate({"name": "John", "birth-date": "-12"}, schema, format_checker=oas31_format_checker)
8891
8992
Traceback (most recent call last):
9093
...

openapi_schema_validator/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
# -*- coding: utf-8 -*-
2-
from openapi_schema_validator._format import oas30_format_checker
2+
from openapi_schema_validator._format import oas30_format_checker, oas31_format_checker
33
from openapi_schema_validator.shortcuts import validate
4-
from openapi_schema_validator.validators import OAS30Validator
4+
from openapi_schema_validator.validators import OAS30Validator, OAS31Validator
55

66
__author__ = 'Artur Maciag'
77
__email__ = '[email protected]'
88
__version__ = '0.1.5'
99
__url__ = 'https://github.com/p1c2u/openapi-schema-validator'
1010
__license__ = 'BSD 3-Clause License'
1111

12-
__all__ = ['validate', 'OAS30Validator', 'oas30_format_checker']
12+
__all__ = [
13+
'validate',
14+
'OAS30Validator',
15+
'oas30_format_checker',
16+
'OAS31Validator',
17+
'oas31_format_checker',
18+
]

openapi_schema_validator/_format.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,4 @@ def check(self, instance, format):
145145

146146

147147
oas30_format_checker = OASFormatChecker()
148+
oas31_format_checker = oas30_format_checker

openapi_schema_validator/_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from jsonschema._types import (
22
TypeChecker, is_array, is_bool, is_integer,
3-
is_object, is_number,
3+
is_object, is_number, draft202012_type_checker,
44
)
55
from six import text_type, binary_type
66

@@ -19,3 +19,4 @@ def is_string(checker, instance):
1919
u"object": is_object,
2020
},
2121
)
22+
oas31_type_checker = draft202012_type_checker

openapi_schema_validator/shortcuts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from jsonschema.exceptions import best_match
22

3-
from openapi_schema_validator.validators import OAS30Validator
3+
from openapi_schema_validator.validators import OAS31Validator
44

55

6-
def validate(instance, schema, cls=OAS30Validator, *args, **kwargs):
6+
def validate(instance, schema, cls=OAS31Validator, *args, **kwargs):
77
cls.check_schema(schema)
88
validator = cls(schema, *args, **kwargs)
99
error = best_match(validator.iter_errors(instance))

openapi_schema_validator/validators.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from jsonschema import _legacy_validators, _utils, _validators
2-
from jsonschema.validators import create
2+
from jsonschema.validators import create, Draft202012Validator, extend
33

44
from openapi_schema_validator import _types as oas_types
55
from openapi_schema_validator import _validators as oas_validators
6-
6+
from openapi_schema_validator._types import oas31_type_checker
77

88
BaseOAS30Validator = create(
99
meta_schema=_utils.load_schema("draft4"),
@@ -53,6 +53,21 @@
5353
id_of=lambda schema: schema.get(u"id", ""),
5454
)
5555

56+
BaseOAS31Validator = extend(
57+
Draft202012Validator,
58+
{
59+
# adjusted to OAS
60+
u"description": oas_validators.not_implemented,
61+
u"format": oas_validators.format,
62+
# fixed OAS fields
63+
u"discriminator": oas_validators.not_implemented,
64+
u"xml": oas_validators.not_implemented,
65+
u"externalDocs": oas_validators.not_implemented,
66+
u"example": oas_validators.not_implemented,
67+
},
68+
type_checker=oas31_type_checker,
69+
)
70+
5671

5772
class OAS30Validator(BaseOAS30Validator):
5873

@@ -72,3 +87,7 @@ def iter_errors(self, instance, _schema=None):
7287
})
7388

7489
return super(OAS30Validator, self).iter_errors(instance, _schema)
90+
91+
92+
class OAS31Validator(BaseOAS31Validator):
93+
pass

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
isodate
2-
jsonschema
2+
git+https://github.com/Julian/jsonschema@main#egg=jsonschema[format]
33
six
44
strict-rfc3339
55
rfc3339-validator

setup.cfg

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ classifiers =
99
Intended Audience :: Developers
1010
Topic :: Software Development :: Libraries :: Python Modules
1111
Operating System :: OS Independent
12-
Programming Language :: Python :: 2.7
13-
Programming Language :: Python :: 3.5
1412
Programming Language :: Python :: 3.6
1513
Programming Language :: Python :: 3.7
1614
Programming Language :: Python :: 3.8
@@ -22,15 +20,14 @@ include_package_data = True
2220
packages = find:
2321
zip_safe = False
2422
test_suite = tests
25-
python_requires = >= 2.7, != 3.0.*, != 3.1.*, != 3.2.*, != 3.3.*, != 3.4.*
23+
python_requires = >= 3.6
2624
setup_requires =
2725
setuptools
2826
install_requires =
2927
isodate
3028
jsonschema>=3.0.0
3129
six
3230
tests_require =
33-
mock; python_version<"3.0"
3431
pytest
3532
pytest-flake8
3633
pytest-cov

0 commit comments

Comments
 (0)