Skip to main content

brazil-validator (Python)

A validation, normalization, and formatting package for Brazilian data: CPF, CNPJ, CEP, phone numbers, e-mail, PIX keys, and Inscrição Estadual.

This is the Python port of the TypeScript implementation, replicating the same officially-verified rules. Every module follows the same conceptual API:

module.is_valid(value: str) -> bool
module.normalize(value: str) -> str
module.format(value: str) -> str

ie extends this with a second uf argument (the check-digit algorithm is defined per state, not nationally), and pix additionally exposes get_key_type.

Installation

Not yet published to PyPI. Install straight from this repository's python/ subdirectory:

pip install "brazil-validator @ git+https://github.com/matheuslm7/brazil-validator.git#subdirectory=python"

(the PyPI distribution name is brazil-validator, but — like beautifulsoup4/bs4 or python-dateutil/dateutil — the importable package keeps its original name, br_validator)

Usage

from br_validator import cpf, cnpj, ie, pix

cpf.is_valid("529.982.247-25")       # True
cpf.format("52998224725")            # "529.982.247-25"

cnpj.is_valid("11.222.333/0001-81")  # True

ie.is_valid("110.042.490.114", "SP") # True

pix.get_key_type("+5511987654321")   # pix.KeyType.PHONE

Modules

Module Notes
cpf
cnpj Numeric and alphanumeric
cep
phone National numbers only, no +55
email format returns the same value as normalize (no visual mask)
pix KeyType enum: CPF, CNPJ, EMAIL, PHONE, EVP
ie All 27 states/DF; is_valid/normalize/format take (value, uf)

See the main README for the full behavior reference (official sources, normalize/format rules per validator, known gaps) — it applies identically here.

Conformance

tests/test_conformance.py checks this implementation against specification/, the language-independent test vectors generated from the TypeScript implementation. If those tests pass, this port behaves identically to TypeScript (and to the Go, Java, Ruby, and C# ports) for every vector on file.

Development

This package has zero runtime dependencies and tests are written with the standard library's unittest (no pytest needed).

cd python
PYTHONPATH=src python3 -m unittest discover -s tests -v
python3 -m compileall src tests

Metadata

Release files for brazil-validator 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for brazil-validator 0.1.0
File Size Uploaded Provenance Actions
brazil_validator-0.1.0.tar.gz 20.4 kB Without provenance Details for brazil_validator-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for brazil-validator 0.1.0
File Interpreter ABI Platform Actions
brazil_validator-0.1.0-py3-none-any.whl Python 3 none any Details for brazil_validator-0.1.0-py3-none-any.whl

Total release size: 52.9 kB

Release files / brazil_validator-0.1.0.tar.gz

Download URL brazil_validator-0.1.0.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a19e3e65f28d1d23ecb93dd8cd36cb78eea1f10766f51b02e5c2446c70d2f858
BLAKE2b-256 checksum
How to use checksums
0dbe0c0a5ee2bc0e9fede12dbf39f3e40bdd18635456ebf021604d52363e74b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / brazil_validator-0.1.0-py3-none-any.whl

Download URL brazil_validator-0.1.0-py3-none-any.whl
Size 32.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f32fae6bf735a2bf3dcd4527c296172b041c016e7f1b412262cdc18807124963
BLAKE2b-256 checksum
How to use checksums
bb9d77e5a3a57a642ecce1de86262792f3a099cd4e41fa753ae921a9d9a62a2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Security for brazil-validator 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page