11import os
22import json
33import jsonschema
4- from typing import Dict , Any , Optional
4+ from typing import Dict , Any , Optional , List
55from .zipfile_handler import ZipFileHandler
66from .extracted_data_validator import ExtractedDataValidator
77
88SCHEMA_PATH = os .path .join (os .path .dirname (__file__ ), 'schema' )
99
1010
1111class ValidationResult :
12- def __init__ (self , is_valid : bool , error : Optional [str ] = None ):
12+ def __init__ (self , is_valid : bool , errors : Optional [List [ str ] ] = None ):
1313 self .is_valid = is_valid
14- self .error = error
14+ self .errors = errors
1515
1616
1717class OSWValidation :
@@ -20,21 +20,19 @@ class OSWValidation:
2020 def __init__ (self , zipfile_path : str , schema_file_path = None ):
2121 self .zipfile_path = zipfile_path
2222 self .extracted_dir = None
23+ self .errors = []
2324 if schema_file_path is None :
24- self .schema = self . load_osw_schema ( OSWValidation .default_schema_file_path )
25+ self .schema_file_path = OSWValidation .default_schema_file_path
2526 else :
26- self .schema = self .load_osw_schema (schema_file_path )
27-
28- self .error = None
27+ self .schema_file_path = schema_file_path
2928
3029 def load_osw_schema (self , schema_path : str ) -> Dict [str , Any ]:
3130 '''Load OSW Schema'''
3231 try :
3332 with open (schema_path , 'r' ) as file :
34- schema = json .load (file )
35- return schema
33+ return json .load (file )
3634 except Exception as e :
37- self .error = e
35+ self .errors . append ( f'Invalid or missing schema file: { e } ' )
3836 raise Exception (f'Invalid or missing schema file: { e } ' )
3937
4038 def validate (self ) -> ValidationResult :
@@ -44,40 +42,39 @@ def validate(self) -> ValidationResult:
4442 self .extracted_dir = zip_handler .extract_zip ()
4543
4644 if not self .extracted_dir :
47- self .error = zip_handler .error
48- return ValidationResult (False , self .error )
45+ self .errors . append ( zip_handler .error )
46+ return ValidationResult (False , self .errors )
4947
5048 # Validate the folder structure
5149 validator = ExtractedDataValidator (self .extracted_dir )
5250 if not validator .is_valid ():
53- self .error = validator .error
54- return ValidationResult (False , self .error )
51+ self .errors . append ( validator .error )
52+ return ValidationResult (False , self .errors )
5553
5654 for file in validator .files :
5755 file_path = os .path .join (file )
5856 is_valid = self .validate_osw_errors (self .load_osw_file (file_path ))
5957 if not is_valid :
6058 zip_handler .remove_extracted_files ()
61- return ValidationResult (False , self .error )
59+ return ValidationResult (False , self .errors )
6260
63- return ValidationResult (True , None )
61+ return ValidationResult (True )
6462 except Exception as e :
65- self .error = f'Unable to validate: { e } '
66- return ValidationResult (False , self .error )
63+ self .errors . append ( f'Unable to validate: { e } ' )
64+ return ValidationResult (False , self .errors )
6765
6866 def load_osw_file (self , graph_geojson_path : str ) -> Dict [str , Any ]:
6967 '''Load OSW Data'''
70-
7168 with open (graph_geojson_path , 'r' ) as file :
72- data = json .load (file )
73- return data
69+ return json .load (file )
7470
7571 def validate_osw_errors (self , geojson_data : Dict [str , Any ]) -> bool :
7672 '''Validate OSW Data against the schema and process all errors'''
77- validator = jsonschema .Draft7Validator (self .schema )
73+ validator = jsonschema .Draft7Validator (self .load_osw_schema ( self . schema_file_path ) )
7874 errors = list (validator .iter_errors (geojson_data ))
7975
8076 if errors :
81- self .error = errors [0 ]
77+ for error in errors :
78+ self .errors .append (f'Validation error: { error .message } ' )
8279 return False
8380 return True
0 commit comments