|
11 | 11 | from ..builtin_schemas import get_builtin_schema |
12 | 12 | from ..formats import FormatOptions, make_format_checker |
13 | 13 | from ..parsers import ParserSet |
| 14 | +from ..regex_variants import RegexImplementation |
14 | 15 | from ..utils import is_url_ish |
15 | 16 | from .errors import UnsupportedUrlScheme |
16 | 17 | from .readers import HttpSchemaReader, LocalSchemaReader, StdinSchemaReader |
@@ -45,12 +46,23 @@ def set_defaults_then_validate( |
45 | 46 | ) |
46 | 47 |
|
47 | 48 |
|
| 49 | +def _extend_with_pattern_implementation( |
| 50 | + validator_class: type[jsonschema.protocols.Validator], |
| 51 | + regex_impl: RegexImplementation, |
| 52 | +) -> type[jsonschema.Validator]: |
| 53 | + return jsonschema.validators.extend( |
| 54 | + validator_class, |
| 55 | + {"pattern": regex_impl.pattern_keyword}, |
| 56 | + ) |
| 57 | + |
| 58 | + |
48 | 59 | class SchemaLoaderBase: |
49 | 60 | def get_validator( |
50 | 61 | self, |
51 | 62 | path: pathlib.Path | str, |
52 | 63 | instance_doc: dict[str, t.Any], |
53 | 64 | format_opts: FormatOptions, |
| 65 | + regex_impl: RegexImplementation, |
54 | 66 | fill_defaults: bool, |
55 | 67 | ) -> jsonschema.protocols.Validator: |
56 | 68 | raise NotImplementedError |
@@ -124,14 +136,16 @@ def get_validator( |
124 | 136 | path: pathlib.Path | str, |
125 | 137 | instance_doc: dict[str, t.Any], |
126 | 138 | format_opts: FormatOptions, |
| 139 | + regex_impl: RegexImplementation, |
127 | 140 | fill_defaults: bool, |
128 | 141 | ) -> jsonschema.protocols.Validator: |
129 | | - return self._get_validator(format_opts, fill_defaults) |
| 142 | + return self._get_validator(format_opts, regex_impl, fill_defaults) |
130 | 143 |
|
131 | 144 | @functools.lru_cache |
132 | 145 | def _get_validator( |
133 | 146 | self, |
134 | 147 | format_opts: FormatOptions, |
| 148 | + regex_impl: RegexImplementation, |
135 | 149 | fill_defaults: bool, |
136 | 150 | ) -> jsonschema.protocols.Validator: |
137 | 151 | retrieval_uri = self.get_schema_retrieval_uri() |
@@ -168,6 +182,9 @@ def _get_validator( |
168 | 182 | if fill_defaults: |
169 | 183 | validator_cls = _extend_with_default(validator_cls) |
170 | 184 |
|
| 185 | + # set the regex variant for 'pattern' keywords |
| 186 | + validator_cls = _extend_with_pattern_implementation(validator_cls, regex_impl) |
| 187 | + |
171 | 188 | # now that we know it's safe to try to create the validator instance, do it |
172 | 189 | validator = validator_cls( |
173 | 190 | schema, |
@@ -206,6 +223,7 @@ def get_validator( |
206 | 223 | path: pathlib.Path | str, |
207 | 224 | instance_doc: dict[str, t.Any], |
208 | 225 | format_opts: FormatOptions, |
| 226 | + regex_impl: RegexImplementation, |
209 | 227 | fill_defaults: bool, |
210 | 228 | ) -> jsonschema.protocols.Validator: |
211 | 229 | schema_validator = jsonschema.validators.validator_for(instance_doc) |
|
0 commit comments