|
3 | 3 | from typing import Callable
|
4 | 4 | from typing import Hashable
|
5 | 5 | from typing import Mapping
|
6 |
| -from typing import Tuple |
7 | 6 |
|
8 | 7 | from jsonschema_spec.handlers import all_urls_handler
|
9 | 8 |
|
10 |
| -from openapi_spec_validator.exceptions import ValidatorDetectError |
11 |
| -from openapi_spec_validator.validation.validators import SpecValidator |
| 9 | +from openapi_spec_validator.validation.protocols import SupportsValidation |
12 | 10 |
|
13 | 11 |
|
14 |
| -def detect_validator(choices: Mapping[Tuple[str, str], SpecValidator], spec: Mapping[Hashable, Any]) -> SpecValidator: |
15 |
| - for (key, value), validator in choices.items(): |
16 |
| - if key in spec and spec[key].startswith(value): |
17 |
| - return validator |
18 |
| - raise ValidatorDetectError("Spec schema version not detected") |
19 |
| - |
20 |
| - |
21 |
| -def validate_spec_detect_factory(choices: Mapping[Tuple[str, str], SpecValidator]) -> Callable[[Mapping[Hashable, Any], str], None]: |
22 |
| - def validate(spec: Mapping[Hashable, Any], spec_url: str = "") -> None: |
23 |
| - validator = detect_validator(choices, spec) |
24 |
| - return validator.validate(spec, spec_url=spec_url) |
25 |
| - |
26 |
| - return validate |
27 |
| - |
28 |
| - |
29 |
| -def validate_spec_factory(validator: SpecValidator) -> Callable[[Mapping[Hashable, Any], str], None]: |
| 12 | +def validate_spec_factory( |
| 13 | + validator: SupportsValidation, |
| 14 | +) -> Callable[[Mapping[Hashable, Any], str], None]: |
30 | 15 | def validate(spec: Mapping[Hashable, Any], spec_url: str = "") -> None:
|
31 | 16 | return validator.validate(spec, spec_url=spec_url)
|
32 | 17 |
|
33 | 18 | return validate
|
34 | 19 |
|
35 | 20 |
|
36 |
| -def validate_spec_url_detect_factory(choices: Mapping[Tuple[str, str], SpecValidator]) -> Callable[[str], None]: |
37 |
| - def validate(spec_url: str) -> None: |
38 |
| - spec = all_urls_handler(spec_url) |
39 |
| - validator = detect_validator(choices, spec) |
40 |
| - return validator.validate(spec, spec_url=spec_url) |
41 |
| - |
42 |
| - return validate |
43 |
| - |
44 |
| - |
45 |
| -def validate_spec_url_factory(validator: SpecValidator) -> Callable[[str], None]: |
| 21 | +def validate_spec_url_factory( |
| 22 | + validator: SupportsValidation, |
| 23 | +) -> Callable[[str], None]: |
46 | 24 | def validate(spec_url: str) -> None:
|
47 | 25 | spec = all_urls_handler(spec_url)
|
48 | 26 | return validator.validate(spec, spec_url=spec_url)
|
|
0 commit comments