Skip to content

Commit 3d7bacc

Browse files
committed
Add OpenAPI 3.2 support
OAS 3.2 uses the same JSON Schema dialect (Draft 2020-12) as 3.1, so OAS32Validator and oas32_format_checker are aliases.
1 parent 625bebd commit 3d7bacc

File tree

8 files changed

+39
-1
lines changed

8 files changed

+39
-1
lines changed

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Use this as the default operating playbook when making changes.
1717
## Source Layout
1818

1919
- `openapi_schema_validator/__init__.py`: public exports + package metadata.
20-
- `openapi_schema_validator/validators.py`: validator class setup for OAS 3.0/3.1.
20+
- `openapi_schema_validator/validators.py`: validator class setup for OAS 3.0/3.1/3.2.
2121
- `openapi_schema_validator/_keywords.py`: custom keyword handlers and ValidationError generation.
2222
- `openapi_schema_validator/_format.py`: format checker functions and registrations.
2323
- `openapi_schema_validator/_types.py`: custom type checker setup.

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Openapi-schema-validator is a Python library that validates schema against:
2222

2323
* `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/>`__.
2424
* `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/>`__.
25+
* `OpenAPI Schema Specification v3.2 <https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#schemaObject>`__ which uses the same JSON Schema dialect as v3.1.
2526

2627

2728
Documentation

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Openapi-schema-validator is a Python library that validates schema against:
1414

1515
* `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/>`__.
1616
* `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/>`__.
17+
* `OpenAPI Schema Specification v3.2 <https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#schemaObject>`__ which uses the same JSON Schema dialect as v3.1.
1718

1819
Installation
1920
------------

docs/validation.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ if you want to disambiguate the expected schema version, import and use ``OAS31V
6767
6868
validate({"name": "John", "age": 23}, schema, cls=OAS31Validator)
6969
70+
For OpenAPI 3.2, use ``OAS32Validator`` (an alias for ``OAS31Validator``, since 3.2 uses the same JSON Schema dialect).
71+
7072
In order to validate OpenAPI 3.0 schema, import and use ``OAS30Validator`` instead of ``OAS31Validator``.
7173

7274
.. code-block:: python

openapi_schema_validator/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from openapi_schema_validator._format import oas30_format_checker
22
from openapi_schema_validator._format import oas30_strict_format_checker
33
from openapi_schema_validator._format import oas31_format_checker
4+
from openapi_schema_validator._format import oas32_format_checker
45
from openapi_schema_validator.shortcuts import validate
56
from openapi_schema_validator.validators import OAS30ReadValidator
67
from openapi_schema_validator.validators import OAS30StrictValidator
78
from openapi_schema_validator.validators import OAS30Validator
89
from openapi_schema_validator.validators import OAS30WriteValidator
910
from openapi_schema_validator.validators import OAS31Validator
11+
from openapi_schema_validator.validators import OAS32Validator
1012

1113
__author__ = "Artur Maciag"
1214
__email__ = "maciag.artur@gmail.com"
@@ -24,4 +26,6 @@
2426
"oas30_strict_format_checker",
2527
"OAS31Validator",
2628
"oas31_format_checker",
29+
"OAS32Validator",
30+
"oas32_format_checker",
2731
]

openapi_schema_validator/_format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,5 @@ def is_password(instance: object) -> bool:
105105
oas31_format_checker.checks("float")(is_float)
106106
oas31_format_checker.checks("double")(is_double)
107107
oas31_format_checker.checks("password")(is_password)
108+
109+
oas32_format_checker = oas31_format_checker

openapi_schema_validator/validators.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@ def _oas30_id_of(schema: Any) -> str:
121121
type_checker=oas31_type_checker,
122122
format_checker=oas_format.oas31_format_checker,
123123
)
124+
125+
# OAS 3.2 uses the same JSON Schema dialect (Draft 2020-12) as OAS 3.1
126+
OAS32Validator = OAS31Validator

tests/integration/test_validators.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
from openapi_schema_validator import OAS30Validator
2020
from openapi_schema_validator import OAS30WriteValidator
2121
from openapi_schema_validator import OAS31Validator
22+
from openapi_schema_validator import OAS32Validator
2223
from openapi_schema_validator import oas30_format_checker
2324
from openapi_schema_validator import oas30_strict_format_checker
2425
from openapi_schema_validator import oas31_format_checker
26+
from openapi_schema_validator import oas32_format_checker
2527

2628

2729
class TestOAS30ValidatorFormatChecker:
@@ -1003,6 +1005,29 @@ def test_array_prefixitems_invalid(self, validator_class, value):
10031005
assert any(error in str(excinfo.value) for error in errors)
10041006

10051007

1008+
class TestOAS32ValidatorAliases:
1009+
"""OAS 3.2 uses the same JSON Schema dialect as 3.1, so the validators are aliases."""
1010+
1011+
def test_validator_is_oas31(self):
1012+
assert OAS32Validator is OAS31Validator
1013+
1014+
def test_format_checker_is_oas31(self):
1015+
assert oas32_format_checker is oas31_format_checker
1016+
1017+
def test_basic_validation(self):
1018+
schema = {
1019+
"type": "object",
1020+
"required": ["name"],
1021+
"properties": {"name": {"type": "string"}},
1022+
}
1023+
validator = OAS32Validator(schema, format_checker=oas32_format_checker)
1024+
1025+
assert validator.validate({"name": "test"}) is None
1026+
1027+
with pytest.raises(ValidationError):
1028+
validator.validate({"not_name": "test"})
1029+
1030+
10061031
class TestOAS30StrictValidator:
10071032
"""
10081033
Tests for OAS30StrictValidator which follows OAS spec strictly:

0 commit comments

Comments
 (0)