Skip to content

Commit 935f17e

Browse files
authored
Merge pull request #43 from p1c2u/feature/exitend-readme-about-openapi-31-schema-support
Extend readme for OpenAPI 3.1 support
2 parents 8eef193 + 4a80746 commit 935f17e

File tree

2 files changed

+105
-5
lines changed

2 files changed

+105
-5
lines changed

README.rst

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ Alternatively you can download the code and install from the repository:
4242
Usage
4343
#####
4444

45-
Simple usage
45+
By default, the latest OpenAPI schema syntax is expected.
46+
47+
To validate an OpenAPI v3.1 schema:
4648

4749
.. code-block:: python
4850
@@ -59,28 +61,77 @@ Simple usage
5961
"type": "string"
6062
},
6163
"age": {
62-
"type": "integer",
64+
"type": ["integer", "null"],
6365
"format": "int32",
6466
"minimum": 0,
65-
"nullable": True,
6667
},
6768
"birth-date": {
6869
"type": "string",
6970
"format": "date",
70-
}
71+
},
72+
"address": {
73+
"type": 'array',
74+
"prefixItems": [
75+
{ "type": "number" },
76+
{ "type": "string" },
77+
{ "enum": ["Street", "Avenue", "Boulevard"] },
78+
{ "enum": ["NW", "NE", "SW", "SE"] }
79+
],
80+
"items": False,
81+
}
7182
},
7283
"additionalProperties": False,
7384
}
7485
7586
# If no exception is raised by validate(), the instance is valid.
76-
validate({"name": "John", "age": 23}, schema)
87+
validate({"name": "John", "age": 23, "address": [1600, "Pennsylvania", "Avenue"]}, schema)
7788
7889
validate({"name": "John", "city": "London"}, schema)
7990
8091
Traceback (most recent call last):
8192
...
8293
ValidationError: Additional properties are not allowed ('city' was unexpected)
8394
95+
if you want to disambiguate the expected schema version, import and use ``OAS31Validator``:
96+
97+
.. code-block:: python
98+
99+
from openapi_schema_validator import OAS31Validator
100+
101+
validate({"name": "John", "age": 23}, schema, cls=OAS31Validator)
102+
103+
In order to validate OpenAPI 3.0 schema, import and use ``OAS30Validator`` instead of ``OAS31Validator``.
104+
105+
.. code-block:: python
106+
107+
from openapi_schema_validator import OAS30Validator
108+
109+
# A sample schema
110+
schema = {
111+
"type": "object",
112+
"required": [
113+
"name"
114+
],
115+
"properties": {
116+
"name": {
117+
"type": "string"
118+
},
119+
"age": {
120+
"type": "integer",
121+
"format": "int32",
122+
"minimum": 0,
123+
"nullable": True,
124+
},
125+
"birth-date": {
126+
"type": "string",
127+
"format": "date",
128+
}
129+
},
130+
"additionalProperties": False,
131+
}
132+
133+
validate({"name": "John", "age": 23}, schema, cls=OAS30Validator)
134+
84135
Format check
85136
************
86137

tests/integration/test_validators.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,52 @@ def test_schema_ref(self):
582582

583583
error = "'id' is a required property"
584584
assert error in str(excinfo.value)
585+
586+
@pytest.mark.parametrize('value', [
587+
[1600, "Pennsylvania", "Avenue", "NW"],
588+
[1600, "Pennsylvania", "Avenue"],
589+
])
590+
def test_array_prefixitems(self, value):
591+
schema = {
592+
"type": 'array',
593+
"prefixItems": [
594+
{ "type": "number" },
595+
{ "type": "string" },
596+
{ "enum": ["Street", "Avenue", "Boulevard"] },
597+
{ "enum": ["NW", "NE", "SW", "SE"] }
598+
],
599+
"items": False,
600+
}
601+
validator = OAS31Validator(
602+
schema,
603+
format_checker=oas31_format_checker,
604+
)
605+
606+
result = validator.validate(value)
607+
608+
assert result is None
609+
610+
@pytest.mark.parametrize('value', [
611+
[1600, "Pennsylvania", "Avenue", "NW", "Washington"],
612+
])
613+
def test_array_prefixitems_invalid(self, value):
614+
schema = {
615+
"type": 'array',
616+
"prefixItems": [
617+
{ "type": "number" },
618+
{ "type": "string" },
619+
{ "enum": ["Street", "Avenue", "Boulevard"] },
620+
{ "enum": ["NW", "NE", "SW", "SE"] }
621+
],
622+
"items": False,
623+
}
624+
validator = OAS31Validator(
625+
schema,
626+
format_checker=oas31_format_checker,
627+
)
628+
629+
with pytest.raises(ValidationError) as excinfo:
630+
validator.validate(value)
631+
632+
error = "Expected at most 4 items, but found 5"
633+
assert error in str(excinfo.value)

0 commit comments

Comments
 (0)