-
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 to use base field class
- Loading branch information
Showing
8 changed files
with
53 additions
and
213 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,46 +1,16 @@ | ||
from typing import Any, Callable, Dict, Generator | ||
|
||
from ..field_erros import FieldDigitError, FieldInvalidError, FieldTypeError | ||
from ..validators.cnh_validator import CNHValidator | ||
from .base_field import BaseDigits | ||
|
||
__all__ = ["CNH"] | ||
|
||
|
||
AnyCallable = Callable[..., Any] | ||
CallableGenerator = Generator[AnyCallable, None, None] | ||
|
||
|
||
class CNH(str): | ||
__slots__ = ["number"] | ||
|
||
def __init__(self, number: str) -> None: | ||
self.number = number | ||
|
||
@classmethod | ||
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None: | ||
field_schema.update(type="string", format="cnh") | ||
|
||
@classmethod | ||
def __get_validators__(cls) -> CallableGenerator: | ||
yield cls.validate_type | ||
yield cls.validate_numbers | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate_type(cls, value: str) -> str: | ||
if not isinstance(value, str): | ||
raise FieldTypeError() | ||
return value | ||
class CNH(BaseDigits): | ||
""" | ||
Only Accepts string of CNH with digits. | ||
@classmethod | ||
def validate_numbers(cls, value: str) -> str: | ||
if not value.isdigit(): | ||
raise FieldDigitError() | ||
return value | ||
Attributes: | ||
number (str): CNH number. | ||
""" | ||
|
||
@classmethod | ||
def validate(cls, value: str) -> str: | ||
cnh = CNHValidator(value) | ||
if not cnh.validate(): | ||
raise FieldInvalidError() | ||
return value | ||
format = "cnh" | ||
Validator = CNHValidator |
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 |
---|---|---|
@@ -1,99 +1,45 @@ | ||
from typing import Any, Callable, Dict, Generator | ||
|
||
from ..field_erros import ( | ||
FieldDigitError, | ||
FieldInvalidError, | ||
FieldMaskError, | ||
FieldTypeError, | ||
) | ||
from ..validators.cpf_validator import CPFValidator | ||
from .base_field import Base, BaseDigits, BaseMask | ||
|
||
__all__ = [ | ||
"CPF", | ||
"CPFMask", | ||
"CPFDigits", | ||
] | ||
|
||
AnyCallable = Callable[..., Any] | ||
CallableGenerator = Generator[AnyCallable, None, None] | ||
|
||
|
||
class CPFBase(str): | ||
__slots__ = ["number"] | ||
|
||
def __init__(self, number: str) -> None: | ||
self.number = number | ||
|
||
@classmethod | ||
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None: | ||
field_schema.update(type="string", format="cpf") | ||
|
||
@classmethod | ||
def validate_type(cls, value: str) -> str: | ||
if not isinstance(value, str): | ||
raise FieldTypeError() | ||
return value | ||
|
||
@classmethod | ||
def validate(cls, value: str) -> str: | ||
cpf = CPFValidator(value) | ||
if not cpf.validate(): | ||
raise FieldInvalidError() | ||
return value | ||
|
||
|
||
class CPF(CPFBase): | ||
class CPF(Base): | ||
""" | ||
Accepts string of CPF with or without mask. | ||
Attributes: | ||
number (str): CPF number. | ||
""" | ||
|
||
@classmethod | ||
def __get_validators__(cls) -> CallableGenerator: | ||
yield cls.validate_type | ||
yield cls.validate | ||
format = "cpf" | ||
Validator = CPFValidator | ||
|
||
|
||
class CPFMask(CPFBase): | ||
class CPFMask(BaseMask): | ||
""" | ||
Only Accepts string of CPF with mask. | ||
Attributes: | ||
number (str): CPF number. | ||
""" | ||
|
||
@classmethod | ||
def __get_validators__(cls) -> CallableGenerator: | ||
yield cls.validate_type | ||
yield cls.validate_mask | ||
yield cls.validate | ||
format = "cpf mask" | ||
Validator = CPFValidator | ||
|
||
@classmethod | ||
def validate_mask(cls, value: str) -> str: | ||
cpf = CPFValidator(value) | ||
if not cpf.validate_mask(): | ||
raise FieldMaskError() | ||
return value | ||
|
||
|
||
class CPFDigits(CPFBase): | ||
class CPFDigits(BaseDigits): | ||
""" | ||
Only Accepts string of CPF with digits. | ||
Attributes: | ||
number (str): CPF number. | ||
""" | ||
|
||
@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 FieldDigitError() | ||
return value | ||
format = "cpf digits" | ||
Validator = CPFValidator |
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,46 +1,17 @@ | ||
from typing import Any, Callable, Dict, Generator | ||
|
||
from ..field_erros import FieldDigitError, FieldInvalidError, FieldTypeError | ||
from ..validators.te_validator import TEValidator | ||
from .base_field import BaseDigits | ||
|
||
__all__ = ["TE"] | ||
|
||
|
||
AnyCallable = Callable[..., Any] | ||
CallableGenerator = Generator[AnyCallable, None, None] | ||
|
||
|
||
class TE(str): | ||
__slots__ = ["number"] | ||
|
||
def __init__(self, number: str) -> None: | ||
self.number = number | ||
|
||
@classmethod | ||
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None: | ||
field_schema.update(type="string", format="te") | ||
|
||
@classmethod | ||
def __get_validators__(cls) -> CallableGenerator: | ||
yield cls.validate_type | ||
yield cls.validate_numbers | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate_type(cls, value: str) -> str: | ||
if not isinstance(value, str): | ||
raise FieldTypeError() | ||
return value | ||
class TE(BaseDigits): | ||
format = "te" | ||
Validator = TEValidator | ||
""" | ||
Only Accepts string of TE with digits. | ||
@classmethod | ||
def validate_numbers(cls, value: str) -> str: | ||
if not value.isdigit(): | ||
raise FieldDigitError() | ||
return value | ||
Attributes: | ||
number (str): TE number. | ||
""" | ||
|
||
@classmethod | ||
def validate(cls, value: str) -> str: | ||
te = TEValidator(value) | ||
if not te.validate(): | ||
raise FieldInvalidError() | ||
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
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