# ViraStack Mask (full) > `@virastack/mask` (2.x): zero-dependency input masks for React Hook Form. Headless, no CSS, zero runtime dependencies. The form stores raw values; inputs show masked text. ## Install ```bash npm install @virastack/mask # peers: react >= 18.2, react-hook-form >= 7.55 ``` Docs: https://virastack.com/mask/docs · Index: https://virastack.com/mask/llms.txt · Repo: https://github.com/virastack/form/tree/main/packages/mask ## Critical rules for agents 1. Call `useViraMask(form, schema)`: the first argument is what `useForm()` returns. (v1's `useViraMask({ form, schema })` no longer exists.) 2. Schema keys are form field paths. `phone`, `iban`, `currency` and `number` take `country: "TR"` for the Turkish layout; defaults are global. For nested fields quote the key (`"address.zip"`) or set `name`. 3. Spread the returned object onto the input as is. There is no `rawValue`; read raw values with `form.watch(name)` / `getValues`. 4. Don't pass `value`: the input is uncontrolled (`defaultValue`). `reset()` and `setValue()` update it; pass raw values (ISO for dates). 5. With a resolver (Zod, Valibot), wrap it: `resolver: maskResolver(masks, zodResolver(schema))` or `maskResolver(masks, schema)`. Otherwise mask validation is skipped. 6. In Server Components, Server Actions and shared schema files import from `@virastack/mask/server` (the root entry is `"use client"`). 7. Errors are keys (`tckn.invalid`, `phone.incomplete`), not messages. Map them in your i18n layer. ## Quick start ```tsx import { useForm } from "react-hook-form" import { useViraMask } from "@virastack/mask" function Checkout() { const form = useForm<{ phone: string; card: string; cvv: string }>() const { phone, card, cvv } = useViraMask(form, { phone: { preset: "phone", country: "TR" }, card: "card", cvv: { preset: "cvv", from: "card" }, }) return (
) } // form.watch("phone") → "5551234567" ``` ## Presets | Preset | Display | Form value | Error keys | | --- | --- | --- | --- | | `phone` | `+90 (555) 123 45 67`, others `+4915112345678` | `+905551234567` (E.164) | `phone.incomplete` | | `phone` + `country: "TR"` | `(555) 123 45 67` (type +90, 0090, 0 or 555) | `5551234567` | `phone.incomplete` | | `iban` | `GB82 WEST 1234 5698 7654 32` (any country) | `GB82WEST12345698765432` | `iban.incomplete`, `iban.invalid` | | `iban` + `country: "TR"` | `TR33 0006 1005 1978 6457 8413 26` | `TR330006100519786457841326` | `iban.incomplete`, `iban.invalid` | | `card` | `4111 1111 1111 1111`, Amex `3782 822463 10005` | digits | `card.incomplete`, `card.invalid` (Luhn) | | `cvv` | 3 digits, 4 when `from` card is Amex | digits | `cvv.incomplete` | | `expiry` | `12/28`, past months invalid | `1228` | `expiry.incomplete`, `expiry.invalid`, `expiry.min` | | `tckn` | `10000000146` | digits | `tckn.incomplete`, `tckn.invalid` | | `vkn` | `1234567890` | digits | `vkn.incomplete`, `vkn.invalid` | | `plate` (2.3+) | `34 ABC 123`; province 01–81, then 1 letter + 4–5, 2 letters + 3–4 or 3 letters + 2–3 digits ([article 34 of the vehicle registration regulation](https://www.mevzuat.gov.tr/anasayfa/MevzuatFihristDetayIframe?MevzuatTur=7&MevzuatNo=24356&MevzuatTertip=5)) | `34ABC123` | `plate.incomplete`, `plate.invalid` | | `fullName` (2.2+) | `Ömer Gülçiçek`, letters of any language and single spaces, 70 characters at most | same text | none | | `currency` | `1,234.56` (no symbol); with a code `$1,234.56` (Intl) | `1234.56` | `currency.min`, `currency.max` | | `currency` + `country: "TR"` | `₺1.234,56` | `1234.56` | `currency.min`, `currency.max` | | `number` | `1,234` | `1234` | `number.min`, `number.max` | | `number` + `country: "TR"` | `1.234` | `1234` | `number.min`, `number.max` | | `date` | `29/07/2026` | `2026-07-29` (ISO) | `date.incomplete`, `date.invalid`, `date.min`, `date.max`, `date.minAge`, `date.beforeFrom` | ## Schema entry ```ts useViraMask(form, { tckn: "tckn", // preset name birthDate: { preset: "date", date: { max: "today", minAge: 18 } }, // preset + overrides phone: { preset: "phone", country: "TR", rules: { required: "required" } }, // national layout + register rules price: { preset: "currency", from: "currency" }, // linked field endDate: { preset: "date", from: "startDate" }, // can't be before start "address.zip": { mask: "99999" }, // nested field iban: { preset: "iban", country: "TR", output: "masked" }, // write masked text to the form card: { preset: "card", onComplete: () => form.setFocus("expiry") }, }) ``` Returned props per key: `name`, `ref`, `defaultValue`, `onChange`, `onBlur`, `onCompositionEnd`, `type`, `inputMode`, `autoComplete`, `disabled?`, `aria-invalid?`. ## Custom masks - Pattern: `9` digit, `a` letter, `*` letter or digit, `\` escape, `[...]` optional part, `{...}` fixed part kept in the value. - `tokens: { h: /[0-9a-f]/i }`, `transform: "uppercase" | "lowercase"`, `normalize: (value) => string`. - Dynamic: `mask: ["999-999", "9999-9999"]` or `mask: (value, ctx) => pattern` (`ctx.from` is the linked field). - `number: { min, max, scale, signed, locale }`, `currency: "USD" | { code, locale, min, max, scale }`, `date: { format, min, max, minAge }` (formats `DD/MM/YYYY`, `MM/DD/YYYY`, `YYYY-MM-DD`, `DD.MM.YYYY`, `MM/YY`, `MM/YYYY`). - `mask`, `number`, `currency` and `date` exclude each other (type error). - `validate: (value, ctx) => true | false | "my.key"` runs after built-in checks. - `definePreset("orderNo", { mask: "aa-9999", transform: "uppercase" })` → error keys `orderNo.*`. ## Server ```ts "use server" import { parseMasked } from "@virastack/mask/server" export async function checkout(formData: FormData) { const result = await parseMasked(checkoutMasks, formData, zodSchema) // schema optional (any Standard Schema) if (!result.success) return { errors: result.errors } // { phone: "phone.incomplete" } // result.data → raw values, nested names expanded } ``` ## Helpers `createMask(spec)` → `{ format, parse, validate, isComplete }` · `getCardType` · `isValidCard`, `isValidLuhn`, `isValidTCKN`, `isValidVKN`, `isValidIBAN` (any country; country code sets the length), `isValidPlate` (2.3+; spaces, hyphens and lowercase OK; Latin A–Z, Q/W/X included; name plates and special plates not checked) · `toMinorUnits("1234.56", "TRY") → 123456` · `getTurkishBank("TR63 0006 2…") → { code: "0062", name: "T. GARANTİ BANKASI A.Ş." }` (from the IBAN's bank code, partial input OK, no checksum check; `turkishBanks` is the full TCMB list of 70, 2.1+) · `maskResolver(masks, resolverOrStandardSchema)`. ## Test data (2.3+) Tests and seed data only: a generated value can belong to a real person or account. ```ts import { generateIBAN, generateTCKN } from "@virastack/mask/testing" // not exported from the root or /server generateTCKN() // passes isValidTCKN, new value per call generateTCKN({ seed: 1 }) // "49537717654" on every run (integer seed) generateIBAN() // Turkish IBAN, random bank from turkishBanks, no spaces generateIBAN("TR", { bank: "0062" }) // Garanti; getTurkishBank finds the bank generateIBAN("DE", { seed: 1 }) // any registry country; digits-only BBAN, passes isValidIBAN, not national BBAN rules ``` Unknown country, unknown bank code or `bank` outside `TR` throws. ## Upgrade from v1 `useViraMask({ form, schema })` → `useViraMask(form, schema)`; `rawValue` removed; `phone` → `{ preset: "phone", country: "TR" }` for the Turkish layout or E.164 `"phone"`; `iban` → `{ preset: "iban", country: "TR" }` (value keeps `TR`) or global `"iban"`; `taxNumber` → `vkn`; `numeric` → `number`; `email`, `url`, `username`, `alpha`, `password`, `text`, `zipCode` removed; `resolveMask` → `mask` function; `errorMessage` → error keys; `mergeRefs`, `applyMask`, `formatCurrency` removed (use `createMask`). Full guide: https://virastack.com/mask/docs/upgrade