-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change struct of base_fields
- Loading branch information
Showing
5 changed files
with
105 additions
and
97 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
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 |
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,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 |
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,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 |
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