modulae/ptbr-doc-validator

Brazilian document validators for Laravel.

Maintainers

Package info

github.com/modulae/ptbr-doc-validator

pkg:composer/modulae/ptbr-doc-validator

Statistics

Installs: 193

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 2

0.1.1 2026-02-19 14:01 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A lightweight Laravel package that provides validators and utilities for common Brazilian documents. The first supported document is CNPJ, including support for alphanumeric variants that are to be introduced in June 2026. It includes:

  • A cnpj validation rule registered with Laravel's Validator facade.
  • A Cnpj value object with helpers to normalize, format, cast in Eloquent models, and validate values.
  • A cnpjToString helper to normalize strings and arrays of CNPJ values.
  • Localized validation error messages (EN and PT-BR).

Installation

Install via Composer:

composer require modulae/ptbr-doc-validator

Publish translations (optional, if you want to customize messages):

php artisan vendor:publish --tag="ptbr-doc-validator-translations"

Usage

1) Using the built-in cnpj validation rule

use Illuminate\Support\Facades\Validator;

$validator = Validator::make(
    ['cnpj' => 'T6.JSP.XPS/0001-11'],
    ['cnpj' => 'cnpj']
);

$validator->passes(); // true/false

2) Using the Cnpj value object

use Modulae\PTBRDocValidator\ValueObjects\Cnpj;

$cnpj = new Cnpj('T6.JSP.XPS/0001-11');
$cnpj->isValid();     // bool
$cnpj->raw();         // "T6JSPXPS000111"
$cnpj->formatted();   // "T6.JSP.XPS/0001-11"
(string) $cnpj;       // "T6JSPXPS000111"

Eloquent cast example:

class Company extends Model
{
    protected $casts = [
        'cnpj' => \Modulae\PTBRDocValidator\ValueObjects\Cnpj::class,
    ];
}

3) Using the cnpjToString helper

use function Modulae\PTBRDocValidator\cnpjToString;

cnpjToString('T6.JSP.XPS/0001-11');
// => "T6JSPXPS000111"

cnpjToString(['32.332.643/0001-29', 'T6.JSP.XPS/0001-11']);
// => ['32332643000129', 'T6JSPXPS000111']

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.