Skip to content
Merged
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
14 changes: 14 additions & 0 deletions .changeset/tiny-bobcats-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@obosbbl/validation": patch
---

add method `no/validateAccountNumber()`

Validates that the input is a valid Norwegian bank account number.

```
import { validateAccountNumber } from "@obosbbl/validation/no";

validateAccountNumber('12345678903') // => true

```
13 changes: 13 additions & 0 deletions packages/validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ import { validateObosMembershipNumber } from '@obosbbl/validation/se';
validateObosMembershipNumber('0000000') // => true
```

### validateAccountNumber()

Validates that the value is a valid organization number. Validates the checksum of the number.

```js
// 🇳🇴 example
import { validateAccountNumber } from '@obosbbl/validation/no';
validateAccountNumber('12345678903'); // => true

// 🇸🇪 example
// TODO: implement
```


## Example usage with Zod

Expand Down
26 changes: 26 additions & 0 deletions packages/validation/src/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,29 @@ export function validateNationalIdentityNumber(

return isValidDate(year, month, day);
}

type AccountNumberOptions = ValidatorOptions;

/**
* Validates that the input value is a Norwegian bank account number (kontonummer).
*
* It validates the control digit.
*
* @example
* ```
* validateAccountNumber('XXXXXXXXXXX') // => true
* ```
*/
export function validateAccountNumber(
value: string,
options: AccountNumberOptions = {},
): boolean {
if (options.allowFormatting) {
// biome-ignore lint/style/noParameterAssign:
value = stripFormatting(value);
}

// Norwegian bank account numbers use mod 11 with one control digits.
// The first one is calculated for all 11 digits
return mod11(value, [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]);
}
15 changes: 15 additions & 0 deletions packages/validation/src/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ describe('no', () => {
expect(no.validateOrganizationNumber(input, options)).toBe(expected);
});

test.each([
['12345678903', true, undefined],
['12345678901', false, undefined],
['1234 56 78903', false, undefined],
['95230820052', false, undefined],
['9523.08.20059', false, undefined],

['1234 56 78903', true, { allowFormatting: true }],
['1234 56 78903', false, { allowFormatting: false }],
['9523.08.20059', true, { allowFormatting: true }],
['9523.08.20059', false, { allowFormatting: false }],
])('validateAccountNumber(%s) -> %s', (input, expected, options) => {
expect(no.validateAccountNumber(input, options)).toBe(expected);
});

test.each([
['1234567', true, undefined],
// too short
Expand Down