Skip to Content
🎉 Rut.ts 3.4.0 is released! Check out Safe Mode and improved API.

validate()

Validates a given RUT string, checking both format and verifier digit.

Basic Usage

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

Type 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 options
    • strict (boolean, default: false) - If true, rejects suspicious patterns

Return Value

Returns boolean:

  • true if the RUT is valid
  • false if 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('abc') // false validate('') // false validate(null) // false validate(123) // false

Strict 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 // Without strict mode, these pass validate('11.111.111-1') // true validate('22.222.222-2') // true

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
Last updated on