Skip to content

Commit

Permalink
feat: new validation RENAVAM and CNS
Browse files Browse the repository at this point in the history
Took 4 hours 52 minutes
  • Loading branch information
vinicius-oa committed Feb 26, 2024
1 parent d536f62 commit e15755b
Show file tree
Hide file tree
Showing 10 changed files with 1,046 additions and 14 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Test](https://github.com/vinicius-oa/BRdocs-validation/actions/workflows/test.yml/badge.svg)
![codecov](https://codecov.io/gh/vinicius-oa/BRdocs-validation/graph/badge.svg?token=Z211YIKO8L)
[![PyPI version](https://badge.fury.io/py/brdocs-validation.svg)](https://badge.fury.io/py/brdocs-validation)
![PyPI version](https://badge.fury.io/py/brdocs-validation.svg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/brdocs-validation?color=2334D058)
![Pydantic V2](https://img.shields.io/badge/Pydantic_V2->=2.0-2334D058.svg)

Expand All @@ -12,17 +12,18 @@ pip install brdocs-validation

## Supported docs and its formats

| Supports | Format |
|:-----------------:|:-----------------------------------------------:|
| CNPJ | *12.345.678/9012-34* OR _Without special chars_ |
| CPF | *123.456.789-01* OR _Without special chars_ |
| CNH | Only numbers |
| NIS/PIS/PASEP/NIT | *123.45678.90-1* OR _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_ |
## Usage

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


Expand All @@ -31,4 +32,6 @@ class User(BaseModel):
cnpj: CNPJ
cnh: CNH
nis: NIS
cns: CNS
renavam: RENAVAM
```
5 changes: 4 additions & 1 deletion br_docs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

from br_docs.validators.cnh import CNHv
from br_docs.validators.cnpj import CNPJv
from br_docs.validators.cns import CNSv
from br_docs.validators.cpf import CPFv
from br_docs.validators.nis import NISv

from br_docs.validators.renavam import RENAVAMv

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())]
2 changes: 1 addition & 1 deletion br_docs/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ def validate(self, value: str):

@staticmethod
@abstractmethod
def calculate_digits(non_digits) -> tuple[int, ...]: # pragma: no cover
def calculate_digits(non_digits: list[int]) -> tuple[int, ...]: # pragma: no cover
...
37 changes: 37 additions & 0 deletions br_docs/validators/cns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import re

from pydantic_core import PydanticCustomError

from br_docs.validators import CheckDigits


class CNSv(CheckDigits):
Patterns = re.compile(r"^\d{15}$"),

def validate(self, value: str):
valid_value = self.calculate_digits(list(map(int, value)))[0]
if not valid_value:
raise PydanticCustomError(
'invalid',
'Invalid value'
)

@staticmethod
def calculate_digits(value: list[int]) -> tuple[int]:
""" https://web.archive.org/web/20190106003440/http://cartaonet.datasus.gov.br/Rotina_Java.doc """
def sum_(value_slice=11, range_stop=4):
return sum(int(x) * y for x, y in zip(value[:value_slice], range(15, range_stop, -1)))

if value[0] in (1, 2):
dv = 11 - (sum_() % 11)
if dv == 11:
dv = 0
elif dv == 10:
dv = 11 - ((sum_() + 2) % 11)
if dv != value[-1]:
return 0,
else:
dv = sum_(value_slice=len(value), range_stop=0) % 11
if dv != 0:
return 0,
return 1,
28 changes: 28 additions & 0 deletions br_docs/validators/renavam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import re

from br_docs.validators import CheckDigits


class RENAVAMv(CheckDigits):
Patterns = re.compile(r"^\d{9,11}$"),
CHECK_DIGITS = 1

@staticmethod
def calculate_digits(value: list[int]) -> tuple[int, ...]:
value_length = len(value)
if value_length == 8:
value.insert(0, 0)
value.insert(0, 0)

elif value_length == 9:
value.insert(0, 0)

value_reversed = value[::-1]
calc = sum((i + 2) * v for i, v in zip(range(8), value_reversed))
calc += value_reversed[8] * 2
calc += value_reversed[9] * 3
mod11 = calc % 11
dv = 11 - mod11 if mod11 != 0 else 0
if dv == 10:
dv = 0
return dv,
182 changes: 182 additions & 0 deletions tests/cns.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
700205481863823
700405969996050
700400437357148
700500337269254
700505148993553
700003562113506
702501337577932
704605182555301
706306741232177
708407788683562
701807246246970
704607105113229
708005315521524
705002880408852
704803577861444
702405037177429
706400643101489
704001824650563
700501309159157
702308100731819
703503085874830
705105837024640
700200928046926
706802705148120
700703982162775
704001838066567
707809671584416
706405185741589
702303115942212
700608963208264
700005146112309
700406440607945
700802452817780
709005850810410
709003834713919
700507456998760
708900756181315
708007854609624
702005852657582
702303562787020
704009890057864
700005837942208
704609197734626
704607110878521
704105130355373
701003898972596
704608652849325
700209940462126
704808592414741
898000989285954
708605054876485
700007144667305
700006148294200
700006737538902
704807044667449
709202252600132
704203258091485
702409550443520
702409550443520
704602697985621
706201064661566
162565502430008
209490898630005
165775782740005
162565502430008
165773662850006
162173926590004
209814134170000
206667242070003
200694383440018
206179103100007
209490898630005
210002086070004
165775782740005
203458517780004
209937704060005
203470313790004
203458482620006
166068677040004
162562349290001
206667237900001
162228904670009
160180784820006
209824185360001
206072968570007
201177304960000
209814115380003
201326932020007
209392539700008
201177322940008
200707494200009
206429609460001
206667212830018
165709199630006
206857431570001
203470305690004
206857539850005
201383871700000
206122675840018
206667296670009
161279056650005
128080114250002
163911170050006
165775843040008
209391920410000
201327047780007
209678611490003
162169797020008
200663489130003
200707565750006
200632370530003
207179761190004
210128902970018
122117509330005
201177327660003
206857475000006
206176815160018
203022085120005
206667237900001
165709199630006
206857431570001
206857539850005
898004506436921
898006163175826
898003921625744
898004506436921
898002832212109
898005991001237
898004702970978
898002027685946
898004603199637
898004609169535
898004853646029
898004861435899
898004164768969
898002027670809
898004832537484
898002027732421
898005902038652
898002027718976
898003921625744
898005952117327
898003718765065
898002371802419
898001026562378
898004967224034
898004154516894
898003986025679
898002914730871
898006010024406
898004861854921
898001023729808
801434314083943
898004849878727
898001026097098
898005892314482
898006198232834
898006198702849
898006198267824
898005855945156
898004978736147
898002027731115
898004857869227
898005907426448
898004830268125
898004956715538
898004833493855
898004926679843
898006131614719
898005822316656
898002027717112
898004097085613
898004826280809
898002326753994
898004848704695
898004861489972
898003736308029
898004823300132
898004973461697
898005156301454
898004917963836
898005927434748
22 changes: 21 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def cnh_list():
def cpf_list():
"""
1-60 line: https://web.archive.org/web/20240223042316/https://www.detran.ac.gov.br/wp-content/uploads/2022/06/Lista-de-Selecionados-CNH-Social.pdf
61-122 line: https://web.archive.org/web/20240226031446/https://www.detran.ac.gov.br/wp-content/uploads/2022/06/Lista-de-Selecionados-CNH-Social.pdf
"""
return open_valid_docs('cpfs.txt')

Expand All @@ -40,6 +41,25 @@ def cnpj_list():
def nis_list():
"""
1-56 line: https://web.archive.org/web/20240225200956/http://www.varzeagrande.mt.gov.br/storage/Anexos/18cd1d8751995dccb6116b97ab9e0ce7.pdf
56-356 line: http://www.adcon.rn.gov.br/ACERVO/SEARH/DOC/DOC000000000151962.PDF
57-356 line: http://www.adcon.rn.gov.br/ACERVO/SEARH/DOC/DOC000000000151962.PDF
"""
return open_valid_docs('nis.txt')


@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
62-182 line: https://web.archive.org/web/20240226015830/https://altamira.pa.gov.br/wp-content/uploads/2021/07/LISTA-DE-VACINADOS-20-07.pdf
"""
return open_valid_docs('cns.txt')


@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
174-257 line: https://web.archive.org/web/20240226051016/https://www.euamoleilao.com.br/imprimir/0139-repasse-leilao-ciretran-s-sebastiao
258-687 line: https://web.archive.org/web/20240226051542/https://portal.tjpr.jus.br/pesquisa_athos/publico/carregarAnexo.do;jsessionid=5ff0c586fab5cb74176b49c48765?tjpr.url.crypto=16c74de0ca500657bb7c1cc39118d26e6a12b4f4c9aa9444c033d87933160fa249878bb1b73255ac
"""
return open_valid_docs('renavam.txt')
Loading

0 comments on commit e15755b

Please sign in to comment.