validate()
Validates a given RUT string, checking bounded input shape and verifier digit.
Try it#
Basic Usage#
import { validate } from 'rut.ts'
validate('12.345.678-5') // true
validate('12.345.678-0') // false (wrong verifier)
validate('invalid') // falseType Signature#
function validate(rut: unknown, options?: ValidateOptions): boolean
type ValidateOptions = {
strict?: boolean
}Parameters#
rut(unknown) - The RUT to validate. Accepts unknown types for type safety.options(optional) - Validation optionsstrict(boolean, default:false) - If true, rejects suspicious repeated-digit placeholders
Return Value#
Returns boolean:
trueif the RUT is validfalseif the RUT is invalid or not a string
Examples#
Basic Validation#
import { validate } from 'rut.ts'
// Valid RUTs
validate('12.345.678-5') // true
validate('12345678-5') // true (without dots)
validate('123456785') // true (no formatting)
validate('14.625.621-K') // true (K verifier)
// Invalid RUTs
validate('12.345.678-0') // false (wrong verifier)
validate('12.345678-5') // false (malformed dot grouping)
validate('abc') // false
validate('') // false
validate(null) // false
validate(123) // falseStrict Mode#
Strict mode rejects suspicious patterns like repeated digits:
import { validate } from 'rut.ts'
// Suspicious RUTs (technically valid, but unlikely to be real)
validate('11.111.111-1', { strict: true }) // false
validate('22.222.222-2', { strict: true }) // false
validate('33.333.333-3', { strict: true }) // false
validate('8.888.888-K', { strict: true }) // false
// Without strict mode, these pass
validate('11.111.111-1') // true
validate('22.222.222-2') // trueAccepted Input Shapes#
validate() accepts the common Chilean RUT shapes and rejects ambiguous grouping before calculating the verifier digit:
import { validate } from 'rut.ts'
validate('12.345.678-5') // true
validate('12345678-5') // true
validate('123456785') // true
validate('12.345678-5') // false
validate('12345.678-5') // false
validate('12,345,678-5') // false
validate('12.345.6785') // false (dotted shape requires the verifier hyphen)Inputs are length-capped before parsing so very large strings fail fast instead of spending time in regular expressions.
Leading Zeros Are Rejected (5.0.0)#
Since 5.0.0, a canonical RUT carries no leading-zero padding: validate(), isValidRut() and isRutLike() reject it. Padding forms an unbounded family of distinct strings for one RUT (12345678 = 012345678 = 0012345678 = …), which must not slip past an identity or uniqueness gate.
import { validate } from 'rut.ts'
validate('12345674') // true
validate('012345674') // false (leading zero — not canonical)
validate('012.345.678-5') // falseIngesting zero-padded or messy input? Normalize first, then validate the normalized value — the two-line recipe:
const rut = clean(raw, { throwOnError: false }) // '0012345674' → '12345674' | null
if (rut !== null && validate(rut)) store(rut) // canonical, Modulo 11 verifiedclean() collapses the zero-padded family into one canonical string, and
validate() then proves the verifier. validate() deliberately has no
relaxation flags — messy input gets normalized before the gate, never waved
through it.
Type Safety#
The function accepts unknown input for better type safety:
import { validate } from 'rut.ts'
function processUserInput(input: unknown) {
if (validate(input)) {
// TypeScript doesn't narrow type here, but validation passes
console.log('Valid RUT')
}
}
processUserInput('12.345.678-5') // Valid RUT
processUserInput(12345678) // Silent fail (returns false)
processUserInput(null) // Silent fail (returns false)Use Cases#
- ✅ Form validation
- ✅ API input validation
- ✅ Database validation before storage
- ✅ Type-safe validation with unknown inputs
- ✅ Detecting test/placeholder RUTs with strict mode
Related Functions#
isValidRut()- Type guard that narrows to the brandedRuttypeformat()- Format a RUT stringclean()- Remove formattingisRutLike()- Quick format check without validation