Value objects for TypeScript — validated, formatted, typed — with first-class Brazilian support: one place for the rules of phone numbers, base64 images, measures (value and unit, with exact conversions), CPF, CNPJ (alphanumeric included) and vehicle plates. No runtime dependencies; ESM and CommonJS, with types.
pnpm add @devix-tecnologia/value-objects # or: npm i @devix-tecnologia/value-objectsimport { Phone, Base64Image, measure } from '@devix-tecnologia/value-objects'
import { CpfCnpj, PlacaVeiculo } from '@devix-tecnologia/value-objects/br'
new Phone('27 97645-5555').formatted // '+55 27 97645-5555'
new Base64Image(base64OrDataUri).format // 'PNG', 'JPEG', 'WEBP'…
measure(10, 'liter').convert('milliliter').formatted // '10000 mL'
new CpfCnpj('52998224725').formatted // '529.982.247-25'
new CpfCnpj('12ABC34501DE35').isValid() // true (alphanumeric CNPJ)
new PlacaVeiculo('abc1d23').formatted // 'ABC1D23' (format: 'MERCOSUL')Requires Node.js 22 or later. The types work with TypeScript 4.7 or later.
Governments ship breaking changes too.
A CPF, a CNPJ, a plate or a phone number looks like a string. It is not: it carries rules — check digits, formats, valid area codes — and those rules belong to someone else. The Receita Federal, Contran, Anatel and the IETF change them on their schedule, not yours.
When every project copies the rule (a regex here, a parseInt there, a numeric database column
somewhere else), each change becomes a hunt across repositories. A value object keeps the rule
in one place: a single source of truth that every package and project imports. That is
cohesion, and it is cheaper: when the rule changes, you update one dependency instead of
auditing every codebase.
The alphanumeric CNPJ is the real example. In October 2024 the Receita Federal announced that
CNPJs would accept letters (IN RFB 2,229/2024), and on 31 July 2026 it issued the first one.
Every /^\d{14}$/, every Number(cnpj) and every numeric column broke on that day. Projects
that validated the CNPJ in one place changed one version number.
It was not the first time, and it will not be the last:
Eight currencies in 52 years, a ninth digit on every mobile phone, a new plate with new colors, letters in the CNPJ. The rules will change again; this package exists so that, when they do, the change lands in one place. Every date, body and document is in docs/TIMELINE.md.
| Path | Group | Exports |
|---|---|---|
@devix-tecnologia/value-objects |
Universal | Phone, Base64Image, Measure and, for compatibility, everything from /br |
@devix-tecnologia/value-objects/br |
Brazilian rules | CpfCnpj, PlacaVeiculo, their types and constants |
Phone stays in the root: it is international, with the Brazilian numbering plan applied when
the country code is 55. The root still re-exports the Brazilian value objects, so existing
import { CpfCnpj } from '@devix-tecnologia/value-objects' keeps working.
- Members are in English everywhere:
isValid,formatted,toJSON,equals,category,convert… - Universal classes are in English, with Portuguese aliases of the same class at the root:
Phone/Telefone,Base64Image/ImagemBase64,Measure/Medida(and the factorymeasure/medida, the typesDimension/GrandezaandUnit/Unidade).Telefone === Phone, so you can pick either name, and code written with the former names keeps working. - Members have one name only, in English: the aliases cover class and type names, not methods.
/bris about Brazilian rules, not about the Portuguese language: it holds only what a Brazilian rule defines; universal value objects never move there.- Brazilian classes are in Portuguese, because they are terms of Brazilian law:
CpfCnpj,PlacaVeiculo(and, later,Cep,ChavePix). Values taken from a Brazilian rule stay in Portuguese too, as the rule writes them ('aluguel','vermelha').
Every value object has isValid(), formatted, toJSON(), toString() and equals(), plus
type, version and validation metadata.
| Class | Path | What it holds | Details |
|---|---|---|---|
Phone |
root | Phone number, with country code and Brazilian area code (DDD) (alias Telefone) |
README |
Base64Image |
root | Base64 image or data URI, with the format read from the file bytes (alias ImagemBase64) |
README |
Measure |
root | A value in a unit (10 L, 5 km), with the dimension in the type and exact conversions (alias Medida) |
README |
CpfCnpj |
/br |
CPF or CNPJ, numeric or alphanumeric | README |
PlacaVeiculo |
/br |
Vehicle plate, old (ABC-1234) or Mercosul (ABC1D23), with its category and colors |
README |
const tel = new Phone('+55 11 91234-5678')
tel.isValid() // true
tel.formatted // '+55 11 91234-5678'
tel.value // '5511912345678'
tel.country // 'BR'
tel.estate // 'SP' (state of the area code)
new Phone({ countryCode: '+1', areaCode: '415', phoneNum: '555-0123' })
.formatted // '+1 415 555-0123'const img = new Base64Image('data:image/png;base64,iVBORw0KGgo…')
img.isValid() // true
img.format // 'PNG' (from the data URI type, or from the file signature in plain base64)
img.content // the base64 without the data URI prefix
img.formatted // the data URIconst tank = measure(50, 'liter') // Measure<'volume', 'liter'>
tank.formatted // '50 L'
tank.convert('us-gallon').round(2).formatted // '13.21 US gal'
measure(1, 'kilometer').add(measure(500, 'meter')).formatted // '1.5 km'
measure(1, 'foot').equals(measure(12, 'inch')) // true
measure('2.5', 'liter').format({ decimals: 2, locale: 'pt-BR' }) // '2,50 L'
tank.convert('meter') // ✗ does not compile: volume is not lengthLength, area, volume, mass and time, with the SI units and exact non-SI ones (inch, foot, mile, acre, US and imperial gallon, barrel, pound, ounce). No floating point: the value is an exact decimal (a fraction when a conversion repeats, like 1 m in inches) and rounding is explicit. Temperature and derived dimensions come next; money is not a measure (its conversion depends on an exchange rate). See the Measure README.
const cpf = new CpfCnpj('529.982.247-25')
cpf.type // 'CPF'
cpf.value // '52998224725'
const cnpj = new CpfCnpj('12ABC34501DE35')
cnpj.type // 'CNPJ'
cnpj.formatted // '12.ABC.345/01DE-35'new PlacaVeiculo('abc-1234').formatted // 'ABC-1234' (format: 'ANTIGA')
new PlacaVeiculo('abc 1d23').formatted // 'ABC1D23' (format: 'MERCOSUL', no hyphen)
const plate = new PlacaVeiculo('ABC1D23', { use: 'aluguel' })
plate.category
// { group: 'comercial', use: 'aluguel', characters: 'vermelha', background: 'branca' }
new PlacaVeiculo('ABC1D23', { color: 'azul' }).group // 'oficial'The category comes from the vehicle's use or from the color of the characters (never
both: it does not compile), following Contran Resolution 969/2022, Annex I, Table III. The
values ('aluguel', 'vermelha') are the resolution's own terms, in Portuguese.
CpfCnpj,PlacaVeiculoandBase64Imageaccept any string in the constructor;isValid()tells whether it is valid.Measureaccepts an integer, a decimal string or abigint; a number with decimals or a malformed string gives an invalid measure. An unknown unit throws aTypeError.Phonethrows in the constructor when it cannot read a phone number from the string; pass an object ({ countryCode, areaCode, phoneNum }) to build one field by field.PlacaVeiculothrows when the options bring bothuseandcolor, or a value outside Table III.formatted,value,unformattedandisValid({ raiseException: true })throw anErrorwhen the value is invalid. CheckisValid()first when the input comes from a user.
const doc = new CpfCnpj(input)
if (doc.isValid()) save(doc.formatted)Each rule follows the official source, cited in the tests. The history of each rule is in docs/TIMELINE.md.
| Value object | Rule | Source |
|---|---|---|
| CPF | Check digits by modulo 11; sequences of a repeated digit are refused | Receita Federal |
| CNPJ | Alphanumeric: each character is worth its ASCII code minus 48, then modulo 11 | IN RFB 2,229/2024; Receita Federal Q&A and Serpro |
| Phone | Brazilian area codes (DDD); subscriber number with 8 digits, or 9 starting with 9 | Anatel, Resolution 553/2010 (ninth digit) and Resolution 749/2022 (numbering regulation) |
| Vehicle plate | Mercosul plate without hyphen (ABC1D23); old plate ABC-1234 |
Contran Resolution 969/2022, which replaced Resolution 780/2019 on 2022-07-01 |
| Plate colors | Category by use or by character color; background white, or black for national collection | Contran Resolution 969/2022, Annex I, item 1.1/1.1.1 and Table III (DOU of 2022-06-24, Section 1, p. 57) |
| Measure | SI units and prefixes; non-SI units by their exact definition (1 in = 0.0254 m, 1 lb = 0.453 592 37 kg, 1 US gal = 231 in³) | BIPM SI Brochure (9th ed.), Tables 7 and 8; Inmetro QGUM (Ordinance 615/2023); NIST Handbook 44, Appendix C; NIST SP 811, Appendix B.8 |
| Base64 image | Format by the file signature (magic bytes); data URI as in RFC 2397; base64 as in RFC 4648 | WHATWG MIME Sniffing and each format's specification |
The changes of each version are in the GitHub releases. Versions before 2.6.1 are deprecated on npm: upgrade to the latest 2.x.
Measure (length, area, volume, mass and time) arrives in 2.7. Next, in this order:
| Value object | Group | Why |
|---|---|---|
Cep (ZIP code) |
/br |
Postal code of the Correios, with or without hyphen |
ChavePix |
/br |
A discriminated union over the objects above: CPF, CNPJ, phone, e-mail or random key |
| Money | universal | Amount and ISO 4217 currency together, no float, no currency conversion |
Measure, step 2 |
universal | Temperature (scales with offset; temperature vs. temperature difference) and derived dimensions (speed, area as length × length) |
Later candidates: e-mail, a validity period (start and end, "as of an instant"), geographic coordinates, GTIN/EAN barcodes, VIN, IBAN, country, language and currency codes; and, in /br, the bank slip (boleto), the NF-e access key, the state tax registration (Inscrição Estadual), RENAVAM, CNH, PIS/PASEP/NIT, voter ID and the national health card (CNS).
Ideas and help are welcome in the issues: choose the value object in the form.
Contributions are welcome. See CONTRIBUTING.md: fork, create a branch, add tests and open a pull request. When a rule changes, cite the official document in the test.
The README images are generated from HTML: pnpm banner (docs/banner.html) and
pnpm timeline (docs/timeline.html).
MIT. See LICENSE.

