Skip to content

Commit 12dd743

Browse files
committed
Refactor: schema validation output
Cleanup the schema validation error output so it is more useable. Testing: make test - python3 - all tests pass
1 parent fdbc720 commit 12dd743

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

manic/externals_description.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ def _validate(self):
330330
fields.
331331
332332
"""
333+
def print_compare_difference(data_a, data_b, loc_a, loc_b):
334+
"""Look through the data structures and print the differences.
335+
336+
"""
337+
for item in data_a:
338+
if item in data_b:
339+
if not isinstance(data_b[item], type(data_a[item])):
340+
printlog(" {item}: {loc} = {val} ({val_type})".format(
341+
item=item, loc=loc_a, val=data_a[item],
342+
val_type=type(data_a[item])))
343+
printlog(" {item} {loc} = {val} ({val_type})".format(
344+
item=' ' * len(item), loc=loc_b, val=data_b[item],
345+
val_type=type(data_b[item])))
346+
else:
347+
printlog(" {item}: {loc} = {val} ({val_type})".format(
348+
item=item, loc=loc_a, val=data_a[item],
349+
val_type=type(data_a[item])))
350+
printlog(" {item} {loc} missing".format(
351+
item=' ' * len(item), loc=loc_b))
352+
333353
def validate_data_struct(schema, data):
334354
"""Compare a data structure against a schema and validate all required
335355
fields are present.
@@ -339,26 +359,29 @@ def validate_data_struct(schema, data):
339359
in_ref = True
340360
valid = True
341361
if isinstance(schema, dict) and isinstance(data, dict):
362+
# Both are dicts, recursively verify that all fields
363+
# in schema are present in the data.
342364
for k in schema:
343365
in_ref = in_ref and (k in data)
344366
if in_ref:
345367
valid = valid and (
346368
validate_data_struct(schema[k], data[k]))
347369
is_valid = in_ref and valid
348370
else:
371+
# non-recursive structure. verify data and schema have
372+
# the same type.
349373
is_valid = isinstance(data, type(schema))
374+
350375
if not is_valid:
351-
printlog(" Unmatched schema and data:")
376+
printlog(" Unmatched schema and input:")
352377
if isinstance(schema, dict):
353-
for item in schema:
354-
printlog(" {0} schema = {1} ({2})".format(
355-
item, schema[item], type(schema[item])))
356-
printlog(" {0} data = {1} ({2})".format(
357-
item, data[item], type(data[item])))
378+
print_compare_difference(schema, data, 'schema', 'input')
379+
print_compare_difference(data, schema, 'input', 'schema')
358380
else:
359381
printlog(" schema = {0} ({1})".format(
360382
schema, type(schema)))
361-
printlog(" data = {0} ({1})".format(data, type(data)))
383+
printlog(" input = {0} ({1})".format(data, type(data)))
384+
362385
return is_valid
363386

364387
for field in self:

0 commit comments

Comments
 (0)