|
1 | | -from collections import deque |
2 | 1 | from collections.abc import Mapping, MutableMapping, Sequence |
3 | 2 | from urllib.parse import urlsplit |
4 | 3 | import itertools |
@@ -347,85 +346,3 @@ def find_evaluated_property_keys_by_schema(validator, instance, schema): |
347 | 346 | ) |
348 | 347 |
|
349 | 348 | return evaluated_keys |
350 | | - |
351 | | - |
352 | | -def _schema_is_referenced(schema, parent_schema): |
353 | | - """ |
354 | | - Checks if a schema is referenced by another schema |
355 | | - """ |
356 | | - return ( |
357 | | - "$id" in schema |
358 | | - and "$ref" in parent_schema |
359 | | - and parent_schema["$ref"] == schema["$id"] |
360 | | - ) |
361 | | - |
362 | | - |
363 | | -def _find_dynamic_anchor_extender(validator, scopes, fragment, schema): |
364 | | - """ |
365 | | - Find a schema that extends the dynamic anchor |
366 | | - """ |
367 | | - for url in scopes: |
368 | | - with validator.resolver.resolving(url) as parent_schema: |
369 | | - if _schema_is_referenced(schema, parent_schema): |
370 | | - return validator.resolver.resolve_fragment( |
371 | | - parent_schema, |
372 | | - fragment, |
373 | | - ) |
374 | | - |
375 | | - |
376 | | -def _find_dynamic_anchor_intermediate(validator, scopes, fragment): |
377 | | - """ |
378 | | - Find a schema that extends the dynamic anchor by an intermediate schema |
379 | | - """ |
380 | | - for url in scopes: |
381 | | - with validator.resolver.resolving(url) as schema: |
382 | | - if "$id" in schema: |
383 | | - for intermediate_url in scopes: |
384 | | - with validator.resolver.resolving( |
385 | | - intermediate_url) as intermediate_schema: |
386 | | - for subschema in search_schema( |
387 | | - intermediate_schema, match_keyword("$ref")): |
388 | | - if _schema_is_referenced(subschema, schema): |
389 | | - return _find_dynamic_anchor_extender( |
390 | | - validator, |
391 | | - scopes, |
392 | | - fragment, |
393 | | - subschema, |
394 | | - ) |
395 | | - |
396 | | - |
397 | | -def dynamic_anchor_extender(validator, scopes, fragment, schema, subschema): |
398 | | - extender_schema = _find_dynamic_anchor_extender( |
399 | | - validator, scopes, fragment, schema, |
400 | | - ) |
401 | | - if not extender_schema: |
402 | | - extender_schema = _find_dynamic_anchor_extender( |
403 | | - validator, scopes, fragment, subschema, |
404 | | - ) |
405 | | - if not extender_schema: |
406 | | - extender_schema = _find_dynamic_anchor_intermediate( |
407 | | - validator, scopes, fragment, |
408 | | - ) |
409 | | - |
410 | | - return extender_schema |
411 | | - |
412 | | - |
413 | | -def match_keyword(keyword): |
414 | | - def matcher(value): |
415 | | - if keyword in value: |
416 | | - yield value |
417 | | - return matcher |
418 | | - |
419 | | - |
420 | | -def search_schema(schema, matcher): |
421 | | - """Breadth-first search routine.""" |
422 | | - values = deque([schema]) |
423 | | - while values: |
424 | | - value = values.pop() |
425 | | - if isinstance(value, list): |
426 | | - values.extendleft(value) |
427 | | - continue |
428 | | - if not isinstance(value, dict): |
429 | | - continue |
430 | | - yield from matcher(value) |
431 | | - values.extendleft(value.values()) |
0 commit comments