forked from python-openapi/openapi-schema-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortcuts.py
More file actions
28 lines (24 loc) · 756 Bytes
/
shortcuts.py
File metadata and controls
28 lines (24 loc) · 756 Bytes
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
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.validators import OAS31Validator
def validate(
instance: Any,
schema: Mapping[str, Any],
cls: type[Validator] = OAS31Validator,
*args: Any,
**kwargs: Any
) -> None:
"""
Validate an instance against a given schema using the specified validator class.
"""
schema_dict = cast(dict[str, Any], schema)
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