-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Coerce and encode enum types * Add pydantic validator step * Test enum coercion * Use isinstance in pipe.find to support finding subclasses * Wrap pydantic ValidationError * Test extract validation * Pydantic json type, test special string types * Validate lists * Pydantic skip Any fields * Move validator base class * Subclass validator from ItemTransform * Imrpvoe validation error message * Cleanup
- Loading branch information
Showing
13 changed files
with
396 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Optional, Protocol, TypeVar, Generic, Type, Union, Any, List | ||
|
||
try: | ||
from pydantic import BaseModel as PydanticBaseModel, ValidationError as PydanticValidationError, create_model | ||
except ModuleNotFoundError: | ||
PydanticBaseModel = None # type: ignore[misc] | ||
|
||
from dlt.extract.exceptions import ValidationError | ||
from dlt.common.typing import TDataItems | ||
from dlt.common.schema.typing import TAnySchemaColumns | ||
from dlt.extract.typing import TTableHintTemplate, ValidateItem | ||
|
||
|
||
_TPydanticModel = TypeVar("_TPydanticModel", bound=PydanticBaseModel) | ||
|
||
|
||
class PydanticValidator(ValidateItem, Generic[_TPydanticModel]): | ||
model: Type[_TPydanticModel] | ||
def __init__(self, model: Type[_TPydanticModel]) -> None: | ||
self.model = model | ||
|
||
# Create a model for validating list of items in batch | ||
self.list_model = create_model( | ||
"List" + model.__name__, | ||
items=(List[model], ...) # type: ignore[valid-type] | ||
) | ||
|
||
def __call__(self, item: TDataItems, meta: Any = None) -> Union[_TPydanticModel, List[_TPydanticModel]]: | ||
"""Validate a data item against the pydantic model""" | ||
if item is None: | ||
return None | ||
try: | ||
if isinstance(item, list): | ||
return self.list_model(items=item).items # type: ignore[attr-defined, no-any-return] | ||
return self.model.parse_obj(item) | ||
except PydanticValidationError as e: | ||
raise ValidationError(self, item, e) from e | ||
|
||
def __str__(self, *args: Any, **kwargs: Any) -> str: | ||
return f"PydanticValidator(model={self.model.__qualname__})" | ||
|
||
|
||
def get_column_validator(columns: TTableHintTemplate[TAnySchemaColumns]) -> Optional[ValidateItem]: | ||
if PydanticBaseModel is not None and isinstance(columns, type) and issubclass(columns, PydanticBaseModel): | ||
return PydanticValidator(columns) | ||
return None |
Oops, something went wrong.