-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new validation RENAVAM and CNS
Took 4 hours 52 minutes
- Loading branch information
1 parent
d536f62
commit e15755b
Showing
10 changed files
with
1,046 additions
and
14 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
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,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, |
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,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, |
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,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 |
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
Oops, something went wrong.