isValidRut()
Type guard that validates a RUT and narrows the input to the branded Rut type at compile time.
isValidRut() applies the exact same validity rules as validate() (bounded input shape plus Modulo 11 verifier digit), but as a TypeScript type guard. When it returns true, the compiler narrows the checked value to the branded Rut type, so you can carry a "this string has been validated" guarantee through the rest of your code.
Basic Usage#
import { isValidRut, type Rut } from 'rut.ts'
const input: unknown = '12.345.678-5'
if (isValidRut(input)) {
// input is narrowed to Rut here
const rut: Rut = input
console.log(rut)
}Type Signature#
function isValidRut(rut: unknown, options?: ValidateOptions): rut is Rut
type ValidateOptions = {
strict?: boolean
}
type Rut = string & { /* brand */ }Parameters#
rut(unknown) - The value to check. Accepts unknown types for type safety.options(optional) - Validation optionsstrict(boolean, default:false) - If true, rejects suspicious repeated-digit placeholders
Return Value#
Returns a type predicate rut is Rut:
trueif the value is a valid RUT, narrowing it to the brandedRuttypefalseif the value is invalid or not a string
Examples#
Type Narrowing#
When the guard passes, the value is narrowed to Rut without any cast:
import { isValidRut, type Rut } from 'rut.ts'
function handle(input: unknown) {
if (isValidRut(input)) {
// No cast needed: input is now Rut
save(input)
}
}
function save(rut: Rut) {
// Only a validated Rut can reach here
console.log(rut)
}Enforcing Validation at Compile Time#
Because Rut is a branded type, the only way to obtain one is by passing the isValidRut() guard. Typing a function parameter or model field as Rut turns "I forgot to validate this string" into a compile-time error.
import { isValidRut, type Rut } from 'rut.ts'
interface Customer {
// The field can only ever hold a validated RUT
rut: Rut
}
function createCustomer(rut: Rut): Customer {
return { rut }
}
const raw = '12.345.678-5'
// createCustomer(raw) // Type error: string is not assignable to Rut
if (isValidRut(raw)) {
createCustomer(raw) // OK: raw is narrowed to Rut
}Same Rules as validate()#
isValidRut() accepts the same RUT shapes and rejects the same inputs as validate():
import { isValidRut } from 'rut.ts'
// Valid
isValidRut('12.345.678-5') // true
isValidRut('12345678-5') // true (compact + hyphen)
isValidRut('123456785') // true (compact)
isValidRut('14.625.621-K') // true (K verifier)
// Invalid
isValidRut('12.345.678-0') // false (wrong verifier)
isValidRut('12.345678-5') // false (malformed dot grouping)
isValidRut('abc') // false
isValidRut(null) // false
isValidRut(12345678) // false (not a string)Strict Mode#
import { isValidRut } from 'rut.ts'
// Suspicious repeated-digit placeholders
isValidRut('11.111.111-1', { strict: true }) // false
isValidRut('22.222.222-2', { strict: true }) // false
// Without strict mode, these pass
isValidRut('11.111.111-1') // trueUse Cases#
- ✅ Narrowing
unknowninput to a validatedRutwithout casts - ✅ Typing function parameters and model fields as
Rutto require validation - ✅ Pushing "already validated" guarantees through the type system
- ✅ Filtering or branching on validity in a type-safe way
- ✅ Rejecting test/placeholder RUTs with strict mode
Related Functions#
validate()- Boolean validation without type narrowingisRutLike()- Quick format check without verifier validation