Skip to content

Commit

Permalink
refactor: change struct of base_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
scjorge committed Jul 31, 2024
1 parent a8e0164 commit 41401ba
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 97 deletions.
80 changes: 10 additions & 70 deletions pydantic_br/fields/base_field.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,14 @@
from typing import Any, Callable, Dict, Generator
from ..get_versions import get_pydantic_version

from ..field_errors import FieldTypes, raise_field
from ..validators.base_validator import FieldMaskValidator, FieldValidator
from .base_field_v2 import BaseDigitsV2, BaseMaskV2, BasePydanticV2
pydantic_version = get_pydantic_version()

__all__ = [
"Base",
"BaseMask",
"BaseDigits",
]

AnyCallable = Callable[..., Any]
CallableGenerator = Generator[AnyCallable, None, None]
if pydantic_version.value == 1:
from .base_field_v1 import BaseDigitsv1 as BaseDigits # noqa
from .base_field_v1 import BaseMaskv1 as BaseMask # noqa
from .base_field_v1 import Basev1 as Base # noqa


class Base(BasePydanticV2):
format: str
Validator: Callable[..., FieldValidator]

__slots__ = ["number"]

@classmethod
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
field_schema.update(type="string", format=cls.format)

@classmethod
def __get_validators__(cls) -> CallableGenerator:
yield cls.validate_type
yield cls.validate

@classmethod
def validate_type(cls, value: Any) -> str:
if not isinstance(value, str):
raise_field(FieldTypes.type)
return value

@classmethod
def validate(cls, value: str) -> str:
doc = cls.Validator(value)
if not doc.validate():
raise_field(FieldTypes.invalid)
return value


class BaseMask(Base, BaseMaskV2):
Validator: Callable[..., FieldMaskValidator]

@classmethod
def __get_validators__(cls) -> CallableGenerator:
yield cls.validate_type
yield cls.validate_mask
yield cls.validate

@classmethod
def validate_mask(cls, value: str) -> str:
doc = cls.Validator(value)
if not doc.validate_mask():
raise_field(FieldTypes.mask)
return value


class BaseDigits(Base, BaseDigitsV2):
@classmethod
def __get_validators__(cls) -> CallableGenerator:
yield cls.validate_type
yield cls.validate_numbers
yield cls.validate

@classmethod
def validate_numbers(cls, value: str) -> str:
if not value.isdigit():
raise_field(FieldTypes.digit)
return value
if pydantic_version.value == 2:
from .base_field_v2 import BaseDigitsV2 as BaseDigits # noqa
from .base_field_v2 import BaseMaskV2 as BaseMask # noqa
from .base_field_v2 import BaseV2 as Base # noqa
42 changes: 42 additions & 0 deletions pydantic_br/fields/base_field_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Any, Callable, Generator

from ..field_errors import FieldTypes, raise_field

__all__ = ["BaseFieldClass"]


AnyCallable = Callable[..., Any]
CallableGenerator = Generator[AnyCallable, None, None]


class BaseFieldClass:
format: str
Validator: Callable[..., Any]

__slots__ = ["number"]

@classmethod
def validate_type(cls, value: Any) -> str:
if not isinstance(value, str):
raise_field(FieldTypes.type)
return value

@classmethod
def validate(cls, value: str) -> str:
doc = cls.Validator(value)
if not doc.validate():
raise_field(FieldTypes.invalid)
return value

@classmethod
def validate_mask(cls, value: str) -> str:
doc = cls.Validator(value)
if not doc.validate_mask():
raise_field(FieldTypes.mask)
return value

@classmethod
def validate_numbers(cls, value: str) -> str:
if not value.isdigit():
raise_field(FieldTypes.digit)
return value
44 changes: 44 additions & 0 deletions pydantic_br/fields/base_field_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Any, Callable, Dict, Generator

from ..validators.base_validator import FieldMaskValidator, FieldValidator
from .base_field_class import BaseFieldClass

__all__ = [
"Basev1",
"BaseMaskv1",
"BaseDigitsv1",
]

AnyCallable = Callable[..., Any]
CallableGenerator = Generator[AnyCallable, None, None]


class Basev1(BaseFieldClass):
Validator: Callable[..., FieldValidator]

@classmethod
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
field_schema.update(type="string", format=cls.format)

@classmethod
def __get_validators__(cls) -> CallableGenerator:
yield cls.validate_type
yield cls.validate


class BaseMaskv1(Basev1):
Validator: Callable[..., FieldMaskValidator]

@classmethod
def __get_validators__(cls) -> CallableGenerator:
yield cls.validate_type
yield cls.validate_mask
yield cls.validate


class BaseDigitsv1(Basev1):
@classmethod
def __get_validators__(cls) -> CallableGenerator:
yield cls.validate_type
yield cls.validate_numbers
yield cls.validate
34 changes: 8 additions & 26 deletions pydantic_br/fields/base_field_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,21 @@
from typing import Any, Dict, Mapping

from ..get_versions import get_pydantic_version
from .base_field_class import BaseFieldClass

pydantic_version = get_pydantic_version()


if pydantic_version.value == 2:
try:
from pydantic_core import core_schema # type: ignore
except ModuleNotFoundError:
raise ModuleNotFoundError("Are you sure you installed pydantic_core")
try:
from pydantic_core import core_schema # type: ignore
except ModuleNotFoundError:
raise ModuleNotFoundError("Are you sure you installed pydantic_core")

CoreSchema: Any = Mapping[str, Any]
JsonSchemaValue = Dict[str, Any]


class BasePydanticV2:
format = "generic"

@classmethod
def validate_type(cls, value: Any):
...

@classmethod
def validate(cls, value: str):
...

@classmethod
def validate_mask(cls, value: str):
...

@classmethod
def validate_numbers(cls, value: str):
...

class BaseV2(BaseFieldClass):
@classmethod
def __get_pydantic_core_schema__(
cls,
Expand Down Expand Up @@ -71,15 +53,15 @@ def _validate(cls, __input_value: str, *args, **kwargs) -> str:
return cls.validate(__input_value)


class BaseMaskV2(BasePydanticV2):
class BaseMaskV2(BaseV2):
@classmethod
def _validate(cls, __input_value: str, *args, **kwargs) -> str:
cls.validate_type(__input_value)
cls.validate_mask(__input_value)
return cls.validate(__input_value)


class BaseDigitsV2(BasePydanticV2):
class BaseDigitsV2(BaseV2):
@classmethod
def _validate(cls, __input_value: str, *args, **kwargs) -> str:
cls.validate_type(__input_value)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ profile = "black"
profile = "black"

[tool.mypy]
exclude = 'pydantic_br/field_errors.py'
exclude = ['pydantic_br/field_errors.py', 'pydantic_br/fields/base_field.py']

[tool.taskipy.tasks]
lint = "black pydantic_br && isort pydantic_br && mypy pydantic_br && flake8 pydantic_br"
Expand Down

0 comments on commit 41401ba

Please sign in to comment.