validation-br 2.0: Validação em TypeScript, zero dependências
Acabei de lançar a versão 2.0 do validation-br , uma biblioteca em TypeScript para validar documentos brasileiros: CPF, CNPJ (numérico e alfanumérico), CEP, Boleto, chave Pix, Pix Copia e Cola, Título de Eleitor, PIS/PASEP, CNH, RENAVAM e mais uma dezena de outros formatos. A grande mudança da 2.0 é a forma como a validação funciona: cada documento agora é uma classe imutável, no estilo value…
The author has just released version 2.0 of the TypeScript library validation-br, which is used for validating Brazilian documents such as CPF, CNPJ, CEP, boleto, Pix, Pix Copia e Cola, Título de Eleitor, PIS/PASEP, CNH, RENAVAM, and many more. The major change in version 2.0 is that each document is now an immutable class, similar to a value object. Instead of calling a function that returns true or false, you instantiate the document and it is already validated.
For example, to create a new CPF object:
import { CPF } from "validation-br/cpf";
const cpf = new CPF("906.259.666-51");
The value property will return the normalized CPF value without any formatting:
cpf.value // "90625966651"
The mask() method formats the value with the traditional CPF mask:
cpf.mask() // "906.259.666-51"
If an invalid value is provided, the constructor will throw an exception instead of returning false. This makes the flow more explicit: you either have a valid CPF or you handle the error. For those who prefer a quick boolean result, the shortcuts like isCPF and isCNPJ are still available and work as before.
One important point is that the package has zero dependencies in production. It only uses native JS/TS APIs, resulting in a leaner node_modules folder and a smaller attack surface for supply chain concerns.
A notable addition is support for alphanumeric CNPJs, which the Receita Federal began accepting in 2026. The library can now generate fake examples for testing:
import { CNPJ } from "validation-br/cnpj";
// Generates an alphanumeric CNPJ example
CNPJ.fake({ alphanumeric: true }).mask();
// "WX.BC2.1FX/0001-00"
You can create a new CNPJ instance with the masked value and access its normalized value:
new CNPJ("WX.BC2.1FX/0001-00").value; // "WXBC21FX000100"
The same class handles calculating the check digit, applying formatting, removing masks, and generating valid fake values in both numeric and alphanumeric formats. For more information, visit the website at https://validation-br.js.org, check out the code on GitHub at https://github.com/klawdyo/validation-br, or install it via npm with the command: npm install validation-br.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.