|
1 | 1 | """OpenAIP spec validator schemas module.""" |
2 | 2 |
|
3 | 3 | from functools import partial |
| 4 | +from typing import Any |
4 | 5 |
|
5 | 6 | from jsonschema.validators import Draft4Validator |
6 | 7 | from jsonschema.validators import Draft202012Validator |
7 | 8 | from lazy_object_proxy import Proxy |
8 | 9 |
|
9 | 10 | from openapi_spec_validator.schemas.utils import get_schema_content |
| 11 | +from openapi_spec_validator.settings import get_schema_validator_backend |
10 | 12 |
|
11 | | -__all__ = ["schema_v2", "schema_v3", "schema_v30", "schema_v31", "schema_v32"] |
| 13 | +_create_jsonschema_rs_validator_impl: Any = None |
| 14 | + |
| 15 | +# Import jsonschema-rs adapters |
| 16 | +try: |
| 17 | + from openapi_spec_validator.schemas.jsonschema_rs_adapters import ( |
| 18 | + create_validator as _create_jsonschema_rs_validator_impl, |
| 19 | + ) |
| 20 | + from openapi_spec_validator.schemas.jsonschema_rs_adapters import ( |
| 21 | + has_jsonschema_rs_validators, |
| 22 | + ) |
| 23 | + |
| 24 | + _USE_JSONSCHEMA_RS = has_jsonschema_rs_validators() |
| 25 | +except ImportError: |
| 26 | + _create_jsonschema_rs_validator_impl = None |
| 27 | + _USE_JSONSCHEMA_RS = False |
| 28 | + |
| 29 | + def has_jsonschema_rs_validators() -> bool: |
| 30 | + return False |
| 31 | + |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +_BACKEND_MODE = get_schema_validator_backend() |
| 36 | + |
| 37 | +if _BACKEND_MODE == "jsonschema": |
| 38 | + _USE_JSONSCHEMA_RS = False |
| 39 | +elif _BACKEND_MODE == "jsonschema-rs" and not _USE_JSONSCHEMA_RS: |
| 40 | + raise ImportError( |
| 41 | + "OPENAPI_SPEC_VALIDATOR_SCHEMA_VALIDATOR_BACKEND=jsonschema-rs " |
| 42 | + "is set but jsonschema-rs is not available. " |
| 43 | + "Install it with: pip install jsonschema-rs" |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def get_validator_backend() -> str: |
| 48 | + if _USE_JSONSCHEMA_RS: |
| 49 | + return "rust (jsonschema-rs)" |
| 50 | + return "python (jsonschema)" |
| 51 | + |
| 52 | + |
| 53 | +__all__ = [ |
| 54 | + "schema_v2", |
| 55 | + "schema_v3", |
| 56 | + "schema_v30", |
| 57 | + "schema_v31", |
| 58 | + "schema_v32", |
| 59 | + "get_validator_backend", |
| 60 | +] |
12 | 61 |
|
13 | 62 | get_schema_content_v2 = partial(get_schema_content, "2.0") |
14 | 63 | get_schema_content_v30 = partial(get_schema_content, "3.0") |
|
23 | 72 | # alias to the latest v3 version |
24 | 73 | schema_v3 = schema_v32 |
25 | 74 |
|
26 | | -get_openapi_v2_schema_validator = partial(Draft4Validator, schema_v2) |
27 | | -get_openapi_v30_schema_validator = partial(Draft4Validator, schema_v30) |
28 | | -get_openapi_v31_schema_validator = partial(Draft202012Validator, schema_v31) |
29 | | -get_openapi_v32_schema_validator = partial(Draft202012Validator, schema_v32) |
| 75 | + |
| 76 | +def _create_jsonschema_rs_schema_validator( |
| 77 | + schema: dict[str, Any], |
| 78 | + draft: str, |
| 79 | +) -> Any: |
| 80 | + if _create_jsonschema_rs_validator_impl is None: |
| 81 | + raise ImportError( |
| 82 | + "jsonschema-rs is not available. " |
| 83 | + "Install it with: pip install jsonschema-rs" |
| 84 | + ) |
| 85 | + return _create_jsonschema_rs_validator_impl(schema, draft) |
| 86 | + |
| 87 | + |
| 88 | +# Validator factory functions with Rust/Python selection |
| 89 | +def get_openapi_v2_schema_validator() -> Any: |
| 90 | + """Create OpenAPI 2.0 schema validator (Draft4).""" |
| 91 | + if _USE_JSONSCHEMA_RS: |
| 92 | + return _create_jsonschema_rs_schema_validator(dict(schema_v2), draft="draft4") |
| 93 | + return Draft4Validator(schema_v2) |
| 94 | + |
| 95 | + |
| 96 | +def get_openapi_v30_schema_validator() -> Any: |
| 97 | + """Create OpenAPI 3.0 schema validator (Draft4).""" |
| 98 | + if _USE_JSONSCHEMA_RS: |
| 99 | + return _create_jsonschema_rs_schema_validator(dict(schema_v30), draft="draft4") |
| 100 | + return Draft4Validator(schema_v30) |
| 101 | + |
| 102 | + |
| 103 | +def get_openapi_v31_schema_validator() -> Any: |
| 104 | + """Create OpenAPI 3.1 schema validator (Draft 2020-12).""" |
| 105 | + if _USE_JSONSCHEMA_RS: |
| 106 | + return _create_jsonschema_rs_schema_validator( |
| 107 | + dict(schema_v31), |
| 108 | + draft="draft202012", |
| 109 | + ) |
| 110 | + return Draft202012Validator(schema_v31) |
| 111 | + |
| 112 | + |
| 113 | +def get_openapi_v32_schema_validator() -> Any: |
| 114 | + """Create OpenAPI 3.2 schema validator (Draft 2020-12).""" |
| 115 | + if _USE_JSONSCHEMA_RS: |
| 116 | + return _create_jsonschema_rs_schema_validator( |
| 117 | + dict(schema_v32), |
| 118 | + draft="draft202012", |
| 119 | + ) |
| 120 | + return Draft202012Validator(schema_v32) |
| 121 | + |
30 | 122 |
|
31 | 123 | openapi_v2_schema_validator = Proxy(get_openapi_v2_schema_validator) |
32 | 124 | openapi_v30_schema_validator = Proxy(get_openapi_v30_schema_validator) |
|
0 commit comments