Skip to content

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#

TypeScript
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#

TypeScript
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 options
    • strict (boolean, default: false) - If true, rejects suspicious repeated-digit placeholders

Return Value#

Returns a type predicate rut is Rut:

  • true if the value is a valid RUT, narrowing it to the branded Rut type
  • false if the value is invalid or not a string

Examples#

Type Narrowing#

When the guard passes, the value is narrowed to Rut without any cast:

TypeScript
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.

TypeScript
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():

TypeScript
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#

TypeScript
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') // true

Use Cases#

  • ✅ Narrowing unknown input to a validated Rut without casts
  • ✅ Typing function parameters and model fields as Rut to 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
  • validate() - Boolean validation without type narrowing
  • isRutLike() - Quick format check without verifier validation