Skip to content

validate()

Validates a given RUT string, checking bounded input shape and verifier digit.

Try it#

isRutLike()true
validate()true
validate({ strict: true })true
clean()123456785
format()12.345.678-5
decompose().body12345678
decompose().verifier5
calculateVerifier(body)5
verifier if input is a bodynot applicable

Basic Usage#

TypeScript
import { validate } from 'rut.ts'
 
validate('12.345.678-5') // true
validate('12.345.678-0') // false (wrong verifier)
validate('invalid') // false

Type Signature#

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

Return Value#

Returns boolean:

  • true if the RUT is valid
  • false if the RUT is invalid or not a string

Examples#

Basic Validation#

TypeScript
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) // false

Strict Mode#

Strict mode rejects suspicious patterns like repeated digits:

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

Accepted Input Shapes#

validate() accepts the common Chilean RUT shapes and rejects ambiguous grouping before calculating the verifier digit:

TypeScript
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

Inputs are length-capped before parsing so very large strings fail fast instead of spending time in regular expressions.

Type Safety#

The function accepts unknown input for better type safety:

TypeScript
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