Coverage for src/csv_schema_validator/schema_validator.py: 20%
15 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-20 12:34 +0200
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-20 12:34 +0200
1# schema_validator.py
2from pydantic import ValidationError
3from .schema_models import CSVSchema
6def validate_schema_structure(schema_dict: dict) -> dict:
7 """
8 Validate the structure of a CSV schema.
10 Args:
11 schema_dict: Dictionary containing the schema definition
13 Returns:
14 Dictionary with validation results:
15 - is_valid: Boolean indicating if schema is valid
16 - errors: List of validation errors
17 - validated_schema: Pydantic model instance if valid
18 """
19 try:
20 validated_schema = CSVSchema(**schema_dict)
21 return {"is_valid": True, "errors": [], "validated_schema": validated_schema}
22 except ValidationError as e:
23 errors = []
24 for error in e.errors():
25 errors.append(
26 {
27 "field": ".".join(str(x) for x in error["loc"]),
28 "message": error["msg"],
29 "type": error["type"],
30 "input": error["input"],
31 }
32 )
34 return {"is_valid": False, "errors": errors, "validated_schema": None}
35 except TypeError as e:
36 return {
37 "is_valid": False,
38 "errors": [{"error_type": "invalid_schema", "error_message": str(e)}],
39 "schema_value": schema_dict,
40 }
41 except Exception as e:
42 return {
43 "is_valid": False,
44 "errors": [{"error_type": "unknown_error", "error_message": str(e)}],
45 "schema_value": schema_dict,
46 "validated_schema": validated_schema,
47 }