Skip to content

Commit e510bd8

Browse files
committed
Fixed error
1 parent 1cec0e9 commit e510bd8

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

src/example.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,29 @@
1111

1212
def valid_test_with_provided_schema():
1313
validator = OSWValidation(zipfile_path=VALID_ZIP_FILE, schema_file_path=SCHEMA_FILE_PATH)
14-
validation = validator.validate()
15-
print(f'Valid Test With Provided Schema: {"Passed" if validation.is_valid else "Failed"}')
14+
result = validator.validate()
15+
print(f'Valid Test With Provided Schema: {"Passed" if result.is_valid else "Failed"}')
1616

1717

1818
def valid_test_without_provided_schema():
1919
validator = OSWValidation(zipfile_path=VALID_ZIP_FILE)
20-
validation = validator.validate()
21-
print(f'Valid Test Without Schema: {"Passed" if validation.is_valid else "Failed"}')
20+
result = validator.validate()
21+
print(f'Valid Test Without Schema: {"Passed" if result.is_valid else "Failed"}')
2222

2323

2424
def invalid_test_with_provided_schema():
2525
validator = OSWValidation(zipfile_path=INVALID_ZIP_FILE, schema_file_path=SCHEMA_FILE_PATH)
26-
validation = validator.validate()
27-
print(f'Invalid Test With Provided Schema: {"Failed" if validation.is_valid else "Passed"}')
26+
result = validator.validate()
27+
if not result.is_valid:
28+
for error in result.errors:
29+
print(error)
30+
print(f'Invalid Test With Provided Schema: {"Failed" if result.is_valid else "Passed"}')
2831

2932

3033
def invalid_test_without_provided_schema():
3134
validator = OSWValidation(zipfile_path=INVALID_ZIP_FILE)
32-
validation = validator.validate()
33-
print(f'Invalid Test Without Schema: {"Failed" if validation.is_valid else "Passed"}')
35+
result = validator.validate()
36+
print(f'Invalid Test Without Schema: {"Failed" if result.is_valid else "Passed"}')
3437

3538

3639
if __name__ == '__main__':
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import os
22
import json
33
import jsonschema
4-
from typing import Dict, Any, Optional
4+
from typing import Dict, Any, Optional, List
55
from .zipfile_handler import ZipFileHandler
66
from .extracted_data_validator import ExtractedDataValidator
77

88
SCHEMA_PATH = os.path.join(os.path.dirname(__file__), 'schema')
99

1010

1111
class 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

1717
class 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

Comments
 (0)