Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: IBAN validation #290

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions library/src/validations/iban/iban.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { describe, expect, test } from 'vitest';
import { iban } from './iban.ts';

describe('iban', () => {
test('should pass only valid IBAN', () => {
const validate = iban();

const value1 = 'AT483200000012345864';
expect(validate._parse(value1).output).toBe(value1);
const value2 = 'BE71096123456769';
expect(validate._parse(value2).output).toBe(value2);
const value3 = 'BG18RZBB91550123456789';
expect(validate._parse(value3).output).toBe(value3);
const value4 = 'HR1723600001101234565';
expect(validate._parse(value4).output).toBe(value4);
const value5 = 'HR1723600001101234565';
expect(validate._parse(value5).output).toBe(value5);
const value6 = 'CY21002001950000357001234567';
expect(validate._parse(value6).output).toBe(value6);
const value7 = 'CZ5508000000001234567899';
expect(validate._parse(value7).output).toBe(value7);
const value8 = 'DK9520000123456789';
expect(validate._parse(value8).output).toBe(value8);
const value9 = 'EE471000001020145685';
expect(validate._parse(value9).output).toBe(value9);
const value10 = 'FI1410093000123458';
expect(validate._parse(value10).output).toBe(value10);
const value11 = 'FR7630006000011234567890189';
expect(validate._parse(value11).output).toBe(value11);
const value12 = 'DE75512108001245126199';
expect(validate._parse(value12).output).toBe(value12);
const value13 = 'GR9608100010000001234567890';
expect(validate._parse(value13).output).toBe(value13);
const value14 = 'HU93116000060000000012345676';
expect(validate._parse(value14).output).toBe(value14);
const value15 = 'IE64IRCE92050112345678';
expect(validate._parse(value15).output).toBe(value15);
const value16 = 'IT60X0542811101000000123456';
expect(validate._parse(value16).output).toBe(value16);
const value17 = 'LV97HABA0012345678910';
expect(validate._parse(value17).output).toBe(value17);
const value18 = 'LT601010012345678901';
expect(validate._parse(value18).output).toBe(value18);
const value19 = 'LU120010001234567891';
expect(validate._parse(value19).output).toBe(value19);
const value20 = 'MT31MALT01100000000000000000123';
expect(validate._parse(value20).output).toBe(value20);
const value21 = 'PL10105000997603123456789123';
expect(validate._parse(value21).output).toBe(value21);
const value22 = 'PT50002700000001234567833';
expect(validate._parse(value22).output).toBe(value22);
const value23 = 'RO09BCYP0000001234567890';
expect(validate._parse(value23).output).toBe(value23);
const value24 = 'SK8975000000000012345671';
expect(validate._parse(value24).output).toBe(value24);
const value25 = 'SI56192001234567892';
expect(validate._parse(value25).output).toBe(value25);
const value26 = 'ES7921000813610123456789';
expect(validate._parse(value26).output).toBe(value26);
const value27 = 'SE7280000810340009783242';
expect(validate._parse(value27).output).toBe(value27);
const value28 = 'NL02ABNA0123456789';
expect(validate._parse(value28).output).toBe(value28);
const value29 = 'SI56101000057903854';
expect(validate._parse(value29).output).toBe(value29);
});

test('should reject invalid IBAN', () => {
const validate = iban();

expect(validate._parse('').issues).toBeTruthy();
expect(validate._parse('GB82WEST12345698').issues).toBeTruthy();
expect(validate._parse('DE8937040044053201300').issues).toBeTruthy();
expect(validate._parse('GB82 WEST 1234 5698 7654 32').issues).toBeTruthy();
expect(validate._parse('XX82WEST12345698765432').issues).toBeTruthy();
expect(validate._parse('GB82TEST12345698765432').issues).toBeTruthy();
expect(validate._parse('FR7630006000011234567890289').issues).toBeTruthy();
expect(validate._parse('abcdefghij1234567890').issues).toBeTruthy();
expect(validate._parse('GB82!EST12345698765432').issues).toBeTruthy();
expect(validate._parse('GB82!NL91ABNA0417164300').issues).toBeTruthy();
expect(validate._parse('GB12 3456 7890 1234 5678 90').issues).toBeTruthy();

expect(validate._parse('XX22YYY1234567890123').issues).toBeTruthy();
expect(validate._parse('FR14 2004 1010 0505 0001 3').issues).toBeTruthy();
expect(validate._parse('FR7630006000011234567890189@').issues).toBeTruthy();
expect(
validate._parse('FR7630006000011234567890189😅').issues
).toBeTruthy();
expect(
validate._parse('FR763000600001123456!!🤨7890189@').issues
).toBeTruthy();
});

test('should return custom error message', () => {
const error = 'Value is invalid IBAN!';
const validate = iban(error);
expect(validate._parse('test').issues?.[0].message).toBe(error);
});
});
149 changes: 149 additions & 0 deletions library/src/validations/iban/iban.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import type { BaseValidation, ErrorMessage } from '../../types/index.ts';
import { actionIssue, actionOutput } from '../../utils/index.ts';

/**
* Iban validation type.
*/
export type IbanValidation<TInput extends string> = BaseValidation<TInput> & {
/**
* Validation type.
*/
type: 'iban';
/**
* IBAN validation function.
*/
requirement: (input: string) => boolean;
};

/**
* Creates a validation function that validates an [IBAN](https://en.wikipedia.org/wiki/International_Bank_Account_Number).
*
* @param message The error message.
*
* @returns A validation function.
*/
export function iban<TInput extends string>(
message: ErrorMessage = 'Invalid IBAN'
): IbanValidation<TInput> {
return {
type: 'iban',
async: false,
message,
requirement: isIBAN,
_parse(input) {
return !this.requirement(input.toUpperCase())
? actionIssue(this.type, this.message, input, this.requirement)
: actionOutput(input);
},
};
}

/**
* IBAN lengths for each country mapped to country code
*/
const COUNTRY_CODE_IBAN_LENGTHS: Record<string, number> = {
AD: 24,
AE: 23,
AT: 20,
AZ: 28,
BA: 20,
BE: 16,
BG: 22,
BH: 22,
BR: 29,
CH: 21,
CR: 21,
CY: 28,
CZ: 24,
DE: 22,
DK: 18,
DO: 28,
EE: 20,
ES: 24,
FI: 18,
FO: 18,
FR: 27,
GB: 22,
GI: 23,
GL: 18,
GR: 27,
GT: 28,
HR: 21,
HU: 28,
IE: 22,
IL: 23,
IS: 26,
IT: 27,
JO: 30,
KW: 30,
KZ: 20,
LB: 28,
LI: 21,
LT: 20,
LU: 20,
LV: 21,
MC: 27,
MD: 24,
ME: 22,
MK: 19,
MR: 27,
MT: 31,
MU: 30,
NL: 18,
NO: 15,
PK: 24,
PL: 28,
PS: 29,
PT: 25,
QA: 29,
RO: 24,
RS: 22,
SA: 24,
SE: 24,
SI: 19,
SK: 24,
SM: 27,
TN: 24,
TR: 26,
};

/**
* Upper case letter regex.
*/
const UPPERCASE_LETTER_REGEX = /[A-Z]/gu;

/**
* Checks whether a string with numbers corresponds to valid IBAN number.
*
* @param input The input to be checked.
*
* @returns Whether input is valid.
*/
function isIBAN(input: string) {
const countryCode = input.slice(0, 2);
if (COUNTRY_CODE_IBAN_LENGTHS[countryCode] !== input.length) {
return false;
}

const rearranged = input.slice(4) + countryCode + input.slice(2, 4);
const numbers = rearranged.replace(UPPERCASE_LETTER_REGEX, (char) =>
(char.charCodeAt(0) - 55).toString()
);

return mod97(numbers) === 1;
}

/**
* Calculate modul 97 on input string
*
* @param input string of numbers
*
* @returns checksum
*/
function mod97(input: string): number {
let checksum = 0;
for (let i = 0; i < input.length; i++) {
checksum = (checksum * 10 + parseInt(input[i], 10)) % 97;
}
return checksum;
}
1 change: 1 addition & 0 deletions library/src/validations/iban/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './iban.ts';
1 change: 1 addition & 0 deletions library/src/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './endsWith/index.ts';
export * from './equal/index.ts';
export * from './excludes/index.ts';
export * from './finite/index.ts';
export * from './iban/index.ts';
export * from './imei/index.ts';
export * from './includes/index.ts';
export * from './integer/integer.ts';
Expand Down
9 changes: 9 additions & 0 deletions website/src/routes/api/(validations)/iban/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: iban
contributors:
- ariskemper
---

# iban

> The content of this page is not yet ready. Until then just use the [source code](https://github.com/fabian-hiller/valibot/blob/main/library/src/validations/iban/iban.ts).
1 change: 1 addition & 0 deletions website/src/routes/api/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- [endsWith](/api/endsWith/)
- [excludes](/api/excludes/)
- [finite](/api/finite/)
- [iban](/api/iban/)
- [imei](/api/imei/)
- [includes](/api/includes/)
- [integer](/api/integer/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Validation functions examine the input and, if the input does not meet a certain
'endsWith',
'excludes',
'finite',
'iban',
'includes',
'integer',
'ip',
Expand Down