rut-kit
Core API reference for validating, formatting, and working with Chilean RUTs.
import { isValidRut, validateRut, formatRut, cleanRut, getRutCheckDigit, getErrorMessage } from 'rut-kit'isValidRut
Validates if a Chilean RUT is valid.
function isValidRut(rut: string): booleanParameters
| Name | Type | Description |
|---|---|---|
rut | string | RUT to validate |
Return
boolean - true if the RUT is valid, false otherwise.
Accepted Formats
- With dots and dash:
18.972.631-7 - Dash only:
18972631-7 - No formatting:
189726317
WARNING
Only accepts valid Chilean formats. Other separators (commas, asterisks) are rejected. Minimum RUT: 1.000.000.
Example
isValidRut('18.972.631-7') // true
isValidRut('18.972.631-0') // false
isValidRut('18,972,631-7') // falsevalidateRut
Validates a RUT and returns detailed information about the result.
function validateRut(rut: string): RutValidationResultParameters
| Name | Type | Description |
|---|---|---|
rut | string | RUT to validate |
Return
RutValidationResult - Object with the validation result:
// If valid
{ valid: true, rut: string }
// If invalid
{ valid: false, error: RutValidationError }Error Types
| Error | Description |
|---|---|
invalidFormat | Invalid format, incorrect length, or disallowed characters |
invalidCheckDigit | The check digit doesn't match |
Example
validateRut('18.972.631-7')
// { valid: true, rut: '189726317' }
validateRut('18.972.631-0')
// { valid: false, error: 'invalidCheckDigit' }
validateRut('18,972,631-7')
// { valid: false, error: 'invalidFormat' }formatRut
Formats a RUT to the specified format.
function formatRut(rut: string, format?: RutOutputFormat): stringParameters
| Name | Type | Description |
|---|---|---|
rut | string | RUT to format |
format | RutOutputFormat | Output format (optional) |
Output Formats
| Format | Result | Recommended Use |
|---|---|---|
undefined (default) | 18972631-7 | Storage |
'formatted' | 18.972.631-7 | Display to user |
'clean' | 189726317 | Comparisons, APIs |
Return
string - Formatted RUT.
Example
formatRut('189726317') // '18972631-7'
formatRut('189726317', 'formatted') // '18.972.631-7'
formatRut('189726317', 'clean') // '189726317'TIP
This function is permissive: it accepts any input separator. It uses cleanRut() internally.
cleanRut
Extracts only digits and the letter K from a RUT, removing any other character.
function cleanRut(rut: string): stringParameters
| Name | Type | Description |
|---|---|---|
rut | string | RUT to clean |
Return
string - Clean RUT (only digits and uppercase K). Removes leading zeros.
Example
cleanRut('18.972.631-7') // '189726317'
cleanRut('33.333.335-k') // '33333335K'
cleanRut('18,972,631-7') // '189726317'
cleanRut('18*972*631*7') // '189726317'TIP
Permissive function. Useful for cleaning data from Excel, PDFs, or other sources with non-standard separators.
getRutCheckDigit
Calculates the check digit of a RUT using the modulo 11 algorithm.
function getRutCheckDigit(rut: string): stringParameters
| Name | Type | Description |
|---|---|---|
rut | string | RUT body (without check digit) |
Return
string - Calculated check digit ('0'-'9' or 'K').
Example
getRutCheckDigit('18972631') // '7'
getRutCheckDigit('33333335') // 'K'
getRutCheckDigit('11111111') // '1'getErrorMessage
Converts an error type to a human-readable message.
function getErrorMessage(error: RutValidationError, messages?: RutErrorMessages): stringParameters
| Name | Type | Description |
|---|---|---|
error | RutValidationError | Error type to convert |
messages | RutErrorMessages | Custom messages (optional) |
Return
string - Human-readable error message.
Default Messages
| Error | Message |
|---|---|
invalidFormat | 'Invalid RUT format' |
invalidCheckDigit | 'Invalid check digit' |
Example
const result = validateRut('18.972.631-0')
if (!result.valid) {
getErrorMessage(result.error)
// 'Invalid check digit'
getErrorMessage(result.error, {
invalidCheckDigit: 'RUT is not valid'
})
// 'RUT is not valid'
}