insurello / elm-swedish-bank-account-number / SwedishBankAccountNumber

This library lets you validate Swedish bank account numbers.

First, validate a clearing number using SwedishBankAccountNumber.ClearingNumber.fromString.

Then, validate an account number using SwedishBankAccountNumber.create.

Main


type SwedishBankAccountNumber

Represents a Swedish bank account number.

This is an example of a valid Swedish bank account number:

9420 - 4172385

A bank account number consists of two parts:

  1. The clearing number: 9420
  2. The account number: 4172385

The only way to create a value of the SwedishBankAccountNumber type is by calling create. This way, if you encounter a SwedishBankAccountNumber you always know that it’s valid.

SwedishBankAccountNumber also contains information about which Swedish bank the bank account number is for.


type Error
    = BadAccountNumberLength Basics.Int
    | BadChecksum

Trying to construct a SwedishBankAccountNumber can fail in two ways:

(Constructing a ClearingNumber can fail in its own ways – see SwedishBankAccountNumber.ClearingNumber.Error.)

create : ClearingNumber -> String -> Result Error SwedishBankAccountNumber

Validate and construct a SwedishBankAccountNumber.

First, you need to get a ClearingNumber via SwedishBankAccountNumber.ClearingNumber.fromString.

Then, pass the validated ClearingNumber and an unvalidated account number string to get a full bank account number.

The account number string is allowed to have any kind of crazy formatting. The function simply takes all the digits of the string and discards all other characters. The following all mean the same thing:

4172385
417 23 85
417,23,85
abc417-23#85!

If you have a form field for the account number, you might want to normalize it on blur. You can use String.filter Char.isDigit myString to do so.

toRecord : SwedishBankAccountNumber -> { bankName : String, clearingNumber : String, accountNumber : String }

When you need to display a SwedishBankAccountNumber or send it via HTTP to your backend, use this function to get all of its data.

Helpers


type AccountNumberLength
    = FixedLength Basics.Int
    | Range Basics.Int Basics.Int

Represents the length constraints of an account number.

The most common value is FixedLength 7, which all modern “type 1” accounts have.

Older “type 2” accounts usually have FixedLength 10.

Then there are some special cases:

See https://github.com/jop-io/kontonummer.js/issues/6 for more information.

getAccountNumberLength : ClearingNumber -> AccountNumberLength

Get the length constraints of an account number. Since banks have different rules this depends on the clearing number.

Let’s say you have two form fields – one for the clearing number, and one for the account number. After the clearing number has been filled, you might want to show how long the account number is expected to be. Then this function will come in handy.

By the way – clearing numbers are always 4 digits, except Swedbank clearing numbers starting with 8 which are 5 digits.