Skip to content

Commit 2aa9f7a

Browse files
authored
Merge pull request #269 from python-openapi/docs/document-validate-schema-and-registry-scope
Document validate schema and registry scope
2 parents cde7436 + 3ec8ce2 commit 2aa9f7a

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ The first argument is always the value you want to validate.
6060
The second argument is always the OpenAPI schema object.
6161
The ``cls`` keyword argument is optional and defaults to ``OAS32Validator``.
6262
Use ``cls`` when you need a specific validator version/behavior.
63+
Common forwarded keyword arguments include ``registry`` (reference context)
64+
and ``format_checker`` (format validation behavior).
6365

6466
To validate an OpenAPI schema:
6567

docs/references.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
References
22
==========
33

4-
You can resolve JSON Schema references by passing registry
4+
You can resolve JSON Schema references by passing registry.
5+
The ``validate(instance, schema, ...)`` shortcut resolves local references
6+
(``#/...``) against the provided ``schema`` mapping.
57

68
.. code-block:: python
79

docs/validation.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ The first argument is always the value you want to validate.
1616
The second argument is always the OpenAPI schema object.
1717
The ``cls`` keyword argument is optional and defaults to ``OAS32Validator``.
1818
Use ``cls`` when you need a specific validator version/behavior.
19+
Common forwarded keyword arguments include:
20+
21+
- ``registry`` for reference resolution context
22+
- ``format_checker`` to control format validation behavior
1923

2024
To validate an OpenAPI schema:
2125

@@ -74,9 +78,13 @@ Common pitfalls
7478
``validate(schema, instance)``
7579
- ``validate`` does not load files from a path; load your OpenAPI document
7680
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`
81+
- ``validate`` treats the provided ``schema`` as the reference root; local
82+
references like ``#/components/...`` must exist within that mapping
83+
- when a schema uses external references (for example ``urn:...``), provide
84+
reference context via ``registry=...`` as shown in :doc:`references`
85+
- for schema fragments containing local references (for example,
86+
``paths/.../responses/.../schema``), use a validator built from the full
87+
schema root and then validate the fragment via ``validator.evolve(...)``
8088

8189
Validators
8290
----------

openapi_schema_validator/shortcuts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ def validate(
2929
3030
Args:
3131
instance: Value to validate against ``schema``.
32-
schema: OpenAPI schema mapping used for validation.
32+
schema: OpenAPI schema mapping used for validation. Local references
33+
(``#/...``) are resolved against this mapping.
3334
cls: Validator class to use. Defaults to ``OAS32Validator``.
3435
*args: Positional arguments forwarded to ``cls`` constructor.
35-
**kwargs: Keyword arguments forwarded to ``cls`` constructor.
36+
**kwargs: Keyword arguments forwarded to ``cls`` constructor
37+
(for example ``registry`` and ``format_checker``).
3638
3739
Raises:
3840
jsonschema.exceptions.SchemaError: If ``schema`` is invalid.

0 commit comments

Comments
 (0)