Skip to content

Commit 0e76c54

Browse files
committed
#782: Refactor older draft compatibility
1 parent 7255b4c commit 0e76c54

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

jsonschema/validators.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ def check_schema(cls, schema):
141141
for error in cls(cls.META_SCHEMA).iter_errors(schema):
142142
raise exceptions.SchemaError.create_from(error)
143143

144+
def schema_validators(self, schema):
145+
"""
146+
Returns a list of validators that should apply for the given schema
147+
"""
148+
legacy_cls = [Draft3Validator, Draft4Validator, Draft6Validator, Draft7Validator]
149+
150+
ref = schema.get(u"$ref")
151+
if ref is not None:
152+
# We make sure $ref is evaluated first if available in schema
153+
validators = [(u"$ref", ref)]
154+
155+
# Apply all remaining validators for newer drafts
156+
if not any(isinstance(self, x) for x in legacy_cls):
157+
validators += [(x, schema[x]) for x in schema if x not in ["$ref"]]
158+
else:
159+
validators = schema.items()
160+
161+
return validators
162+
144163
def iter_errors(self, instance, _schema=None):
145164
if _schema is None:
146165
_schema = self.schema
@@ -161,23 +180,7 @@ def iter_errors(self, instance, _schema=None):
161180
if scope:
162181
self.resolver.push_scope(scope)
163182
try:
164-
if isinstance(self, Draft202012Validator):
165-
validators = []
166-
# We make sure $ref is evaluated first if available in schema
167-
ref = _schema.get(u"$ref")
168-
if ref is not None:
169-
validators = [(u"$ref", ref)]
170-
171-
# Add all remaining validators
172-
validators += [(x, _schema[x]) for x in _schema if x not in ["$ref"]]
173-
else:
174-
# Compatibility to Draft7 and older
175-
ref = _schema.get(u"$ref")
176-
if ref is not None:
177-
validators = [(u"$ref", ref)]
178-
else:
179-
validators = _schema.items()
180-
183+
validators = self.schema_validators(_schema)
181184
for k, v in validators:
182185
validator = self.VALIDATORS.get(k)
183186
if validator is None:

0 commit comments

Comments
 (0)