Skip to content

Commit

Permalink
fix: adiciona método _validate_digit_verify
Browse files Browse the repository at this point in the history
  • Loading branch information
hudsonbrendon committed May 1, 2024
1 parent 30516e7 commit 8fcc817
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
14 changes: 5 additions & 9 deletions pydantic_br_validator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

from .field_erros import * # noqa

__version__ = "0.6.0"
__version__ = "0.7.0"


if TYPE_CHECKING:
CPF = str
CNH = str
CPFMask = str
CPFDigits = str
RG = str
RGMask = str
RGDigits = str
CNPJ = str
CNPJDigits = str
CNPJMask = str
TE = str
PIS = str
PISMask = str
PISDigits = str
Certidao = str
CertidaoMask = str
CertidaoDigits = str
CEP = str
CEPMask = str
CEPDigits = str
else:
from .fields.cnpj_field import * # noqa
from .fields.cpf_field import * # noqa
from .fields.cep_field import * # noqa
from .fields.rg_field import * # noqa
45 changes: 45 additions & 0 deletions pydantic_br_validator/fields/rg_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from ..validators.rg_validator import RGValidator
from .base_field import Base, BaseDigits, BaseMask

__all__ = [
"RG",
"RGMask",
"RGDigits",
]


class RG(Base):
"""
Accepts string of RG with or without mask.
Attributes:
number (str): RG number.
"""

format = "rg"
Validator = RGValidator


class RGMask(BaseMask):
"""
Only Accepts string of RG with mask.
Attributes:
number (str): RG number.
"""

format = "rg mask"
Validator = RGValidator


class RGDigits(BaseDigits):
"""
Only Accepts string of RG with digits.
Attributes:
number (str): RG number.
"""

format = "rg digits"
Validator = RGValidator
38 changes: 38 additions & 0 deletions pydantic_br_validator/validators/rg_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from .base_validator import FieldMaskValidator

__all__ = ["RGValidator"]


class RGValidator(FieldMaskValidator):
def __init__(self, rg: str) -> None:
self.rg = rg

def validate_mask(self) -> bool:
"""Valida se o RG tem a máscara correta (XX.XXX.XXX-X)."""
parts = self.rg.split(".")
if len(parts) != 3:
return False
if not all(len(part) == 3 for part in parts[:2]) or len(parts[2]) != 3:
return False
if parts[2][2] != "-":
return False
return all(part.replace("-", "").isdigit() for part in parts)

def validate(self) -> bool:
"""Valida se o RG tem 9 caracteres numéricos, permitindo máscara."""
rg_clean = self.rg.replace(".", "").replace("-", "")
if len(rg_clean) != 9:
return False
if not rg_clean.isdigit():
return False
return self.validate_mask()

def _validate_digit_verify(self, rg: str) -> str:
"""Valida o dígito verificador do RG."""
sum = 0
for i in range(9, 1, -1):
sum += int(rg[9 - i]) * i
sum = sum % 11
if sum == 10:
sum = 0
return str(sum)
Empty file added tests/test_rg.py
Empty file.
2 changes: 1 addition & 1 deletion tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version() -> None:
assert __version__ == "0.6.0"
assert __version__ == "0.7.0"

0 comments on commit 8fcc817

Please sign in to comment.