From a57b5837aca6398c6a801a71b8c3f7e3bedb971c Mon Sep 17 00:00:00 2001 From: Jorge Silva Date: Fri, 21 Apr 2023 23:55:47 -0300 Subject: [PATCH] refactor: change to use base field class --- pydantic_br/fields/cnh_field.py | 48 +++------------ pydantic_br/fields/cnpj_field.py | 77 ++++-------------------- pydantic_br/fields/cpf_field.py | 76 ++++------------------- pydantic_br/fields/te_field.py | 49 +++------------ pydantic_br/validators/cnh_validator.py | 4 +- pydantic_br/validators/cnpj_validator.py | 4 +- pydantic_br/validators/cpf_validator.py | 4 +- pydantic_br/validators/te_validator.py | 4 +- 8 files changed, 53 insertions(+), 213 deletions(-) diff --git a/pydantic_br/fields/cnh_field.py b/pydantic_br/fields/cnh_field.py index 81603df..a2ca7b5 100644 --- a/pydantic_br/fields/cnh_field.py +++ b/pydantic_br/fields/cnh_field.py @@ -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 diff --git a/pydantic_br/fields/cnpj_field.py b/pydantic_br/fields/cnpj_field.py index 0c42ca2..694c39b 100644 --- a/pydantic_br/fields/cnpj_field.py +++ b/pydantic_br/fields/cnpj_field.py @@ -1,12 +1,5 @@ -from typing import Any, Callable, Dict, Generator - -from ..field_erros import ( - FieldDigitError, - FieldInvalidError, - FieldMaskError, - FieldTypeError, -) from ..validators.cnpj_validator import CNPJValidator +from .base_field import Base, BaseDigits, BaseMask __all__ = [ "CNPJ", @@ -15,49 +8,20 @@ ] -AnyCallable = Callable[..., Any] -CallableGenerator = Generator[AnyCallable, None, None] - - -class CNPJBase(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="cnpj") - - @classmethod - def validate_type(cls, value: str) -> str: - if not isinstance(value, str): - raise FieldTypeError() - return value - - @classmethod - def validate(cls, value: str) -> str: - cnpj = CNPJValidator(value) - if not cnpj.validate(): - raise FieldInvalidError() - return value - - -class CNPJ(CNPJBase): +class CNPJ(Base): """ Accepts string of CNPJ with or without mask. Attributes: number (str): CNPJ number. + """ - @classmethod - def __get_validators__(cls) -> CallableGenerator: - yield cls.validate_type - yield cls.validate + format = "cnpj" + Validator = CNPJValidator -class CNPJMask(CNPJBase): +class CNPJMask(BaseMask): """ Only Accepts string of CNPJ with mask. @@ -65,21 +29,11 @@ class CNPJMask(CNPJBase): number (str): CNPJ number. """ - @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: - cnpj = CNPJValidator(value) - if not cnpj.validate_mask(): - raise FieldMaskError() - return value + format = "cnpj" + Validator = CNPJValidator -class CNPJDigits(CNPJBase): +class CNPJDigits(BaseDigits): """ Only Accepts string of CNPJ with digits. @@ -87,14 +41,5 @@ class CNPJDigits(CNPJBase): number (str): CNPJ 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 = "cnpj" + Validator = CNPJValidator diff --git a/pydantic_br/fields/cpf_field.py b/pydantic_br/fields/cpf_field.py index c5ac161..f0e5537 100644 --- a/pydantic_br/fields/cpf_field.py +++ b/pydantic_br/fields/cpf_field.py @@ -1,12 +1,5 @@ -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", @@ -14,49 +7,21 @@ "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. @@ -64,21 +29,11 @@ class CPFMask(CPFBase): 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. @@ -86,14 +41,5 @@ class CPFDigits(CPFBase): 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 diff --git a/pydantic_br/fields/te_field.py b/pydantic_br/fields/te_field.py index 519e5e8..b9769de 100644 --- a/pydantic_br/fields/te_field.py +++ b/pydantic_br/fields/te_field.py @@ -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 + ... diff --git a/pydantic_br/validators/cnh_validator.py b/pydantic_br/validators/cnh_validator.py index f6a6866..26e7b91 100644 --- a/pydantic_br/validators/cnh_validator.py +++ b/pydantic_br/validators/cnh_validator.py @@ -1,9 +1,11 @@ import re +from .base_validator import FieldValidator + __all__ = ["CNHValidator"] -class CNHValidator: +class CNHValidator(FieldValidator): def __init__(self, cnh: str) -> None: self.cnh = cnh diff --git a/pydantic_br/validators/cnpj_validator.py b/pydantic_br/validators/cnpj_validator.py index d9301d3..88233dd 100644 --- a/pydantic_br/validators/cnpj_validator.py +++ b/pydantic_br/validators/cnpj_validator.py @@ -1,9 +1,11 @@ import re +from .base_validator import FieldMaskValidator + __all__ = ["CNPJValidator"] -class CNPJValidator: +class CNPJValidator(FieldMaskValidator): def __init__(self, cnpj) -> None: self.cnpj = cnpj diff --git a/pydantic_br/validators/cpf_validator.py b/pydantic_br/validators/cpf_validator.py index 0627431..2482102 100644 --- a/pydantic_br/validators/cpf_validator.py +++ b/pydantic_br/validators/cpf_validator.py @@ -1,9 +1,11 @@ import re +from .base_validator import FieldMaskValidator + __all__ = ["CPFValidator"] -class CPFValidator: +class CPFValidator(FieldMaskValidator): def __init__(self, cpf: str) -> None: self.cpf = cpf diff --git a/pydantic_br/validators/te_validator.py b/pydantic_br/validators/te_validator.py index 1a3673d..ea45a9d 100644 --- a/pydantic_br/validators/te_validator.py +++ b/pydantic_br/validators/te_validator.py @@ -1,10 +1,12 @@ import re from typing import List +from .base_validator import FieldValidator + __all__ = ["TEValidator"] -class TEValidator: +class TEValidator(FieldValidator): def __init__(self, te: str) -> None: self.te = te self.first_check_digit_weights = list(range(2, 10))