Quick Start
This guide takes you from an untrusted string to a clean, validated RUT ready for storage — the path almost every integration needs.
New here? Skim What is a RUT? first. Already installed? Jump straight in. Not installed yet — see Installation.
The five-minute path#
Format input as the user types
Incremental mode formats progressively and never throws, so it is safe to call on every keystroke.
import { format } from 'rut.ts'
format('12345678', { incremental: true }) // '1.234.567-8'
format('123456785', { incremental: true }) // '12.345.678-5'Validate on blur or submit
validate() checks the shape and the Modulo 11 verifier digit.
Use strict: true as the final acceptance gate for real identities.
import { validate } from 'rut.ts'
validate('12.345.678-5') // true
validate('12.345.678-5', { strict: true }) // true
validate('11.111.111-1', { strict: true }) // false (placeholder)Clean for storage
Store a single canonical shape regardless of how the user typed it.
import { clean } from 'rut.ts'
clean('12.345.678-5') // '123456785'Decompose if your schema splits it
Some schemas keep body and verifier in separate columns.
import { decompose } from 'rut.ts'
const { body, verifier } = decompose('12.345.678-5')
// body: '12345678', verifier: '5'Try the whole flow#
Type below — validate(), format(), clean(), and decompose() all run on every keystroke:
Putting it together#
A complete, copy-pasteable handler that accepts user input, validates it strictly, and returns a storage-ready value:
import { validate, clean } from 'rut.ts'
function acceptRut(input: unknown):
| { ok: true; rut: string }
| { ok: false; error: string } {
if (typeof input !== 'string') {
return { ok: false, error: 'RUT is required' }
}
// Final acceptance gate: shape + verifier + no placeholders.
if (!validate(input, { strict: true })) {
return { ok: false, error: 'Invalid RUT' }
}
// Safe by construction: validate() already passed.
return { ok: true, rut: clean(input) }
}
acceptRut('12.345.678-5') // { ok: true, rut: '123456785' }
acceptRut('11.111.111-1') // { ok: false, error: 'Invalid RUT' }
acceptRut(123456785) // { ok: false, error: 'RUT is required' }Rule of thumb: format() for display, validate({ strict: true })
to accept, clean() to store. Never trust clean() or decompose()
output as “validated” — they normalize shape but do not check the
verifier digit.