luhn
A bank card number with a valid check digit (Luhn algorithm), 12–19 digits.
Signature
ts
function luhn(msg?: Message): Action<string>
function isLuhn(value: string): booleanParameters
| Parameter | Type | Description |
|---|---|---|
msg | Message | Custom error message |
Error codes
| Code | When | Default message |
|---|---|---|
luhn | Invalid checksum or format | Invalid card number |
You can change the texts: see Messages & i18n.
Notes
Catches typos and random digits. Doesn't check expiry, whether the card exists or the brand — tinyval-mask has cardBrand for that.
- Spaces and dashes are allowed: a masked value can be validated.
- The
isLuhnfunction is available separately — for checks without a schema. - Format the input with a mask from tinyval-mask.
Example
ts
const Payment = object({ card: pipe(string(), luhn()) });
isLuhn('4111 1111 1111 1111'); // true