Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adiciona verificação de RG #4

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry config ${{ secrets.PYPI_SECRET_KEY }}
- name: Check Package
run: poetry check
- name: Build Package
run: poetry build
- name: Publish Package
run: poetry publish
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Pydantic BR Validator
# Pydantic BR Validator 🇧🇷

<p align="center">
<a href="https://sqlmodel.tiangolo.com"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Flag_of_Brazil.svg/1600px-Flag_of_Brazil.svg.png" alt="Pydantic BR Validator"></a>
</p>
<p align="center">
<em>Uma biblioteca python com modelos de validação para os principais documentos brasileiros.</em>
</p>
Expand Down Expand Up @@ -39,7 +36,7 @@ pip install pydantic-br-validator
- [ ] Renavam
- [ ] Placa
- [ ] ISBN
- [ ] CEP
- [x] CEP

# Exemplos

Expand Down
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.4.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)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydantic-br-validator"
version = "0.4.0"
version = "0.6.0"
description = "Uma biblioteca python com modelos de validação para os principais documentos brasileiros"
authors = ["Hudson Brendon <[email protected]>"]
readme = "README.md"
Expand Down
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.4.0"
assert __version__ == "0.7.0"
Loading