Skip to content

Commit

Permalink
feat: new validator CERT a.k.a certidao de casamento, nascimento e obito
Browse files Browse the repository at this point in the history
Took 1 hour 11 minutes
  • Loading branch information
vinicius-oa committed Feb 27, 2024
1 parent aba7219 commit 463db12
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 20 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ pip install brdocs-validation

## Supported docs and its formats

| Supports | Description | Format | Format's support |
|:-----------------:|:----------------------------------------:|:-----------------------------------------------:|:--------------------:|
| CNPJ | | *12.345.678/9012-34* OR _Without special chars_ | |
| CPF | | *123.456.789-01* OR _Without special chars_ | |
| CNH | | Only numbers | Length: _11_ |
| NIS/PIS/PASEP/NIT | Use _**NIS**_ type for _PIS, PASEP, NIT_ | *123.45678.90-1* OR _Only numbers_ | |
| CNS | Cartão Nacional de Saúde | Only numbers | |
| RENAVAM | | Only numbers | Length: _9, 10 & 11_ |
| TE | Título de eleitor | Only numbers | |
| Supports | Description | Format | Format's support |
|:-----------------:|:-----------------------------------------:|:-----------------------------------------------:|:--------------------:|
| CNPJ | | *12.345.678/9012-34* OR _Without special chars_ | |
| CPF | | *123.456.789-01* OR _Without special chars_ | |
| CNH | | Only numbers | Length: _11_ |
| NIS/PIS/PASEP/NIT | Use _**NIS**_ type for _PIS, PASEP, NIT_ | *123.45678.90-1* OR _Only numbers_ | |
| CNS | Cartão Nacional de Saúde | Only numbers | |
| RENAVAM | | Only numbers | Length: _9, 10 & 11_ |
| TE | Título de eleitor | Only numbers | |
| CERT | Certidão de casamento, nascimento e óbito | Only numbers | |
## Usage

```python
from br_docs import CNPJ, CPF, CNH, NIS, CNS, RENAVAM, TE
from br_docs import CNPJ, CPF, CNH, NIS, CNS, RENAVAM, TE, CERT
from pydantic import BaseModel


Expand All @@ -36,4 +37,5 @@ class User(BaseModel):
cns: CNS
renavam: RENAVAM
te: TE
cert: CERT
```
3 changes: 3 additions & 0 deletions br_docs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydantic.functional_validators import AfterValidator
from typing_extensions import Annotated

from br_docs.validators.cert import CERTv
from br_docs.validators.cnh import CNHv
from br_docs.validators.cnpj import CNPJv
from br_docs.validators.cns import CNSv
Expand All @@ -9,10 +10,12 @@
from br_docs.validators.renavam import RENAVAMv
from br_docs.validators.te import TEv


CPF = Annotated[str, AfterValidator(CPFv())]
CNPJ = Annotated[str, AfterValidator(CNPJv())]
CNH = Annotated[str, AfterValidator(CNHv())]
NIS = Annotated[str, AfterValidator(NISv())]
CNS = Annotated[str, AfterValidator(CNSv())]
RENAVAM = Annotated[str, AfterValidator(RENAVAMv())]
TE = Annotated[str, AfterValidator(TEv())]
CERT = Annotated[str, AfterValidator(CERTv())]
26 changes: 26 additions & 0 deletions br_docs/validators/cert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import re

from br_docs.validators import CheckDigits


class CERTv(CheckDigits):
""" Certidões de casamento, nascimento e óbito """
Patterns = re.compile(r"^\d{32}$"),
CHECK_DIGITS = 2
CertAlgarismsMultipliers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

@classmethod
def calculate_digits(cls, non_digits: list[int]) -> tuple[int, ...]:
""" https://web.archive.org/web/20240227052915/http://ghiorzi.org/DVnew.htm#zc """
def sum_(slice_: int = 1, value: int | None = None) -> int:
numbers = non_digits
if value is not None:
numbers.append(value)
digit = sum(x * y for x, y in zip(numbers, cls.CertAlgarismsMultipliers[slice_:])) % 11
if digit == 10:
return 1
return digit

digit_one = sum_()
digit_two = sum_(0, digit_one)
return digit_one, digit_two
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version = "0.3.0"
dependencies = [
"pydantic>=2.0"
]
requires-python = ">= 3.9"
requires-python = ">= 3.10"
authors = [
{name = "Vinícius Aguiar", email = "vaguiararqdevsoftware@proton.me"},
]
Expand All @@ -17,15 +17,14 @@ maintainers = [
]
description = "Validate brazilian documents using Type Hints in classes inheriting Pydantic's (V2) BaseModel"
readme = "README.md"
keywords = ["pydantic-v2", "cpf-validador", "cnpj-validador", "validador-pispasep"]
keywords = ["pydantic-v2", "cpf-validador", "cnpj-validador", "validador-pispasep", "validador-titulo-eleitor"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Framework :: Pydantic :: 2",
"Intended Audience :: Developers",
"Topic :: Software Development :: Build Tools",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand All @@ -52,7 +51,7 @@ legacy_tox_ini = """
[tox]
requires =
tox>=4
env_list = py{39,310,311,312}-pydantic{20,21,22,23,24,25,26}
env_list = py{310,311,312}-pydantic{20,21,22,23,24,25,26}
set_env =
TEMP = {env_tmp_dir}
Expand All @@ -69,5 +68,4 @@ python =
3.12 = py312-pydantic{20,21,22,23,24,25,26}
3.11 = py311-pydantic{20,21,22,23,24,25,26}
3.10 = py310-pydantic{20,21,22,23,24,25,26}
3.9 = py39-pydantic{20,21,22,23,24,25,26}
"""
65 changes: 65 additions & 0 deletions tests/cert.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
06630801551976152186257001464831
11893501551995100018157000194796
00540501552011300007001000212795
11518801551992100063003007290959
09431801552020200008124000171688
09988701551933100057269000007981
07988901552012100432027016435121
02094101551917100003167000013291
05320701551996100018146000251915
12302601551986100218063008457637
14806401551992100242009020197007
02173301552023100230001008758091
12989001551979200006090000078705
12989001551979200006090000078705
09314601552014101262049026033793
00414301551951100094179000042813
14806401551988100155119011933226
11507101551941100024075002885914
11854701551959100014196000657983
12302601552022100699001037221050
00511601552008100508243024679536
10628601552022100036228000780711
03103901552011100141075013361911
00507401552013100212051006335103
09897001552018100026148001048108
10632801551957100039401000933535
10110501552020100035175000527205
09982001551968100055149004215046
10632801552018400017125000145808
08244601552011100361085009068751
11084101552014100031121000828411
06766001551961100056009001240430
01750901552022100022128002318451
02929801552016100017146000671211
05992301552008100089241003692754
12269701551985100330272017206834
06440201552001100026046001384534
13680401552012100037103005765705
13680401552012100037103005765705
09982001551968100055149004215046
10985001551998100054233002230713
10481001551994100146019008037524
09989501552003100064331003358656
12813201552011100027121002528590
11537801552014100047245002053480
10659101552019100143152003934205
09908501552023100291029007236946
10812601552021100191235006998164
11898401551965100016226000485689
03162501552014100039265005149694
13556601552004100005107000217894
05804001552009200325011009161864
09988701551933100057269000007981
05804001552001100736153040330687
09854101552006100040019001505212
11531101552018100277268015194714
10628601552016100034065000704881
09453201552019400015213000402315
12813201552011100027121002528590
10812601552018400054094001918799
07204101552014200074104002465975
11084101552014100031156000831913
03298701551993100007215000465656
02983501552002100089142006134657
05804001552009200325011009161864
14 changes: 10 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def cnpj_list():
return open_valid_docs('cnpj.txt')


@pytest.fixture()
@pytest.fixture
def nis_list():
"""
1-56 line: https://web.archive.org/web/20240225200956/http://www.varzeagrande.mt.gov.br/storage/Anexos/18cd1d8751995dccb6116b97ab9e0ce7.pdf
Expand All @@ -46,7 +46,7 @@ def nis_list():
return open_valid_docs('nis.txt')


@pytest.fixture()
@pytest.fixture
def cns_list():
"""
1-61 line: https://web.archive.org/web/20240226014647/https://simaodias.se.gov.br/sites/simaodias.se.gov.br/files/LISTA%20VACINADOS%20D2%20IDOSOS%20-%2010032021%20-%20FORMULARIO.pdf
Expand All @@ -55,7 +55,7 @@ def cns_list():
return open_valid_docs('cns.txt')


@pytest.fixture()
@pytest.fixture
def renavam_list():
"""
1-173 line: https://web.archive.org/web/20240226050829/https://www.euamoleilao.com.br/imprimir/0067-leilao-do-detran-de-sao-paulo
Expand All @@ -65,9 +65,15 @@ def renavam_list():
return open_valid_docs('renavam.txt')


@pytest.fixture()
@pytest.fixture
def te_list():
"""
1-159 line: https://web.archive.org/web/20240226104403/https://issuu.com/psol.df/docs/lista_de_filiados_ao_psol_df___dist
"""
return open_valid_docs('te.txt')


@pytest.fixture
def cert_list():
""" google dorks: intext:"DATA DE NASCIMENTO (POR EXTENSO)" AND intext:"CERTIDÃO DE <CASAMENTO|ÓBITO|NASCIMENTO>" AND intext:"matricula" AND ext:pdf """
return open_valid_docs('cert.txt')
7 changes: 6 additions & 1 deletion tests/test_brdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pydantic import create_model
from pydantic_core import ValidationError

from br_docs import CNH, CPF, CNPJ, NIS, CNS, RENAVAM, TE
from br_docs import CNH, CPF, CNPJ, NIS, CNS, RENAVAM, TE, CERT


@contextmanager
Expand Down Expand Up @@ -53,3 +53,8 @@ def test_renavam(renavam_list):
def test_te(te_list):
with validate(model_name='TestTE', values=te_list, value_type=TE):
pass


def test_cert(cert_list):
with validate(model_name='TestCERT', values=cert_list, value_type=CERT):
pass

0 comments on commit 463db12

Please sign in to comment.