Skip to content

Commit c6b0fc4

Browse files
committed
#782: Add compatibility to draft7 and older
1 parent 841a0a2 commit c6b0fc4

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

jsonschema/_legacy_validators.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
from jsonschema.exceptions import ValidationError
33

44

5+
def ignore_ref_siblings(schema):
6+
"""
7+
Returns a list of validators that should apply for the given schema
8+
Used for draft7 and earlier
9+
"""
10+
ref = schema.get(u"$ref")
11+
if ref is not None:
12+
return [(u"$ref", ref)]
13+
else:
14+
return schema.items()
15+
16+
517
def dependencies_draft3(validator, dependencies, instance, schema):
618
if not validator.is_type(instance, "object"):
719
return

jsonschema/validators.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
validators = {}
2828
meta_schemas = _utils.URIDict()
29-
vocabulary_schemas = _utils.URIDict()
29+
_VOCABULARIES = _utils.URIDict()
3030

3131

3232
def validates(version):
@@ -56,7 +56,7 @@ def _validates(cls):
5656

5757
for vocabulary in cls.VOCABULARY_SCHEMAS:
5858
vocabulary_id = cls.ID_OF(vocabulary)
59-
vocabulary_schemas[vocabulary_id] = vocabulary
59+
_VOCABULARIES[vocabulary_id] = vocabulary
6060

6161
return cls
6262
return _validates
@@ -70,11 +70,10 @@ def _id_of(schema):
7070

7171
def _store_schema_list():
7272
return [
73-
(id, validator.META_SCHEMA)
74-
for id, validator in meta_schemas.items()
75-
] + [
76-
(id, schema) for id, schema in vocabulary_schemas.items()
77-
]
73+
(id, validator.META_SCHEMA) for id, validator in meta_schemas.items()
74+
] + [
75+
(id, schema) for id, schema in _VOCABULARIES.items()
76+
]
7877

7978

8079
def create(
@@ -84,6 +83,7 @@ def create(
8483
version=None,
8584
type_checker=_types.draft7_type_checker,
8685
id_of=_id_of,
86+
applicable_validators=lambda schema: schema.items(),
8787
):
8888
"""
8989
Create a new validator class.
@@ -126,6 +126,11 @@ def create(
126126
127127
A function that given a schema, returns its ID.
128128
129+
applicable_validators (collections.abc.Callable):
130+
131+
A function that returns a list of validators that should apply
132+
to a given schema
133+
129134
Returns:
130135
131136
a new `jsonschema.IValidator` class
@@ -177,16 +182,7 @@ def iter_errors(self, instance, _schema=None):
177182
if scope:
178183
self.resolver.push_scope(scope)
179184
try:
180-
validators = []
181-
182-
# We make sure $ref is evaluated first if available in schema
183-
ref = _schema.get(u"$ref")
184-
if ref is not None:
185-
validators = [(u"$ref", ref)]
186-
187-
# Add all remaining validators
188-
validators += [(x, _schema[x]) for x in _schema if x not in ["$ref"]]
189-
185+
validators = applicable_validators(_schema)
190186
for k, v in validators:
191187
validator = self.VALIDATORS.get(k)
192188
if validator is None:
@@ -332,6 +328,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
332328
type_checker=_types.draft3_type_checker,
333329
version="draft3",
334330
id_of=lambda schema: schema.get(u"id", ""),
331+
applicable_validators=_legacy_validators.ignore_ref_siblings,
335332
)
336333

337334
Draft4Validator = create(
@@ -367,6 +364,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
367364
type_checker=_types.draft4_type_checker,
368365
version="draft4",
369366
id_of=lambda schema: schema.get(u"id", ""),
367+
applicable_validators=_legacy_validators.ignore_ref_siblings,
370368
)
371369

372370
Draft6Validator = create(
@@ -406,6 +404,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
406404
},
407405
type_checker=_types.draft6_type_checker,
408406
version="draft6",
407+
applicable_validators=_legacy_validators.ignore_ref_siblings,
409408
)
410409

411410
Draft7Validator = create(
@@ -446,6 +445,7 @@ def extend(validator, validators=(), version=None, type_checker=None):
446445
},
447446
type_checker=_types.draft7_type_checker,
448447
version="draft7",
448+
applicable_validators=_legacy_validators.ignore_ref_siblings,
449449
)
450450

451451
Draft202012Validator = create(

0 commit comments

Comments
 (0)