data-sitter 0.1.2__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 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
- pydantic_model = self.get_pydantic_model()
72
- return pydantic_model.model_validate(item).model_dump()
72
+ return self.pydantic_model.model_validate(item).model_dump()
73
73
 
74
- def get_pydantic_model(self) -> BaseModel:
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()
@@ -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.get_pydantic_model()
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,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: data-sitter
3
- Version: 0.1.2
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,7 +1,8 @@
1
- data_sitter/Contract.py,sha256=87GcIltT0ofhZNZg_de8DP4rTj9Ab2pvGr-HSVACrfs,3375
1
+ data_sitter/Contract.py,sha256=E3VYrCQZhGk79coHRTV0hCvLAV8uEhYRBMCnCuF_e48,3494
2
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=J_FzO8qnF5AVS4PH2AY5ljs4IpCrIg2l2z6PpFsjjWk,1672
5
+ data_sitter/cli.py,sha256=1ICrtokqV5RvvWhzWKAeS5ZUSUpiviQyy2JSK71ER10,1666
5
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
@@ -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.2.dist-info/METADATA,sha256=bh7X-lbFECJk7uV1oT5miVBOVfypkYAERAS6wbAZ7r8,324
22
- data_sitter-0.1.2.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
23
- data_sitter-0.1.2.dist-info/entry_points.txt,sha256=1I7xxqFZvA78wmDx7NGavttAb8JFWM3Wxgehftx_5C4,53
24
- data_sitter-0.1.2.dist-info/top_level.txt,sha256=Q7N21PYeqIdRbDvZQCJXhbbv0PFIf876gu1_DpInH_E,12
25
- data_sitter-0.1.2.dist-info/RECORD,,
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,,