data-sitter 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data_sitter/Contract.py +9 -4
- data_sitter/FieldResolver.py +2 -1
- data_sitter/Validation.py +30 -0
- data_sitter/cli.py +1 -1
- data_sitter/field_types/BaseField.py +12 -5
- {data_sitter-0.1.1.dist-info → data_sitter-0.1.3.dist-info}/METADATA +1 -1
- {data_sitter-0.1.1.dist-info → data_sitter-0.1.3.dist-info}/RECORD +10 -9
- {data_sitter-0.1.1.dist-info → data_sitter-0.1.3.dist-info}/WHEEL +0 -0
- {data_sitter-0.1.1.dist-info → data_sitter-0.1.3.dist-info}/entry_points.txt +0 -0
- {data_sitter-0.1.1.dist-info → data_sitter-0.1.3.dist-info}/top_level.txt +0 -0
data_sitter/Contract.py
CHANGED
@@ -3,6 +3,7 @@ from functools import cached_property
|
|
3
3
|
|
4
4
|
from pydantic import BaseModel
|
5
5
|
|
6
|
+
from .Validation import Validation
|
6
7
|
from .field_types import BaseField
|
7
8
|
from .FieldResolver import FieldResolver
|
8
9
|
from .rules import MatchedRule, RuleRegistry, RuleParser
|
@@ -68,10 +69,13 @@ class Contract:
|
|
68
69
|
return rules
|
69
70
|
|
70
71
|
def model_validate(self, item: dict):
|
71
|
-
|
72
|
-
return pydantic_model.model_validate(item).model_dump()
|
72
|
+
return self.pydantic_model.model_validate(item).model_dump()
|
73
73
|
|
74
|
-
def
|
74
|
+
def validate(self, item: dict) -> Validation:
|
75
|
+
return Validation.validate(self.pydantic_model, item)
|
76
|
+
|
77
|
+
@cached_property
|
78
|
+
def pydantic_model(self) -> BaseModel:
|
75
79
|
return type(self.name, (BaseModel,), {
|
76
80
|
"__annotations__": {
|
77
81
|
field_name: field_validator.get_annotation()
|
@@ -97,5 +101,6 @@ class Contract:
|
|
97
101
|
]
|
98
102
|
}
|
99
103
|
for field_name, field_validator in self.field_validators.items()
|
100
|
-
]
|
104
|
+
],
|
105
|
+
"values": self.rule_parser.values
|
101
106
|
}
|
data_sitter/FieldResolver.py
CHANGED
@@ -32,7 +32,8 @@ class FieldResolver:
|
|
32
32
|
return matched_rules
|
33
33
|
|
34
34
|
def get_field_validator(self, field_name: str, parsed_rules: List[str]) -> BaseField:
|
35
|
-
|
35
|
+
is_optional = "Validate Not Null" not in parsed_rules
|
36
|
+
validator = self.field_class(field_name, is_optional)
|
36
37
|
matched_rules = self.get_matched_rules(parsed_rules)
|
37
38
|
for matched_rule in matched_rules:
|
38
39
|
matched_rule.add_to_instance(validator)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
from collections import defaultdict
|
3
|
+
from typing import Any, Dict, List, Type
|
4
|
+
|
5
|
+
from pydantic import BaseModel, ValidationError
|
6
|
+
|
7
|
+
|
8
|
+
class Validation():
|
9
|
+
row: Dict[str, Any]
|
10
|
+
errors: Dict[str, List[str]]
|
11
|
+
|
12
|
+
def __init__(self, row: dict, errors: dict = None):
|
13
|
+
self.row = row
|
14
|
+
self.errors = errors or {}
|
15
|
+
|
16
|
+
def to_dict(self) -> dict:
|
17
|
+
return {"row": self.row, "errors": self.errors}
|
18
|
+
|
19
|
+
@classmethod
|
20
|
+
def validate(cls, model: Type[BaseModel], item: dict) -> "Validation":
|
21
|
+
try:
|
22
|
+
row = model(**item) # Validate the row
|
23
|
+
return Validation(row=row.model_dump())
|
24
|
+
except ValidationError as e:
|
25
|
+
errors = defaultdict(list)
|
26
|
+
for error in e.errors():
|
27
|
+
field = error['loc'][0] # Extract the field name
|
28
|
+
msg = error['msg']
|
29
|
+
errors[field].append(msg)
|
30
|
+
return Validation(row=item, errors=dict(errors))
|
data_sitter/cli.py
CHANGED
@@ -23,7 +23,7 @@ def main():
|
|
23
23
|
contract_path = Path(args.contract)
|
24
24
|
contract_dict = json.loads(contract_path.read_text(encoding))
|
25
25
|
contract = Contract.from_dict(contract_dict)
|
26
|
-
pydantic_contract = contract.
|
26
|
+
pydantic_contract = contract.pydantic_model
|
27
27
|
|
28
28
|
if file_path.suffix == '.csv':
|
29
29
|
with open(file_path, encoding=encoding) as f:
|
@@ -1,12 +1,14 @@
|
|
1
1
|
from abc import ABC
|
2
|
-
from typing import Annotated, List, Type
|
2
|
+
from typing import Annotated, List, Optional, Type
|
3
3
|
|
4
4
|
from pydantic import AfterValidator
|
5
5
|
from ..rules import register_rule, register_field
|
6
6
|
|
7
7
|
|
8
|
-
def aggregated_validator(validators: List[callable]):
|
8
|
+
def aggregated_validator(validators: List[callable], is_optional: bool):
|
9
9
|
def _validator(value):
|
10
|
+
if is_optional and value is None:
|
11
|
+
return value
|
10
12
|
for validator_func in validators:
|
11
13
|
validator_func(value)
|
12
14
|
return value
|
@@ -15,18 +17,22 @@ def aggregated_validator(validators: List[callable]):
|
|
15
17
|
@register_field
|
16
18
|
class BaseField(ABC):
|
17
19
|
name: str
|
20
|
+
is_optional: bool
|
18
21
|
validators = None
|
19
22
|
field_type = None
|
20
23
|
|
21
|
-
def __init__(self, name) -> None:
|
24
|
+
def __init__(self, name: str, is_optional: bool) -> None:
|
22
25
|
self.name = name
|
26
|
+
self.is_optional = is_optional
|
23
27
|
self.validators = []
|
24
28
|
|
25
29
|
@register_rule("Validate Not Null")
|
26
30
|
def validator_not_null(self):
|
27
31
|
def _validator(value):
|
32
|
+
if self.is_optional:
|
33
|
+
return value
|
28
34
|
if value is None:
|
29
|
-
raise ValueError()
|
35
|
+
raise ValueError("Value cannot be null")
|
30
36
|
return value
|
31
37
|
|
32
38
|
self.validators.append(_validator)
|
@@ -36,7 +42,8 @@ class BaseField(ABC):
|
|
36
42
|
validator(value)
|
37
43
|
|
38
44
|
def get_annotation(self):
|
39
|
-
|
45
|
+
field_type = Optional[self.field_type] if self.is_optional else self.field_type
|
46
|
+
return Annotated[field_type, AfterValidator(aggregated_validator(self.validators, self.is_optional))]
|
40
47
|
|
41
48
|
@classmethod
|
42
49
|
def get_parents(cls: Type["BaseField"]) -> List[Type["BaseField"]]:
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: data-sitter
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.3
|
4
4
|
Summary: A Python library that reads data contracts and generates Pydantic models for seamless data validation.
|
5
5
|
Author-email: Lázaro Pereira Candea <lazaro@candea.es>
|
6
6
|
Requires-Dist: python-dotenv==1.0.1
|
@@ -1,8 +1,9 @@
|
|
1
|
-
data_sitter/Contract.py,sha256=
|
2
|
-
data_sitter/FieldResolver.py,sha256=
|
1
|
+
data_sitter/Contract.py,sha256=E3VYrCQZhGk79coHRTV0hCvLAV8uEhYRBMCnCuF_e48,3494
|
2
|
+
data_sitter/FieldResolver.py,sha256=aSavmk3V8QCphLRL6i3T_V2DIsWfEKBdcCnZC71hrx0,1895
|
3
|
+
data_sitter/Validation.py,sha256=MHwPMK06XO9YqdWMQMZ0QSFSk7UfkfmE19TmCTUfi3c,940
|
3
4
|
data_sitter/__init__.py,sha256=qbE-wU8ELMFwOMG4UTK0lmzn5XF2MK3rc22E8ROgypo,113
|
4
|
-
data_sitter/cli.py,sha256=
|
5
|
-
data_sitter/field_types/BaseField.py,sha256=
|
5
|
+
data_sitter/cli.py,sha256=1ICrtokqV5RvvWhzWKAeS5ZUSUpiviQyy2JSK71ER10,1666
|
6
|
+
data_sitter/field_types/BaseField.py,sha256=_Pg6a7gdmQFwb4f7LDyOxElX8j0NnTYZGOJJr_jddt8,1797
|
6
7
|
data_sitter/field_types/FloatField.py,sha256=pWU449uUFzlpnIpZI-2WxN1YKv7PxIiYe_c7W91VqCc,147
|
7
8
|
data_sitter/field_types/IntegerField.py,sha256=o__5z3bg6wsx7FIfJbBYZW5b760-WSZw_05J-OSKXR0,147
|
8
9
|
data_sitter/field_types/NumericField.py,sha256=ncPSrUOOgU5MPcA7in_lR6Luqb1xOo8m7UL6TW1qqGw,2665
|
@@ -18,8 +19,8 @@ data_sitter/rules/Parser/alias_parameters_parser.py,sha256=jsx_JWzkA4lY2nq4hzc4f
|
|
18
19
|
data_sitter/rules/Parser/parser_utils.py,sha256=ypI021uYJTsHAoKGShAfnhd5xQGtqqTGTHozleefsLQ,642
|
19
20
|
data_sitter/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
20
21
|
data_sitter/utils/logger_config.py,sha256=w9E4jWfGJnkC9tZz4qrolSqglKm4jEB8l6vjC-qfj8A,1215
|
21
|
-
data_sitter-0.1.
|
22
|
-
data_sitter-0.1.
|
23
|
-
data_sitter-0.1.
|
24
|
-
data_sitter-0.1.
|
25
|
-
data_sitter-0.1.
|
22
|
+
data_sitter-0.1.3.dist-info/METADATA,sha256=cKlUMxk_rNht0G8bHiotyRn7w6Ho6Vila8-0N7yjVhs,324
|
23
|
+
data_sitter-0.1.3.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
|
24
|
+
data_sitter-0.1.3.dist-info/entry_points.txt,sha256=1I7xxqFZvA78wmDx7NGavttAb8JFWM3Wxgehftx_5C4,53
|
25
|
+
data_sitter-0.1.3.dist-info/top_level.txt,sha256=Q7N21PYeqIdRbDvZQCJXhbbv0PFIf876gu1_DpInH_E,12
|
26
|
+
data_sitter-0.1.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|