10
10
def validate_json_schema (
11
11
schema_path : Union [Path , str ],
12
12
data_dict : Optional [Dict ] = None ,
13
- data_path : Union [Path , str , None ] = None ,
14
- error_msg : Optional [str ] = "" ,
15
- indent : Optional [int ] = 2 ,
13
+ data_path : Optional [ Union [Path , str ] ] = None ,
14
+ error_msg : Optional [str ] = None ,
15
+ indent : Optional [int ] = None ,
16
16
):
17
17
"""
18
- Validate metadata.json files against schema.
18
+ Validate a JSON file against a schema.
19
19
20
20
Either `data_dict` or `data_path` must be provided.
21
21
22
- `error_msg` and `indent` are used to format the error message if validation fails.
22
+ `error_msg` and `indent` can be used to format the error message if validation fails.
23
23
"""
24
24
# Confirm that *either* `data_dict` *or* `data_path` has been provided, otherwise raise ValueError
25
25
if data_dict and data_path :
@@ -48,15 +48,19 @@ def validate_json_schema(
48
48
# Load data to be validated
49
49
if data_dict :
50
50
if not isinstance (data_dict , Dict ):
51
- raise ValueError ("Invalid data format" )
51
+ raise ValueError (
52
+ "Invalid data format, `data_dict` should be a Python dictionary"
53
+ )
52
54
data_to_validate = data_dict
53
55
54
56
if data_path :
55
57
# Convert `data_path` to pathlib.Path
56
58
if isinstance (data_path , str ):
57
59
data_path = Path (data_path ).absolute ()
58
60
if not isinstance (data_path , Path ):
59
- raise ValueError ("Invalid data format" )
61
+ raise ValueError (
62
+ "Invalid data format, `data_path` should be a pathlib.Path or string of file location"
63
+ )
60
64
# Check `data_path` exists
61
65
if not data_path .exists ():
62
66
raise ValueError (f"Data path '{ data_path } ' does not exist" )
@@ -82,5 +86,6 @@ def validate_json_schema(
82
86
JSON data:
83
87
{ json .dumps (data_to_validate , indent = indent )}
84
88
"""
89
+ print (formatted_msg )
85
90
raise ValidationError (formatted_msg ) from err
86
91
raise err
0 commit comments