Skip to main content

Documents validator (CNPJ numeric and alphanumeric)

Project description

docs-validator

CI Python License PyPI

A lightweight Python library for validating and generating document numbers, starting with Brazilian documents.

The project focuses on:

  • CPF validation
  • CNPJ validation
  • Experimental alphanumeric CNPJ
  • Automatic document detection
  • Document generators
  • CLI usage
  • PyPI distribution
  • CI/CD and test coverage

Installation

pip install docs-validator

For development:

git clone https://github.com/thinkbruno/docs-validator
cd docs-validator

python -m venv .venv
source .venv/bin/activate

pip install -e .

Quick Usage

from docs_validator import validate

validate("52998224725")
validate("11222333000181")

API Overview

Generic Validation

The validate() function automatically detects the document type.

from docs_validator import validate

validate("52998224725")       # CPF
validate("11222333000181")    # CNPJ

Auto Detection

from docs_validator import detect

detect("52998224725")
detect("11222333000181")
detect("12ABC34501DE35")

Possible results:

CPF
CNPJ
CNPJ_ALPHANUMERIC

CPF Validation

from docs_validator import validate_cpf

validate_cpf("52998224725")

CNPJ Validation

from docs_validator import validate_cnpj

validate_cnpj("11222333000181")

Generators

Generate valid documents for testing environments.

from docs_validator import generate_cpf, generate_cnpj

generate_cpf()
generate_cnpj()

Example:

41799543807
30167399000130

Experimental: Alphanumeric CNPJ

Brazil's tax authority announced a future format allowing alphanumeric CNPJ identifiers.

The library includes an experimental validator for this format.

from docs_validator import validate

validate("12ABC34501DE35", mode="experimental")

⚠️ Important

As of 2026, no real alphanumeric CNPJs are publicly issued.

Any example used in tests or documentation is synthetic and only intended to validate the algorithm.


CLI Usage

After installation:

docs-validator validate 52998224725
docs-validator validate 11222333000181

Example output:

VALID

Testing

Run tests with:

pytest

Run with coverage:

pytest --cov=docs_validator

Project Structure

docs-validator
│
├── src
│   └── docs_validator
│       ├── validator.py
│       ├── detect.py
│       │
│       ├── br
│       │   ├── cpf.py
│       │   ├── cnpj.py
│       │   └── generators.py
│       │
│       └── experimental
│           └── cnpj_alphanumeric.py
│
├── tests
├── README.md
└── pyproject.toml

Example Test Script

from docs_validator import *

print(validate("52998224725"))
print(validate("11222333000181"))

print(detect("52998224725"))
print(detect("11222333000181"))

cpf = generate_cpf()
cnpj = generate_cnpj()

print(cpf, validate_cpf(cpf))
print(cnpj, validate_cnpj(cnpj))

print(validate("12ABC34501DE35", mode="experimental"))

Why docs-validator?

Many validation libraries:

  • focus only on regex
  • do not implement real verification digit algorithms
  • do not support future document formats

docs-validator focuses on:

  • correctness of official validation algorithms
  • simple and clean Python API
  • extensibility for multiple countries

Roadmap

Future goals for the library:

  • Multi-country document validation
  • Support for VAT / SSN style identifiers
  • Additional generators
  • Performance optimization
  • Extended CLI capabilities

Portfolio

Author portfolio:

https://thinkbruno.github.io/


License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docs_validator-1.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docs_validator-1.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file docs_validator-1.1.0.tar.gz.

File metadata

  • Download URL: docs_validator-1.1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for docs_validator-1.1.0.tar.gz
Algorithm Hash digest
SHA256 0761f74a088cc80b079afc4fcaa6ba12b8413c3b53e0ff563a9da2b32e316b1d
MD5 d0afd62683dea01e466a02d08090d233
BLAKE2b-256 f07b70c81148b10f62cb49903f111cafc7bf1496323c8640b5f2bba81c9cfe61

See more details on using hashes here.

File details

Details for the file docs_validator-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: docs_validator-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for docs_validator-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4de97dda6e8cbd797a32f0308961b4c0a47112761de36d801e2e467d04c86182
MD5 08b6a1b72cc6392a35c26084fc19898a
BLAKE2b-256 5bf2409736c2cc20ce0b4d7eb12815d8d39b8ea32faf5a5c1203929cae8f987c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page