docs-validator 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docs_validator/__init__.py +4 -0
- docs_validator/dv.py +15 -0
- docs_validator/formatter.py +10 -0
- docs_validator/utils.py +18 -0
- docs_validator/validator.py +17 -0
- docs_validator-0.1.0.dist-info/METADATA +178 -0
- docs_validator-0.1.0.dist-info/RECORD +10 -0
- docs_validator-0.1.0.dist-info/WHEEL +5 -0
- docs_validator-0.1.0.dist-info/licenses/LICENSE +21 -0
- docs_validator-0.1.0.dist-info/top_level.txt +1 -0
docs_validator/dv.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .utils import char_to_value
|
|
2
|
+
|
|
3
|
+
WEIGHTS_FIRST = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
|
4
|
+
WEIGHTS_SECOND = [6] + WEIGHTS_FIRST
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def calculate_digit(base: str, weights: list[int]) -> int:
|
|
8
|
+
total = 0
|
|
9
|
+
|
|
10
|
+
for char, weight in zip(base, weights):
|
|
11
|
+
total += char_to_value(char) * weight
|
|
12
|
+
|
|
13
|
+
remainder = total % 11
|
|
14
|
+
|
|
15
|
+
return 0 if remainder < 2 else 11 - remainder
|
docs_validator/utils.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def normalize(value: str) -> str:
|
|
5
|
+
"""
|
|
6
|
+
Remove non alphanumeric characters and uppercase value
|
|
7
|
+
"""
|
|
8
|
+
return re.sub(r"[^A-Z0-9]", "", value.upper())
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def char_to_value(char: str) -> int:
|
|
12
|
+
"""
|
|
13
|
+
Convert alphanumeric character to numeric value
|
|
14
|
+
"""
|
|
15
|
+
if char.isdigit():
|
|
16
|
+
return int(char)
|
|
17
|
+
|
|
18
|
+
return ord(char) - 55 # A=10 ... Z=35
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from .utils import normalize
|
|
2
|
+
from .dv import calculate_digit, WEIGHTS_FIRST, WEIGHTS_SECOND
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def validate(cnpj: str) -> bool:
|
|
6
|
+
cnpj = normalize(cnpj)
|
|
7
|
+
|
|
8
|
+
if len(cnpj) != 14:
|
|
9
|
+
return False
|
|
10
|
+
|
|
11
|
+
base = cnpj[:12]
|
|
12
|
+
dv = cnpj[12:]
|
|
13
|
+
|
|
14
|
+
digit1 = calculate_digit(base, WEIGHTS_FIRST)
|
|
15
|
+
digit2 = calculate_digit(base + str(digit1), WEIGHTS_SECOND)
|
|
16
|
+
|
|
17
|
+
return dv == f"{digit1}{digit2}"
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: docs-validator
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Brazilian document validator (CNPJ numeric and alphanumeric)
|
|
5
|
+
Author: Bruno Ramos
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# docs-validator
|
|
13
|
+
|
|
14
|
+

|
|
15
|
+

|
|
16
|
+

|
|
17
|
+
|
|
18
|
+
A lightweight Python library for validating Brazilian document
|
|
19
|
+
identifiers.
|
|
20
|
+
|
|
21
|
+
Currently the project supports:
|
|
22
|
+
|
|
23
|
+
- CNPJ (numeric)
|
|
24
|
+
- **Experimental support for alphanumeric CNPJ**
|
|
25
|
+
|
|
26
|
+
The project is designed with:
|
|
27
|
+
|
|
28
|
+
- clean architecture
|
|
29
|
+
- automated testing
|
|
30
|
+
- CI/CD
|
|
31
|
+
- PyPI-ready packaging
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
# ⚠️ About Alphanumeric CNPJ
|
|
36
|
+
|
|
37
|
+
As of **2026**, there are **no alphanumeric CNPJ numbers in public
|
|
38
|
+
circulation**.
|
|
39
|
+
|
|
40
|
+
The future format has been announced by the Receita Federal do Brasil,
|
|
41
|
+
but the official database still contains **only numeric CNPJ
|
|
42
|
+
identifiers**.
|
|
43
|
+
|
|
44
|
+
Therefore:
|
|
45
|
+
|
|
46
|
+
⚠️ Any alphanumeric CNPJ used in this project is **only for algorithm
|
|
47
|
+
testing purposes** and **does not represent a real company**.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
# Experimental Validation Mode
|
|
52
|
+
|
|
53
|
+
To allow experimentation with the future format, the library supports an
|
|
54
|
+
**experimental validation mode**.
|
|
55
|
+
|
|
56
|
+
Example:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from docs_validator import validate
|
|
60
|
+
|
|
61
|
+
validate("12ABC34501DE35", mode="experimental")
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
When using `mode="experimental"`:
|
|
65
|
+
|
|
66
|
+
- letters **A-Z** are allowed in the first 12 positions
|
|
67
|
+
- letters are converted using **base36 mapping**
|
|
68
|
+
- the **same modulo‑11 check digit algorithm** is applied
|
|
69
|
+
|
|
70
|
+
Mapping example:
|
|
71
|
+
|
|
72
|
+
A = 10
|
|
73
|
+
B = 11
|
|
74
|
+
...
|
|
75
|
+
Z = 35
|
|
76
|
+
|
|
77
|
+
Without experimental mode:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
validate("12ABC34501DE35")
|
|
81
|
+
# returns False
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
# Installation
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pip install docs-validator
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
# Usage
|
|
95
|
+
|
|
96
|
+
## Validate numeric CNPJ
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from docs_validator import validate
|
|
100
|
+
|
|
101
|
+
validate("11222333000181")
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Validate experimental alphanumeric CNPJ
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
validate("12ABC34501DE35", mode="experimental")
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Format CNPJ
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from docs_validator import format_cnpj
|
|
114
|
+
|
|
115
|
+
format_cnpj("11222333000181")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Output:
|
|
119
|
+
|
|
120
|
+
11.222.333/0001-81
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
# Running tests
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
pytest --cov=docs_validator
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Generate HTML coverage report:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
pytest --cov=docs_validator --cov-report=html
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
# Project structure
|
|
139
|
+
|
|
140
|
+
docs-validator
|
|
141
|
+
│
|
|
142
|
+
├── src/
|
|
143
|
+
│ └── docs_validator/
|
|
144
|
+
│
|
|
145
|
+
├── tests/
|
|
146
|
+
│
|
|
147
|
+
├── pyproject.toml
|
|
148
|
+
└── README.md
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
# Roadmap
|
|
153
|
+
|
|
154
|
+
Planned features:
|
|
155
|
+
|
|
156
|
+
- CPF validation
|
|
157
|
+
- document generators
|
|
158
|
+
- CLI interface
|
|
159
|
+
- support for additional Brazilian identifiers
|
|
160
|
+
- benchmarking tools
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
# Author
|
|
165
|
+
|
|
166
|
+
Bruno Ramos
|
|
167
|
+
|
|
168
|
+
LinkedIn\
|
|
169
|
+
https://www.linkedin.com/in/ramosbruno90/
|
|
170
|
+
|
|
171
|
+
GitHub\
|
|
172
|
+
https://github.com/thinkbruno
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
# License
|
|
177
|
+
|
|
178
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
docs_validator/__init__.py,sha256=4qlRKapJbWICGQoyBgz5L_rClCnp8Wf5IoE4206wOvE,106
|
|
2
|
+
docs_validator/dv.py,sha256=mYaXAkIeCewRVP1OYQF3vpcVod9T-PzZ7sIYEJHJ98E,369
|
|
3
|
+
docs_validator/formatter.py,sha256=eZyZS2vjNKmVPgZ684wi2FadpHV0xEChoXLkLhCRpsM,250
|
|
4
|
+
docs_validator/utils.py,sha256=UnIZxZNaaDA_UGale8uDOzqJtE515hvJjqgOPHeTcL0,371
|
|
5
|
+
docs_validator/validator.py,sha256=U5UokBuabzLyN7f5bHchydBqEKB9KFlpyAuce-dBJ94,395
|
|
6
|
+
docs_validator-0.1.0.dist-info/licenses/LICENSE,sha256=yHw7YEBozoJE6GPulQc6mJv51sNCW5em3-JmqLs4JJI,1068
|
|
7
|
+
docs_validator-0.1.0.dist-info/METADATA,sha256=yYXzQDjFkgiatUxCyvKRYJC155vu_Vcqh4kC-dr8P3E,2880
|
|
8
|
+
docs_validator-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
9
|
+
docs_validator-0.1.0.dist-info/top_level.txt,sha256=Bhjw2XA9N0hdQjrxHVXBrFGYlrq0I5Du681X3FcfU7U,15
|
|
10
|
+
docs_validator-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bruno Ramos
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
docs_validator
|