RUT_PATTERN
The RUT shapes validate() accepts, as plain regular-expression strings. Use them where code cannot run: an OpenAPI or JSON Schema pattern, a zod regex, a database CHECK constraint.
Basic Usage#
import { RUT_PATTERN } from 'rut.ts'
RUT_PATTERN.hyphen // '^[1-9][0-9]{6,7}-[0-9kK]$'
new RegExp(RUT_PATTERN.hyphen).test('12345678-5') // trueType Signature#
const RUT_PATTERN: {
readonly any: string
readonly compact: string
readonly hyphen: string
readonly dotted: string
}Each property is typed as its exact string literal, so the value shows up on hover.
The Four Patterns#
| Key | Matches | Value |
|---|---|---|
compact | 123456785 | ^[1-9][0-9]{6,7}[0-9kK]$ |
hyphen | 12345678-5 | ^[1-9][0-9]{6,7}-[0-9kK]$ |
dotted | 12.345.678-5 | ^[1-9][0-9]?[.][0-9]{3}[.][0-9]{3}-[0-9kK]$ |
any | any of the three | ^([1-9][0-9]{6,7}-?[0-9kK]|[1-9][0-9]?[.][0-9]{3}[.][0-9]{3}-[0-9kK])$ |
The strings are anchored, flagless and backslash-free, so they need no escaping when pasted into JSON, YAML or SQL. They carry the same rules as the validator: a 7 or 8 digit body and no leading zeros.
A pattern checks shape only. It never proves the verifier digit: about ten in eleven strings that match are not
real RUTs. Always run validate() wherever code can run.
Examples#
OpenAPI / JSON Schema#
rut:
type: string
pattern: '^[1-9][0-9]{6,7}-[0-9kK]$'Prefer hyphen or dotted in API schemas. any and compact match any 9-digit number, a phone number included.
zod#
The pattern documents the shape; the refinement does the real check.
import { z } from 'zod'
import { RUT_PATTERN, validate } from 'rut.ts'
const rut = z
.string()
.trim()
.regex(new RegExp(RUT_PATTERN.hyphen))
.refine((value) => validate(value))Database constraint#
ALTER TABLE customers
ADD CONSTRAINT customers_rut_shape
CHECK (rut ~ '^[1-9][0-9]{6,7}[0-9kK]$');The example uses PostgreSQL's ~ operator and the compact pattern, for a column that stores the cleaned form (123456785). The constraint keeps malformed values out even when a row is written by something that never calls your validation code.
Same result as isRutLike()#
import { RUT_PATTERN, isRutLike } from 'rut.ts'
const any = new RegExp(RUT_PATTERN.any)
any.test('12.345.678-5') // true
any.test('01234567-4') // false (leading zero)
any.test('123.456.789-5') // false (9-digit body)
// For inputs up to 64 characters:
isRutLike(input) === any.test(input.trim())A test suite in the library pins that equivalence, so the patterns and the validator cannot drift apart.
Other Regex Engines#
The patterns behave exactly like isRutLike() in JavaScript. Other engines read the anchors differently:
- Python: use
re.fullmatch. Withre.search,$also matches before a trailing newline. - Perl:
$also matches before a trailing newline. - Ruby:
^and$match at every line. Replace them with\Aand\z. - Java (
find()), .NET, PHP/PCRE and MySQL 8 (REGEXP_LIKE, which uses ICU):$also matches before a final line break (in Java and ICU, also before U+0085, U+2028 and U+2029). Replace$with\z(\\zinside a MySQL string literal). - Go and SQLite give the same result as JavaScript.
Use Cases#
- ✅ Document the expected RUT format in an API contract
- ✅ Reject malformed values at the database, whatever wrote them
- ✅ Share one shape rule between a TypeScript frontend and a backend in another language
- ❌ Decide whether a RUT is real (use
validate())
Related Functions#
validate()- Shape plus verifier digitisRutLike()- The same shape check, as a functionisValidRut()- Type guard that narrows to the brandedRut