Skip to content

Commit 628b746

Browse files
committed
Validate signature documented
1 parent 51e7d21 commit 628b746

File tree

3 files changed

+78
-28
lines changed

3 files changed

+78
-28
lines changed

README.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ Alternatively you can download the code and install from the repository:
5050
Usage
5151
#####
5252

53-
To validate an OpenAPI v3.1 schema:
53+
``validate`` call signature is:
54+
55+
.. code-block:: python
56+
57+
validate(instance, schema, cls=OAS32Validator, **kwargs)
58+
59+
The first argument is always the value you want to validate.
60+
The second argument is always the OpenAPI schema object.
61+
The ``cls`` keyword argument is optional and defaults to ``OAS32Validator``.
62+
Use ``cls`` when you need a specific validator version/behavior.
63+
64+
To validate an OpenAPI schema:
5465

5566
.. code-block:: python
5667
@@ -100,6 +111,9 @@ To validate an OpenAPI v3.1 schema:
100111
101112
By default, the latest OpenAPI schema syntax is expected.
102113

114+
Default dialect resolution
115+
--------------------------
116+
103117
The OpenAPI 3.1 and 3.2 base dialect URIs are registered for
104118
``jsonschema.validators.validator_for`` resolution.
105119
Schemas declaring ``"$schema"`` as either

docs/validation.rst

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ The simplest way to validate an instance under OAS schema is to use the ``valida
66
Validate
77
--------
88

9-
To validate an OpenAPI v3.1 schema:
9+
``validate`` call signature is:
10+
11+
.. code-block:: python
12+
13+
validate(instance, schema, cls=OAS32Validator, **kwargs)
14+
15+
The first argument is always the value you want to validate.
16+
The second argument is always the OpenAPI schema object.
17+
The ``cls`` keyword argument is optional and defaults to ``OAS32Validator``.
18+
Use ``cls`` when you need a specific validator version/behavior.
19+
20+
To validate an OpenAPI schema:
1021

1122
.. code-block:: python
1223
@@ -56,6 +67,17 @@ To validate an OpenAPI v3.1 schema:
5667
5768
By default, the latest OpenAPI schema syntax is expected.
5869

70+
Common pitfalls
71+
---------------
72+
73+
- argument order matters: call ``validate(instance, schema)``, not
74+
``validate(schema, instance)``
75+
- ``validate`` does not load files from a path; load your OpenAPI document
76+
first and pass the parsed schema mapping
77+
- when validating a schema fragment that uses ``$ref`` (for example,
78+
``paths/.../responses/.../schema``), provide reference context via
79+
``registry=...`` as shown in :doc:`references`
80+
5981
Validators
6082
----------
6183

@@ -67,32 +89,6 @@ if you want to disambiguate the expected schema version, import and use ``OAS31V
6789
6890
validate({"name": "John", "age": 23}, schema, cls=OAS31Validator)
6991
70-
The OpenAPI 3.1 and 3.2 base dialect URIs are registered for
71-
``jsonschema.validators.validator_for`` resolution.
72-
If your schema declares
73-
``"$schema": "https://spec.openapis.org/oas/3.1/dialect/base"`` or
74-
``"$schema": "https://spec.openapis.org/oas/3.2/dialect/2025-09-17"``,
75-
``validator_for`` resolves directly to ``OAS31Validator`` or
76-
``OAS32Validator`` without unresolved-metaschema fallback warnings.
77-
78-
.. code-block:: python
79-
80-
from jsonschema.validators import validator_for
81-
82-
from openapi_schema_validator import OAS31Validator
83-
from openapi_schema_validator import OAS32Validator
84-
85-
schema = {
86-
"$schema": "https://spec.openapis.org/oas/3.1/dialect/base",
87-
"type": "object",
88-
}
89-
schema32 = {
90-
"$schema": "https://spec.openapis.org/oas/3.2/dialect/2025-09-17",
91-
"type": "object",
92-
}
93-
assert validator_for(schema) is OAS31Validator
94-
assert validator_for(schema32) is OAS32Validator
95-
9692
For OpenAPI 3.2, use ``OAS32Validator``.
9793

9894
In order to validate OpenAPI 3.0 schema, import and use ``OAS30Validator`` instead of ``OAS31Validator``.
@@ -127,6 +123,35 @@ In order to validate OpenAPI 3.0 schema, import and use ``OAS30Validator`` inste
127123
128124
validate({"name": "John", "age": None}, schema, cls=OAS30Validator)
129125
126+
Default dialect resolution
127+
--------------------------
128+
129+
The OpenAPI 3.1 and 3.2 base dialect URIs are registered for
130+
``jsonschema.validators.validator_for`` resolution.
131+
If your schema declares
132+
``"$schema": "https://spec.openapis.org/oas/3.1/dialect/base"`` or
133+
``"$schema": "https://spec.openapis.org/oas/3.2/dialect/2025-09-17"``,
134+
``validator_for`` resolves directly to ``OAS31Validator`` or
135+
``OAS32Validator`` without unresolved-metaschema fallback warnings.
136+
137+
.. code-block:: python
138+
139+
from jsonschema.validators import validator_for
140+
141+
from openapi_schema_validator import OAS31Validator
142+
from openapi_schema_validator import OAS32Validator
143+
144+
schema = {
145+
"$schema": "https://spec.openapis.org/oas/3.1/dialect/base",
146+
"type": "object",
147+
}
148+
schema32 = {
149+
"$schema": "https://spec.openapis.org/oas/3.2/dialect/2025-09-17",
150+
"type": "object",
151+
}
152+
assert validator_for(schema) is OAS31Validator
153+
assert validator_for(schema32) is OAS32Validator
154+
130155
Schema errors vs instance errors
131156
--------------------------------
132157

openapi_schema_validator/shortcuts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ def validate(
2626
checks schema validity first.
2727
Invalid schemas therefore raise ``SchemaError`` before any instance
2828
validation occurs.
29+
30+
Args:
31+
instance: Value to validate against ``schema``.
32+
schema: OpenAPI schema mapping used for validation.
33+
cls: Validator class to use. Defaults to ``OAS32Validator``.
34+
*args: Positional arguments forwarded to ``cls`` constructor.
35+
**kwargs: Keyword arguments forwarded to ``cls`` constructor.
36+
37+
Raises:
38+
jsonschema.exceptions.SchemaError: If ``schema`` is invalid.
39+
jsonschema.exceptions.ValidationError: If ``instance`` is invalid.
2940
"""
3041
schema_dict = cast(dict[str, Any], schema)
3142

0 commit comments

Comments
 (0)