1010def validate_json_schema (
1111 schema_path : Union [Path , str ],
1212 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 ,
1616):
1717 """
18- Validate metadata.json files against schema.
18+ Validate a JSON file against a schema.
1919
2020 Either `data_dict` or `data_path` must be provided.
2121
22- `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.
2323 """
2424 # Confirm that *either* `data_dict` *or* `data_path` has been provided, otherwise raise ValueError
2525 if data_dict and data_path :
@@ -46,14 +46,19 @@ def validate_json_schema(
4646 # Load data to be validated as dict
4747 if data_dict :
4848 if not isinstance (data_dict , Dict ):
49- raise ValueError ("Invalid data format" )
49+ raise ValueError (
50+ "Invalid data format, `data_dict` should be a Python dictionary"
51+ )
5052 data_to_validate = data_dict
5153
5254 if data_path :
5355 if isinstance (data_path , str ):
5456 data_path = Path (data_path ).absolute ()
5557 if not isinstance (data_path , Path ):
56- raise ValueError ("Invalid data format" )
58+ raise ValueError (
59+ "Invalid data format, `data_path` should be a pathlib.Path or string of file location"
60+ )
61+ # Check `data_path` exists
5762 if not data_path .exists ():
5863 raise ValueError (f"Data path '{ data_path } ' does not exist" )
5964 with open (data_path , "r" ) as f :
@@ -78,5 +83,6 @@ def validate_json_schema(
7883JSON data:
7984{ json .dumps (data_to_validate , indent = indent )}
8085"""
86+ print (formatted_msg )
8187 raise ValidationError (formatted_msg ) from err
8288 raise err
0 commit comments