Utility function to validate CNPJ (Brazilian employer ID)
Project description
Utility function/class to validate CNPJ (Brazilian employer ID).
Python Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Installation
$ pip install cnpj-val
Import
# Using class-based resource
from cnpj_val import CnpjValidator
# Or using function-based one
from cnpj_val import cnpj_val
Usage
Object-Oriented Usage
validator = CnpjValidator()
cnpj = '98765432000198'
print('Valid' if validator.is_valid(cnpj) else 'Invalid') # returns 'Valid'
cnpj = '98.765.432/0001-98'
print('Valid' if validator.is_valid(cnpj) else 'Invalid') # returns 'Valid'
cnpj = '98765432000199'
print('Valid' if validator.is_valid(cnpj) else 'Invalid') # returns 'Invalid'
Functional programming
The helper function cnpj_val() is just a functional abstraction. Internally it creates an instance of CnpjValidator and calls the is_valid() method right away.
cnpj = '98765432000198'
print('Valid' if cnpj_val(cnpj) else 'Invalid') # returns 'Valid'
print('Valid' if cnpj_val('98.765.432/0001-98') else 'Invalid') # returns 'Valid'
print('Valid' if cnpj_val('98765432000199') else 'Invalid') # returns 'Invalid'
Validation Examples
# Valid CNPJ numbers
cnpj_val('98765432000198') # returns True
cnpj_val('98.765.432/0001-98') # returns True
cnpj_val('03603568000195') # returns True
# Invalid CNPJ numbers
cnpj_val('98765432000199') # returns False
cnpj_val('12345678901234') # returns False
cnpj_val('00000000000000') # returns False
cnpj_val('11111111111111') # returns False
cnpj_val('123') # returns False (too short)
cnpj_val('') # returns False (empty)
Features
- ✅ Format Agnostic: Accepts CNPJ with or without formatting (dots, slashes, dashes)
- ✅ Strict Validation: Validates both check digits according to Brazilian CNPJ algorithm
- ✅ Type Safety: Built with Python 3.10+ type hints
- ✅ Lightweight: Minimal dependencies, only requires
cnpj-genfor check digit calculation - ✅ Dual API: Both object-oriented and functional programming styles supported
API Reference
CnpjValidator Class
is_valid(cnpj_string: str) -> bool
Validates a CNPJ string and returns True if valid, False otherwise.
Parameters:
cnpj_string(str): The CNPJ to validate (with or without formatting)
Returns:
bool:Trueif the CNPJ is valid,Falseotherwise
cnpj_val() Function
cnpj_val(cnpj_string: str) -> bool
Functional wrapper around CnpjValidator.is_valid().
Parameters:
cnpj_string(str): The CNPJ to validate (with or without formatting)
Returns:
bool:Trueif the CNPJ is valid,Falseotherwise
Validation Algorithm
The package validates CNPJ using the official Brazilian algorithm:
- Length Check: Ensures the CNPJ has exactly 14 digits
- First Check Digit: Calculates and validates the 13th digit
- Second Check Digit: Calculates and validates the 14th digit
- Format Tolerance: Automatically strips non-numeric characters before validation
Error Handling
The validator is designed to be forgiving with input format but strict with validation:
- Invalid formats (too short, too long) return
False - Invalid check digits return
False - Empty strings return
False - Non-numeric strings (after stripping formatting) return
False
Dependencies
- Python: >= 3.10
- cnpj-gen: >= 1.0.0 (for check digit calculation)
Contribution & Support
We welcome contributions! Please see our Contributing Guidelines for details. But if you find this project helpful, please consider:
- ⭐ Starring the repository
- 🤝 Contributing to the codebase
- 💡 Suggesting new features
- 🐛 Reporting bugs
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG for a list of changes and version history.
Made with ❤️ by Lacus Solutions
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cnpj_val-1.0.0.tar.gz.
File metadata
- Download URL: cnpj_val-1.0.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
842645a4d4a2b9b8738796caa8982b4c474eb7dd19408e3dd2e8eefb83cebf60
|
|
| MD5 |
43be6e1d9b510464c2d896c657109ebb
|
|
| BLAKE2b-256 |
b54525e4a2619ba9213a52decc357728b09f1cab1f97ed3b996f4f6c8cb7a797
|
File details
Details for the file cnpj_val-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cnpj_val-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab921596f32fd6df6c922fc32ce317dad1e0ffc21d37fb76e9ecc0a64c3f81ca
|
|
| MD5 |
5f91b42ea626109055a1964f4a5c71bf
|
|
| BLAKE2b-256 |
8cd5a113ab224a6b96f5c52dbf6e01727f774548e7838f5ebfb2fa23c70715bc
|