Skip to content

Commit

Permalink
refactor: change to use base field class
Browse files Browse the repository at this point in the history
  • Loading branch information
scjorge committed Apr 22, 2023
1 parent 61db39b commit a57b583
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 213 deletions.
48 changes: 9 additions & 39 deletions pydantic_br/fields/cnh_field.py
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
77 changes: 11 additions & 66 deletions pydantic_br/fields/cnpj_field.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -15,86 +8,38 @@
]


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.
Attributes:
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.
Attributes:
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
76 changes: 11 additions & 65 deletions pydantic_br/fields/cpf_field.py
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
49 changes: 10 additions & 39 deletions pydantic_br/fields/te_field.py
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
...
4 changes: 3 additions & 1 deletion pydantic_br/validators/cnh_validator.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion pydantic_br/validators/cnpj_validator.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion pydantic_br/validators/cpf_validator.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 3 additions & 1 deletion pydantic_br/validators/te_validator.py
Original file line number Diff line number Diff line change
@@ -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))
Expand Down

0 comments on commit a57b583

Please sign in to comment.