Skip to content

Commit 2ead444

Browse files
committed
#782: Adapt validator test for draft2020-12, fixes code styles
1 parent 604fc69 commit 2ead444

File tree

7 files changed

+170
-70
lines changed

7 files changed

+170
-70
lines changed

jsonschema/_format.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,16 @@ def wrap(func):
168168
if draft7:
169169
func = _draft_checkers["draft7"].checks(draft7, raises)(func)
170170
if draft202012:
171-
func = _draft_checkers["draft202012"].checks(draft202012, raises)(func)
171+
func = _draft_checkers["draft202012"].checks(
172+
draft202012, raises
173+
)(func)
172174

173175
# Oy. This is bad global state, but relied upon for now, until
174176
# deprecation. See https://github.com/Julian/jsonschema/issues/519
175177
# and test_format_checkers_come_with_defaults
176-
FormatChecker.cls_checks(draft202012 or draft7 or draft6 or draft4 or draft3, raises)(
177-
func,
178-
)
178+
FormatChecker.cls_checks(
179+
draft202012 or draft7 or draft6 or draft4 or draft3, raises
180+
)(func)
179181
return func
180182
return wrap
181183

jsonschema/_legacy_validators.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ def dependencies_draft3(validator, dependencies, instance, schema):
2727
yield ValidationError(message % (each, property))
2828

2929

30-
def dependencies_draft4_draft6_draft7(validator, dependencies, instance, schema):
30+
def dependencies_draft4_draft6_draft7(
31+
validator,
32+
dependencies,
33+
instance,
34+
schema,
35+
):
3136
"""
3237
Support for the ``dependencies`` validator from pre-draft 2019-09.
3338
34-
In later drafts, the validator was split into separate ``dependentRequired``
35-
and ``dependentSchemas`` validators.
39+
In later drafts, the validator was split into separate
40+
``dependentRequired`` and ``dependentSchemas`` validators.
3641
"""
3742
if not validator.is_type(instance, "object"):
3843
return

jsonschema/_utils.py

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ def find_evaluated_item_indexes_by_schema(validator, instance, schema):
256256
"""
257257
Get all indexes of items that get evaluated under the current schema
258258
259-
Covers all keywords related to unevaluatedItems: items, prefixItems, if, then, else, 'contains', 'unevaluatedItems',
260-
'allOf', 'oneOf', 'anyOf'
259+
Covers all keywords related to unevaluatedItems: items, prefixItems, if,
260+
then, else, 'contains', 'unevaluatedItems', 'allOf', 'oneOf', 'anyOf'
261261
"""
262262
if not validator.is_type(schema, "object"):
263263
return []
264-
evaluated_item_indexes = []
264+
evaluated_indexes = []
265265

266266
if 'items' in schema:
267267
return list(range(0, len(instance)))
@@ -273,49 +273,61 @@ def find_evaluated_item_indexes_by_schema(validator, instance, schema):
273273
validator.resolver.push_scope(scope)
274274

275275
try:
276-
evaluated_item_indexes += find_evaluated_item_indexes_by_schema(validator, instance, resolved)
276+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
277+
validator, instance, resolved)
277278
finally:
278279
validator.resolver.pop_scope()
279280

280281
if 'prefixItems' in schema:
281-
if validator.is_valid(instance, {'prefixItems': schema['prefixItems']}):
282-
evaluated_item_indexes += list(range(0, len(schema['prefixItems'])))
282+
if validator.is_valid(
283+
instance, {'prefixItems': schema['prefixItems']}
284+
):
285+
evaluated_indexes += list(range(0, len(schema['prefixItems'])))
283286

284287
if 'if' in schema:
285288
if validator.is_valid(instance, schema['if']):
286-
evaluated_item_indexes += find_evaluated_item_indexes_by_schema(validator, instance, schema['if'])
289+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
290+
validator, instance, schema['if']
291+
)
287292
if 'then' in schema:
288-
evaluated_item_indexes += find_evaluated_item_indexes_by_schema(validator, instance, schema['then'])
293+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
294+
validator, instance, schema['then']
295+
)
289296
else:
290297
if 'else' in schema:
291-
evaluated_item_indexes += find_evaluated_item_indexes_by_schema(validator, instance, schema['else'])
298+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
299+
validator, instance, schema['else']
300+
)
292301

293302
for keyword in ['contains', 'unevaluatedItems']:
294303
if keyword in schema:
295304
for k, v in enumerate(instance):
296305
if validator.is_valid(v, schema[keyword]):
297-
evaluated_item_indexes.append(k)
306+
evaluated_indexes.append(k)
298307

299308
for keyword in ['allOf', 'oneOf', 'anyOf']:
300309
if keyword in schema:
301310
for subschema in schema[keyword]:
302311
errs = list(validator.descend(instance, subschema))
303312
if not errs:
304-
evaluated_item_indexes += find_evaluated_item_indexes_by_schema(validator, instance, subschema)
313+
evaluated_indexes += find_evaluated_item_indexes_by_schema(
314+
validator, instance, subschema
315+
)
305316

306-
return evaluated_item_indexes
317+
return evaluated_indexes
307318

308319

309320
def find_evaluated_property_keys_by_schema(validator, instance, schema):
310321
"""
311322
Get all keys of items that get evaluated under the current schema
312323
313-
Covers all keywords related to unevaluatedProperties: properties, 'additionalProperties', 'unevaluatedProperties',
314-
patternProperties, dependentSchemas, 'allOf', 'oneOf', 'anyOf', if, then, else
324+
Covers all keywords related to unevaluatedProperties: properties,
325+
'additionalProperties', 'unevaluatedProperties', patternProperties,
326+
dependentSchemas, 'allOf', 'oneOf', 'anyOf', if, then, else
315327
"""
316328
if not validator.is_type(schema, "object"):
317329
return []
318-
evaluated_property_keys = []
330+
evaluated_keys = []
319331

320332
if '$ref' in schema:
321333
resolve = getattr(validator.resolver, "resolve", None)
@@ -324,28 +336,36 @@ def find_evaluated_property_keys_by_schema(validator, instance, schema):
324336
validator.resolver.push_scope(scope)
325337

326338
try:
327-
evaluated_property_keys += find_evaluated_property_keys_by_schema(validator, instance, resolved)
339+
evaluated_keys += find_evaluated_property_keys_by_schema(
340+
validator, instance, resolved
341+
)
328342
finally:
329343
validator.resolver.pop_scope()
330344

331-
for keyword in ['properties', 'additionalProperties', 'unevaluatedProperties']:
345+
for keyword in [
346+
'properties', 'additionalProperties', 'unevaluatedProperties'
347+
]:
332348
if keyword in schema:
333349
if validator.is_type(schema[keyword], "boolean"):
334350
for property, value in instance.items():
335351
if validator.is_valid({property: value}, schema[keyword]):
336-
evaluated_property_keys.append(property)
352+
evaluated_keys.append(property)
337353

338354
if validator.is_type(schema[keyword], "object"):
339355
for property, subschema in schema[keyword].items():
340-
if property in instance and validator.is_valid(instance[property], subschema):
341-
evaluated_property_keys.append(property)
356+
if property in instance and validator.is_valid(
357+
instance[property], subschema
358+
):
359+
evaluated_keys.append(property)
342360

343361
if 'patternProperties' in schema:
344362
for property, value in instance.items():
345363
for pattern, subschema in schema['patternProperties'].items():
346364
if re.search(pattern, property):
347-
if validator.is_valid({property: value}, schema['patternProperties']):
348-
evaluated_property_keys.append(property)
365+
if validator.is_valid(
366+
{property: value}, schema['patternProperties']
367+
):
368+
evaluated_keys.append(property)
349369

350370
if 'dependentSchemas' in schema:
351371
for property, subschema in schema['dependentSchemas'].items():
@@ -354,22 +374,32 @@ def find_evaluated_property_keys_by_schema(validator, instance, schema):
354374

355375
errs = list(validator.descend(instance, subschema))
356376
if not errs:
357-
evaluated_property_keys += find_evaluated_property_keys_by_schema(validator, instance, subschema)
377+
evaluated_keys += find_evaluated_property_keys_by_schema(
378+
validator, instance, subschema
379+
)
358380

359381
for keyword in ['allOf', 'oneOf', 'anyOf']:
360382
if keyword in schema:
361383
for subschema in schema[keyword]:
362384
errs = list(validator.descend(instance, subschema))
363385
if not errs:
364-
evaluated_property_keys += find_evaluated_property_keys_by_schema(validator, instance, subschema)
386+
evaluated_keys += find_evaluated_property_keys_by_schema(
387+
validator, instance, subschema
388+
)
365389

366390
if 'if' in schema:
367391
if validator.is_valid(instance, schema['if']):
368-
evaluated_property_keys += find_evaluated_property_keys_by_schema(validator, instance, schema['if'])
392+
evaluated_keys += find_evaluated_property_keys_by_schema(
393+
validator, instance, schema['if']
394+
)
369395
if 'then' in schema:
370-
evaluated_property_keys += find_evaluated_property_keys_by_schema(validator, instance, schema['then'])
396+
evaluated_keys += find_evaluated_property_keys_by_schema(
397+
validator, instance, schema['then']
398+
)
371399
else:
372400
if 'else' in schema:
373-
evaluated_property_keys += find_evaluated_property_keys_by_schema(validator, instance, schema['else'])
401+
evaluated_keys += find_evaluated_property_keys_by_schema(
402+
validator, instance, schema['else']
403+
)
374404

375-
return evaluated_property_keys
405+
return evaluated_keys

jsonschema/_validators.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def items(validator, items, instance, schema):
7676
if validator.is_type(items, "boolean") and 'prefixItems' in schema:
7777
if not items:
7878
if len(instance) > len(schema['prefixItems']):
79-
yield ValidationError("%r has more items than defined in prefixItems" % instance)
79+
yield ValidationError(
80+
"%r has more items than defined in prefixItems" % instance
81+
)
8082
else:
8183
for error in validator.descend(
8284
instance,
@@ -86,7 +88,11 @@ def items(validator, items, instance, schema):
8688
yield error
8789
else:
8890
if 'prefixItems' in schema:
89-
for error in validator.descend(instance, {'prefixItems': schema['prefixItems']}, path='items__prefixItems'):
91+
for error in validator.descend(
92+
instance,
93+
{'prefixItems': schema['prefixItems']},
94+
path='items__prefixItems',
95+
):
9096
yield error
9197

9298
# Remove evaluated prefixItems indexes
@@ -132,23 +138,30 @@ def contains(validator, contains, instance, schema):
132138
min_contains = schema['minContains']
133139
if not validator.is_type(min_contains, "integer"):
134140
yield ValidationError(
135-
"minContains of %r in not valid under the given schema" % (min_contains,)
141+
"minContains of %r in not valid under the given schema" % (
142+
min_contains,
143+
)
136144
)
137145
return
138146

139147
if 'maxContains' in schema:
140148
max_contains = schema['maxContains']
141149
if not validator.is_type(max_contains, "integer"):
142150
yield ValidationError(
143-
"maxContains of %r is not valid under the given schema" % (instance,)
151+
"maxContains of %r is not valid under the given schema" % (
152+
instance,
153+
)
144154
)
145155
return
146156

147157
# minContains set to 0 will ignore contains
148158
if min_contains == 0:
149159
return
150160

151-
matches = len(list(filter(lambda x: x, [validator.is_valid(element, contains) for element in instance])))
161+
matches = len(list(
162+
filter(lambda x: x, [validator.is_valid(element, contains) for
163+
element in instance]))
164+
)
152165

153166
# default contains behavior
154167
if not matches:
@@ -160,7 +173,8 @@ def contains(validator, contains, instance, schema):
160173
if min_contains and max_contains is None:
161174
if matches < min_contains:
162175
yield ValidationError(
163-
"Invalid number or matches of %r under the given schema, expected min %d, got %d" % (
176+
"Invalid number or matches of %r under the given schema, "
177+
"expected min %d, got %d" % (
164178
instance, min_contains, matches
165179
)
166180
)
@@ -169,7 +183,8 @@ def contains(validator, contains, instance, schema):
169183
if min_contains is None and max_contains:
170184
if matches > max_contains:
171185
yield ValidationError(
172-
"Invalid number or matches of %r under the given schema, expected max %d, got %d" % (
186+
"Invalid number or matches of %r under the given schema, "
187+
"expected max %d, got %d" % (
173188
instance, max_contains, matches
174189
)
175190
)
@@ -178,7 +193,8 @@ def contains(validator, contains, instance, schema):
178193
if min_contains and max_contains:
179194
if matches < min_contains or matches > max_contains:
180195
yield ValidationError(
181-
"Invalid number or matches of %r under the given schema, expected min %d and max %d, got %d" % (
196+
"Invalid number or matches of %r under the given schema, "
197+
"expected min %d and max %d, got %d" % (
182198
instance, min_contains, max_contains, matches
183199
)
184200
)
@@ -366,8 +382,10 @@ def dynamicRef(validator, dynamicRef, instance, schema):
366382
scope_stack = validator.resolver.scopes_stack_copy
367383

368384
for url in scope_stack:
369-
with validator.resolver.resolving(urljoin(url, dynamicRef)) as lookup_schema:
370-
if "$dynamicAnchor" in lookup_schema and fragment == lookup_schema["$dynamicAnchor"]:
385+
lookup_url = urljoin(url, dynamicRef)
386+
with validator.resolver.resolving(lookup_url) as lookup_schema:
387+
if "$dynamicAnchor" in lookup_schema \
388+
and fragment == lookup_schema["$dynamicAnchor"]:
371389
subschema = lookup_schema
372390
for error in validator.descend(instance, subschema):
373391
yield error
@@ -505,18 +523,24 @@ def unevaluatedItems(validator, unevaluatedItems, instance, schema):
505523
if not validator.is_type(instance, "array"):
506524
return
507525

508-
evaluated_item_indexes = find_evaluated_item_indexes_by_schema(validator, instance, schema)
526+
evaluated_item_indexes = find_evaluated_item_indexes_by_schema(
527+
validator, instance, schema
528+
)
509529
for k, v in enumerate(instance):
510530
if k not in evaluated_item_indexes:
511-
for error in validator.descend(v, unevaluatedItems, schema_path="unevaluatedItems"):
531+
for error in validator.descend(
532+
v, unevaluatedItems, schema_path="unevaluatedItems"
533+
):
512534
yield error
513535

514536

515537
def unevaluatedProperties(validator, unevaluatedProperties, instance, schema):
516538
if not validator.is_type(instance, "object"):
517539
return
518540

519-
evaluated_property_keys = find_evaluated_property_keys_by_schema(validator, instance, schema)
541+
evaluated_property_keys = find_evaluated_property_keys_by_schema(
542+
validator, instance, schema
543+
)
520544
for property, subschema in instance.items():
521545
if property not in evaluated_property_keys:
522546
for error in validator.descend(
@@ -534,5 +558,7 @@ def prefixItems(validator, prefixItems, instance, schema):
534558

535559
for k, v in enumerate(instance):
536560
if k < len(prefixItems):
537-
for error in validator.descend(v, prefixItems[k], schema_path="prefixItems"):
561+
for error in validator.descend(
562+
v, prefixItems[k], schema_path="prefixItems"
563+
):
538564
yield error

0 commit comments

Comments
 (0)