-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathshortcuts.py
More file actions
43 lines (37 loc) · 1.39 KB
/
shortcuts.py
File metadata and controls
43 lines (37 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from typing import Any
from typing import Mapping
from typing import cast
from jsonschema.exceptions import best_match
from jsonschema.protocols import Validator
from openapi_schema_validator._dialects import OAS31_BASE_DIALECT_ID
from openapi_schema_validator._dialects import OAS32_BASE_DIALECT_ID
from openapi_schema_validator.validators import OAS32Validator
from openapi_schema_validator.validators import check_openapi_schema
def validate(
instance: Any,
schema: Mapping[str, Any],
cls: type[Validator] = OAS32Validator,
*args: Any,
**kwargs: Any
) -> None:
"""
Validate an instance against a given schema using the specified validator class.
"""
schema_dict = cast(dict[str, Any], schema)
meta_schema = getattr(cls, "META_SCHEMA", None)
# jsonschema's default check_schema path does not accept a custom
# registry, so for OAS dialects we use the package registry
# explicitly to keep metaschema resolution local and deterministic.
if isinstance(meta_schema, dict) and meta_schema.get("$id") in (
OAS31_BASE_DIALECT_ID,
OAS32_BASE_DIALECT_ID,
):
check_openapi_schema(cls, schema_dict)
else:
cls.check_schema(schema_dict)
validator = cls(schema_dict, *args, **kwargs)
error = best_match(
validator.evolve(schema=schema_dict).iter_errors(instance)
)
if error is not None:
raise error