Skip to content

Commit bb45e14

Browse files
authored
validation: add norwegian bank account number (#51)
* validation: add norwegian bank account number * changeset * fix readme * QA fixes * add tests for formatting
1 parent bd0c698 commit bb45e14

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

.changeset/tiny-bobcats-buy.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@obosbbl/validation": patch
3+
---
4+
5+
add method `no/validateAccountNumber()`
6+
7+
Validates that the input is a valid Norwegian bank account number.
8+
9+
```
10+
import { validateAccountNumber } from "@obosbbl/validation/no";
11+
12+
validateAccountNumber('12345678903') // => true
13+
14+
```

packages/validation/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ import { validateObosMembershipNumber } from '@obosbbl/validation/se';
157157
validateObosMembershipNumber('0000000') // => true
158158
```
159159

160+
### validateAccountNumber()
161+
162+
Validates that the value is a valid organization number. Validates the checksum of the number.
163+
164+
```js
165+
// 🇳🇴 example
166+
import { validateAccountNumber } from '@obosbbl/validation/no';
167+
validateAccountNumber('12345678903'); // => true
168+
169+
// 🇸🇪 example
170+
// TODO: implement
171+
```
172+
160173

161174
## Example usage with Zod
162175

packages/validation/src/no.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,29 @@ export function validateNationalIdentityNumber(
159159

160160
return isValidDate(year, month, day);
161161
}
162+
163+
type AccountNumberOptions = ValidatorOptions;
164+
165+
/**
166+
* Validates that the input value is a Norwegian bank account number (kontonummer).
167+
*
168+
* It validates the control digit.
169+
*
170+
* @example
171+
* ```
172+
* validateAccountNumber('XXXXXXXXXXX') // => true
173+
* ```
174+
*/
175+
export function validateAccountNumber(
176+
value: string,
177+
options: AccountNumberOptions = {},
178+
): boolean {
179+
if (options.allowFormatting) {
180+
// biome-ignore lint/style/noParameterAssign:
181+
value = stripFormatting(value);
182+
}
183+
184+
// Norwegian bank account numbers use mod 11 with one control digits.
185+
// The first one is calculated for all 11 digits
186+
return mod11(value, [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]);
187+
}

packages/validation/src/validation.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ describe('no', () => {
4747
expect(no.validateOrganizationNumber(input, options)).toBe(expected);
4848
});
4949

50+
test.each([
51+
['12345678903', true, undefined],
52+
['12345678901', false, undefined],
53+
['1234 56 78903', false, undefined],
54+
['95230820052', false, undefined],
55+
['9523.08.20059', false, undefined],
56+
57+
['1234 56 78903', true, { allowFormatting: true }],
58+
['1234 56 78903', false, { allowFormatting: false }],
59+
['9523.08.20059', true, { allowFormatting: true }],
60+
['9523.08.20059', false, { allowFormatting: false }],
61+
])('validateAccountNumber(%s) -> %s', (input, expected, options) => {
62+
expect(no.validateAccountNumber(input, options)).toBe(expected);
63+
});
64+
5065
test.each([
5166
['1234567', true, undefined],
5267
// too short

0 commit comments

Comments
 (0)