-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: adiciona método _validate_digit_verify
- Loading branch information
1 parent
30516e7
commit 8fcc817
Showing
5 changed files
with
89 additions
and
10 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
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,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 |
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,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.
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
def test_version() -> None: | ||
assert __version__ == "0.6.0" | ||
assert __version__ == "0.7.0" |