|
1 | 1 | from copy import deepcopy |
| 2 | +from functools import lru_cache |
| 3 | +from typing import Any |
2 | 4 | from typing import Optional |
3 | 5 | from typing import cast |
4 | 6 |
|
5 | 7 | from jsonschema._format import FormatChecker |
6 | 8 | from jsonschema.protocols import Validator |
| 9 | +from jsonschema.validators import validator_for |
7 | 10 | from jsonschema_path import SchemaPath |
8 | 11 |
|
9 | 12 | from openapi_core.validation.schemas._validators import ( |
10 | 13 | build_enforce_properties_required_validator, |
11 | 14 | ) |
| 15 | +from openapi_core.validation.schemas._validators import ( |
| 16 | + build_forbid_unspecified_additional_properties_validator, |
| 17 | +) |
12 | 18 | from openapi_core.validation.schemas.datatypes import FormatValidatorsDict |
13 | 19 | from openapi_core.validation.schemas.validators import SchemaValidator |
14 | 20 |
|
15 | 21 |
|
16 | 22 | class SchemaValidatorsFactory: |
17 | 23 | def __init__( |
18 | 24 | self, |
19 | | - schema_validator_class: type[Validator], |
20 | | - strict_schema_validator_class: Optional[type[Validator]] = None, |
| 25 | + schema_validator_cls: type[Validator], |
21 | 26 | format_checker: Optional[FormatChecker] = None, |
22 | 27 | ): |
23 | | - self.schema_validator_class = schema_validator_class |
24 | | - self.strict_schema_validator_class = strict_schema_validator_class |
| 28 | + self.schema_validator_cls = schema_validator_cls |
25 | 29 | if format_checker is None: |
26 | | - format_checker = self.schema_validator_class.FORMAT_CHECKER |
| 30 | + format_checker = self.schema_validator_cls.FORMAT_CHECKER |
27 | 31 | assert format_checker is not None |
28 | 32 | self.format_checker = format_checker |
29 | 33 |
|
| 34 | + def get_validator_cls( |
| 35 | + self, spec: SchemaPath, schema: SchemaPath |
| 36 | + ) -> type[Validator]: |
| 37 | + return self.schema_validator_cls |
| 38 | + |
30 | 39 | def get_format_checker( |
31 | 40 | self, |
32 | 41 | format_validators: Optional[FormatValidatorsDict] = None, |
@@ -57,34 +66,82 @@ def _add_validators( |
57 | 66 |
|
58 | 67 | def create( |
59 | 68 | self, |
| 69 | + spec: SchemaPath, |
60 | 70 | schema: SchemaPath, |
61 | 71 | format_validators: Optional[FormatValidatorsDict] = None, |
62 | 72 | extra_format_validators: Optional[FormatValidatorsDict] = None, |
63 | 73 | forbid_unspecified_additional_properties: bool = False, |
64 | 74 | enforce_properties_required: bool = False, |
65 | 75 | ) -> SchemaValidator: |
66 | | - validator_class: type[Validator] = self.schema_validator_class |
| 76 | + validator_cls: type[Validator] = self.get_validator_cls(spec, schema) |
| 77 | + if enforce_properties_required: |
| 78 | + validator_cls = build_enforce_properties_required_validator( |
| 79 | + validator_cls |
| 80 | + ) |
67 | 81 | if forbid_unspecified_additional_properties: |
68 | | - if self.strict_schema_validator_class is None: |
69 | | - raise ValueError( |
70 | | - "Strict additional properties validation is not supported " |
71 | | - "by this factory." |
| 82 | + validator_cls = ( |
| 83 | + build_forbid_unspecified_additional_properties_validator( |
| 84 | + validator_cls |
72 | 85 | ) |
73 | | - validator_class = self.strict_schema_validator_class |
74 | | - |
75 | | - if enforce_properties_required: |
76 | | - validator_class = build_enforce_properties_required_validator( |
77 | | - validator_class |
78 | 86 | ) |
79 | 87 |
|
80 | 88 | format_checker = self.get_format_checker( |
81 | 89 | format_validators, extra_format_validators |
82 | 90 | ) |
83 | 91 | with schema.resolve() as resolved: |
84 | | - jsonschema_validator = validator_class( |
| 92 | + jsonschema_validator = validator_cls( |
85 | 93 | resolved.contents, |
86 | 94 | _resolver=resolved.resolver, |
87 | 95 | format_checker=format_checker, |
88 | 96 | ) |
89 | 97 |
|
90 | 98 | return SchemaValidator(schema, jsonschema_validator) |
| 99 | + |
| 100 | + |
| 101 | +class DialectSchemaValidatorsFactory(SchemaValidatorsFactory): |
| 102 | + def __init__( |
| 103 | + self, |
| 104 | + schema_validator_cls: type[Validator], |
| 105 | + default_jsonschema_dialect_id: str, |
| 106 | + format_checker: Optional[FormatChecker] = None, |
| 107 | + ): |
| 108 | + super().__init__(schema_validator_cls, format_checker) |
| 109 | + self.default_jsonschema_dialect_id = default_jsonschema_dialect_id |
| 110 | + |
| 111 | + def get_validator_cls( |
| 112 | + self, spec: SchemaPath, schema: SchemaPath |
| 113 | + ) -> type[Validator]: |
| 114 | + dialect_id = self._get_dialect_id(spec, schema) |
| 115 | + |
| 116 | + validator_cls = self._get_validator_class_for_dialect(dialect_id) |
| 117 | + if validator_cls is None: |
| 118 | + raise ValueError(f"Unknown JSON Schema dialect: {dialect_id!r}") |
| 119 | + |
| 120 | + return validator_cls |
| 121 | + |
| 122 | + def _get_dialect_id( |
| 123 | + self, |
| 124 | + spec: SchemaPath, |
| 125 | + schema: SchemaPath, |
| 126 | + ) -> str: |
| 127 | + try: |
| 128 | + return (schema / "$schema").read_str() |
| 129 | + except KeyError: |
| 130 | + return self._get_default_jsonschema_dialect_id(spec) |
| 131 | + |
| 132 | + def _get_default_jsonschema_dialect_id(self, spec: SchemaPath) -> str: |
| 133 | + return (spec / "jsonSchemaDialect").read_str( |
| 134 | + default=self.default_jsonschema_dialect_id |
| 135 | + ) |
| 136 | + |
| 137 | + @lru_cache |
| 138 | + def _get_validator_class_for_dialect( |
| 139 | + self, dialect_id: str |
| 140 | + ) -> type[Validator] | None: |
| 141 | + return cast( |
| 142 | + type[Validator] | None, |
| 143 | + validator_for( |
| 144 | + {"$schema": dialect_id}, |
| 145 | + default=cast(Any, None), |
| 146 | + ), |
| 147 | + ) |
0 commit comments