API Reference
The complete public surface. Every throwing function accepts { throwOnError: false } to return null instead of throwing.
Functions#
Validation#
| Function | Signature | Notes |
|---|---|---|
validate() | validate(rut: unknown, options?: ValidateOptions): boolean | Shape + Modulo 11 verifier; strict rejects placeholders |
isValidRut() | isValidRut(rut: unknown, options?: { strict?: boolean }): rut is Rut | Type guard narrowing to the branded Rut |
isRutLike() | isRutLike(rut: unknown): boolean | Bounded shape check, no verifier math |
Formatting & cleaning#
| Function | Signature | Safe mode |
|---|---|---|
format() | format(rut: string, options?: FormatOptions): string | ✅ |
clean() | clean(rut: string, options?: SafeOptions): string | ✅ |
mask() | mask(rut: string, options?: { throwOnError?: boolean }): string | null | ✅ |
Comparison#
| Function | Signature | Notes |
|---|---|---|
equals() | equals(a: unknown, b: unknown, options?: EqualsOptions): boolean | Normalized cross-shape comparison; checks Modulo 11 by default ({ requireValid: false } for pure shape comparison) |
Decomposition#
| Function | Signature | Safe mode |
|---|---|---|
decompose() | decompose(rut: string, options?: SafeOptions): DecomposedRut | ✅ |
getBody() | getBody(rut: string, options?: SafeOptions): string | ✅ |
getVerifier() | getVerifier(rut: string, options?: SafeOptions): VerifierDigit | ✅ |
Generation & math#
| Function | Signature | Safe mode |
|---|---|---|
generate() | generate(options?: GenerateOptions): string | string[] | N/A |
calculateVerifier() | calculateVerifier(rutBody: string, options?: SafeOptions): VerifierDigit | ✅ |
Constants#
| Constant | Value | Purpose |
|---|---|---|
RUT_PATTERN | { any, compact, hyphen, dotted } — regex strings | The accepted shapes for JSON Schema, OpenAPI, zod and database constraints. Shape only: pair with validate() |
TypeScript types#
import type {
DecomposedRut,
EqualsOptions,
FormatOptions,
GenerateOptions,
Rut,
SafeOptions,
ValidateOptions,
VerifierDigit,
} from 'rut.ts'Since 5.1.2, every function reads options only from the object you pass: a
value inherited through the prototype (for example from a polluted
Object.prototype) is ignored. If you build options through a prototype (a
class instance with getters, Object.create(defaults)), copy the values onto a
plain object first.
Rut#
type Rut = string & { readonly __brand: 'Rut' } // branded validated-RUT stringA branded type representing a validated RUT string. Obtain one by narrowing with
isValidRut(). The brand records neither the
spelling nor strict: a Rut is the caller's string as it came in (dots or
not, k or K, surrounding whitespace), so store or compare clean(rut), and a
Rut narrowed without { strict: true } may still be a placeholder such as
11.111.111-1.
DecomposedRut#
type DecomposedRut = {
body: string // RUT body (7-8 digits, no leading zeros)
verifier: VerifierDigit // Verifier digit ('0'-'9' or 'K')
}Returned by decompose().
EqualsOptions#
type EqualsOptions = {
requireValid?: boolean // Require the shared value to pass Modulo 11 (default: true)
}Used by equals(). { requireValid: false } gives a
pure normalization comparison.
FormatOptions#
type FormatOptions = {
incremental?: boolean // Progressive formatting (default: false)
dots?: boolean // Include dot separators (default: true)
throwOnError?: boolean // Throw vs. return null (default: true)
}Used by format(). Incremental mode skips the Modulo 11
check, so only incremental: true turns it on; since 5.1.2, any other value,
such as the string 'false', formats normally. Incremental mode never throws: a
non-string gives ''. An empty string gives '' in every mode.
GenerateOptions#
type GenerateOptions = {
bodyLength?: 7 | 8 // Body digit count (default: 8)
format?: 'dotted' | 'compact' | 'hyphen' // Output shape (default: 'dotted')
count?: number // Return an array of this many RUTs
}Used by generate(). When count is given, generate()
returns string[]; otherwise it returns a single string. count has no upper
limit: cap it when it comes from user input.
SafeOptions#
type SafeOptions = {
throwOnError?: boolean // Throw vs. return null (default: true)
}Used by clean(), decompose(), getBody(), getVerifier(), calculateVerifier().
ValidateOptions#
type ValidateOptions = {
strict?: boolean // Reject suspicious placeholders (default: false)
}Used by validate().
VerifierDigit#
type VerifierDigit =
| '0' | '1' | '2' | '3' | '4' | '5'
| '6' | '7' | '8' | '9' | 'K'Errors#
InvalidRutError#
import { InvalidRutError } from 'rut.ts'
class InvalidRutError extends Error {
readonly code: 'INVALID_RUT'
// message: 'Invalid RUT input'
}Thrown by the safe helpers in their default mode (throwOnError: true) when the
input is not a valid RUT. Detect it with instanceof rather than matching the
message string.
import { clean, InvalidRutError } from 'rut.ts'
try {
clean('not-a-rut')
} catch (error) {
if (error instanceof InvalidRutError) {
// error.code === 'INVALID_RUT'
}
}Since 5.1.1, both instanceof InvalidRutError and error.code === 'INVALID_RUT'
also match an error thrown by another copy of rut.ts in the same process: the
ESM and CommonJS builds loaded side by side (your app imports it while a
dependency require()s it), another installed version, or another realm (an
iframe, vm). If the other copy may be 5.1.0 or older, check error.code: it
works with every version.
Quick reference#
| Function | Input | Output | Safe mode | Purpose |
|---|---|---|---|---|
validate() | unknown | boolean | N/A | Accept / reject (shape + verifier) |
isValidRut() | unknown | rut is Rut | N/A | Type guard narrowing to branded Rut |
isRutLike() | unknown | boolean | N/A | Fast bounded shape check |
format() | RUT string | formatted string | ✅ | Canonical display; validates in default mode |
clean() | RUT string | digit string | ✅ | Normalize shape (no verifier check) |
mask() | RUT string | masked string | null | ✅ | Partially hide a RUT for display |
equals() | unknown | boolean | N/A | Same-RUT comparison (validity-checked by default) |
decompose() | RUT string | { body, verifier } | ✅ | Split into parts |
getBody() | RUT string | body string | ✅ | Body only |
getVerifier() | RUT string | verifier char | ✅ | Verifier only |
calculateVerifier() | body string | verifier char | ✅ | Derive the Modulo 11 digit |
generate() | options | valid RUT(s) | N/A | Random valid RUT(s) for tests |
Common patterns#
Validation pipeline#
import { isRutLike, validate } from 'rut.ts'
function validateRutPipeline(input: unknown): boolean {
if (typeof input !== 'string') return false
if (!isRutLike(input)) return false // cheap reject
return validate(input, { strict: true }) // authoritative gate
}For production identity flows, validate(input, { strict: true }) is the
acceptance gate. clean() normalizes shape but does not prove the
verifier is correct — see Security.
Safe processing#
import { clean, decompose } from 'rut.ts'
function safeProcessRut(input: string) {
const cleaned = clean(input, { throwOnError: false })
if (!cleaned) return null
return decompose(cleaned, { throwOnError: false })
}Format + validate#
import { format, validate } from 'rut.ts'
function formatAndValidate(input: string) {
const formatted = format(input, { throwOnError: false })
if (!formatted) return { valid: false, formatted: null }
return { valid: validate(formatted), formatted }
}