23
23
_LOGGER = getLogger (__name__ )
24
24
25
25
26
- def _validate_object (object , schema , exclude_case = False ):
26
+ def _validate_object (object , schema , exclude_case = False , sample_name_colname = False ):
27
27
"""
28
28
Generic function to validate object against a schema
29
29
@@ -34,37 +34,30 @@ def _validate_object(object, schema, exclude_case=False):
34
34
"""
35
35
36
36
validator = Draft7Validator (schema )
37
+ print (object ,schema )
37
38
if not validator .is_valid (object ):
38
39
errors = sorted (validator .iter_errors (object ), key = lambda e : e .path )
39
40
errors_by_type = {}
41
+
40
42
# Accumulate and restructure error objects by error type
41
43
for error in errors :
42
44
if not error .message in errors_by_type :
43
45
errors_by_type [error .message ] = []
46
+
47
+ try :
48
+ instance_name = error .instance [sample_name_colname ]
49
+ except KeyError :
50
+ instance_name = "unnamed"
44
51
errors_by_type [error .message ].append (
45
52
{
46
53
"type" : error .message ,
47
- "message" : f"{ error .message } on instance { error . instance [ 'sample_name' ] } " ,
48
- "sample_name" : error . instance [ 'sample_name' ]
54
+ "message" : f"{ error .message } on instance { instance_name } " ,
55
+ "sample_name" : instance_name
49
56
})
50
-
51
- # Print a summary of errors, organized by error type
52
- n_error_types = len (errors_by_type )
53
- print (f"Found { n_error_types } types of error:" )
54
- for type in errors_by_type :
55
- n = len (errors_by_type [type ])
56
- msg = f" - { type } : ({ n } samples) "
57
- if n < 50 :
58
- msg += ", " .join ([x ["sample_name" ] for x in errors_by_type [type ]])
59
- print (msg )
60
-
61
- if len (errors ) > 1 :
62
- final_msg = f"Validation unsuccessful. { len (errors )} errors found."
63
- else :
64
- final_msg = f"Validation unsuccessful. { len (errors )} error found."
65
-
66
- raise EidoValidationError (final_msg , errors )
67
-
57
+
58
+ raise EidoValidationError ("Validation failed" , errors_by_type )
59
+ else :
60
+ _LOGGER .debug ("Validation was successful..." )
68
61
69
62
def validate_project (project , schema , exclude_case = False ):
70
63
"""
@@ -75,6 +68,7 @@ def validate_project(project, schema, exclude_case=False):
75
68
:param bool exclude_case: whether to exclude validated objects
76
69
from the error. Useful when used ith large projects
77
70
"""
71
+ sample_name_colname = project .sample_name_colname
78
72
schema_dicts = read_schema (schema = schema )
79
73
for schema_dict in schema_dicts :
80
74
project_dict = project .to_dict ()
0 commit comments