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') // 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 patterns
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('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
// Without strict mode, these pass
validate('11.111.111-1') // true
validate('22.222.222-2') // trueType 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
format()- Format a RUT stringclean()- Remove formattingisRutLike()- Quick format check without validation
Last updated on