fix: resolve CI/CD test and lint failures
This commit is contained in:
@@ -11,11 +11,7 @@ from .parsers import load_data
|
|||||||
class SchemaValidator:
|
class SchemaValidator:
|
||||||
"""Handle JSON Schema validation for data files."""
|
"""Handle JSON Schema validation for data files."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, schema: Optional[Dict[str, Any]] = None, schema_file: Optional[str] = None):
|
||||||
self,
|
|
||||||
schema: Optional[Dict[str, Any]] = None,
|
|
||||||
schema_file: Optional[str] = None,
|
|
||||||
):
|
|
||||||
"""Initialize validator with optional schema."""
|
"""Initialize validator with optional schema."""
|
||||||
self.schema = None
|
self.schema = None
|
||||||
self.validator_class = None
|
self.validator_class = None
|
||||||
@@ -27,12 +23,11 @@ class SchemaValidator:
|
|||||||
def set_schema(self, schema: Dict[str, Any]) -> None:
|
def set_schema(self, schema: Dict[str, Any]) -> None:
|
||||||
"""Set the validation schema."""
|
"""Set the validation schema."""
|
||||||
self.schema = schema
|
self.schema = schema
|
||||||
draft = schema.get("$schema", "http://json-schema.org/draft-07/schema#")
|
draft = schema.get("$", "http://json-schema.org/draft-07/schema#")
|
||||||
if "draft-07" in draft or "draft-07" in draft:
|
if "draft-07" in draft or "draft-07" in draft:
|
||||||
self.validator_class = Draft7Validator
|
self.validator_class = Draft7Validator
|
||||||
elif "draft-2019-09" in draft:
|
elif "draft-2019-09" in draft:
|
||||||
from jsonschema import Draft201909Validator
|
from jsonschema import Draft201909Validator
|
||||||
|
|
||||||
self.validator_class = Draft201909Validator
|
self.validator_class = Draft201909Validator
|
||||||
else:
|
else:
|
||||||
self.validator_class = Draft7Validator
|
self.validator_class = Draft7Validator
|
||||||
@@ -42,9 +37,7 @@ class SchemaValidator:
|
|||||||
schema_data = load_data(schema_file)
|
schema_data = load_data(schema_file)
|
||||||
self.set_schema(schema_data)
|
self.set_schema(schema_data)
|
||||||
|
|
||||||
def validate(
|
def validate(self, data: Any, raise_on_error: bool = False) -> List[ValidationError]:
|
||||||
self, data: Any, raise_on_error: bool = False
|
|
||||||
) -> List[ValidationError]:
|
|
||||||
"""Validate data against the schema."""
|
"""Validate data against the schema."""
|
||||||
if self.schema is None:
|
if self.schema is None:
|
||||||
raise ValueError("No schema has been set for validation")
|
raise ValueError("No schema has been set for validation")
|
||||||
@@ -54,9 +47,7 @@ class SchemaValidator:
|
|||||||
raise ValidationError(errors[0].message)
|
raise ValidationError(errors[0].message)
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
def validate_file(
|
def validate_file(self, file_path: str, format: Optional[str] = None) -> List[ValidationError]:
|
||||||
self, file_path: str, format: Optional[str] = None
|
|
||||||
) -> List[ValidationError]:
|
|
||||||
"""Validate a file against the schema."""
|
"""Validate a file against the schema."""
|
||||||
data = load_data(file_path, format)
|
data = load_data(file_path, format)
|
||||||
return self.validate(data)
|
return self.validate(data)
|
||||||
@@ -70,9 +61,7 @@ class SchemaValidator:
|
|||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
|
||||||
def validate_data(
|
def validate_data(data: Any, schema: Dict[str, Any]) -> tuple[bool, List[str]]:
|
||||||
data: Any, schema: Dict[str, Any]
|
|
||||||
) -> tuple[bool, List[str]]:
|
|
||||||
"""Validate data against a schema and return success status and error messages."""
|
"""Validate data against a schema and return success status and error messages."""
|
||||||
validator = SchemaValidator(schema=schema)
|
validator = SchemaValidator(schema=schema)
|
||||||
errors = validator.validate(data)
|
errors = validator.validate(data)
|
||||||
@@ -80,9 +69,7 @@ def validate_data(
|
|||||||
return len(errors) == 0, messages
|
return len(errors) == 0, messages
|
||||||
|
|
||||||
|
|
||||||
def validate_file(
|
def validate_file(file_path: str, schema_file: str, format: Optional[str] = None) -> tuple[bool, List[str]]:
|
||||||
file_path: str, schema_file: str, format: Optional[str] = None
|
|
||||||
) -> tuple[bool, List[str]]:
|
|
||||||
"""Validate a file against a schema file."""
|
"""Validate a file against a schema file."""
|
||||||
validator = SchemaValidator(schema_file=schema_file)
|
validator = SchemaValidator(schema_file=schema_file)
|
||||||
errors = validator.validate_file(file_path, format)
|
errors = validator.validate_file(file_path, format)
|
||||||
|
|||||||
Reference in New Issue
Block a user