swantzter / kontonummer

Parse, validate and format Swedish bank account numbers
https://kontonummer.se
MIT License
5 stars 2 forks source link
bank-account bank-accounts bankgirot banking plusgirot-numbers swedish-bank validation

Kontonummer

JavaScript Style Guide test release codecov

This is a reimagination of jop-io/kontonummer.js with some additional goals:

Some Code (c) Jonas Persson and Tobbe Lundberg which they have gracefully released under a MIT license. See LICENCE

This implementation is written in TypeScript but the following specification should be applicable to other languages as well. But some language specific modifications may be required.

Important Caveat

As explained in the research section below there are some bank account numbers that is impossible to validate (as they do not have a check digit) that are indistinguishable from validatable accounts. I recommend using this library on form input fields but do not prevent form submission if the account number is reported as invalid. A good idea would be something like a warning saying "there is a chance this is not a valid bank account number you may want to double check."

Specification [v1]

Class

The package should include a class that which should be the return value of parse

class Kontonummer {
  constructor (sortingCode: string | number, accountNumber: string | number, options?: InitOptions)
  constructor (sortingCodeAndAccountNumber: string | number, options?: InitOptions)
}

InitOptions

Properties

The class should expose the following properties once initialised

class Kontonummer {
  readonly bankName: string
  readonly sortingCode: string
  readonly accountNumber: string
  readonly type: 1 | 2
  readonly comment: 1 | 2 | 3
  readonly valid: boolean // only relevant in `lax` mode
}

Errors

All methods except for validate should throw an exception or return an error as a second return value. Error handling may be different depending on language. The exception/error class should be prefixed with Kontonummer.

Parse

The class should include a static parse method that creates a new instance of the class. It should take the same arguments as the class constructor. This function should also be an exported standalone method of the package.

Pseudocode:

class Kontonummer {
  static parse (...args) {
    return new Kontonummer(...args)
  }
}

export const parse = Kontonummer.parse

Validate

The class should include a static validate method that attempts to parse the provided input and returns a boolean that indicates if it succeeded. validate should not accept any options. This function should also be an exported standalone method of the package.

Pseudocode:

class Kontonummer {
  validate (sortingCode, accountNumber)
  validate (sortingCodeAndAccountNumber) {
    try {
      new Kontonummer(sortingCode, accountNumber)
      return true
    } catch {
      return false
    }
  }
}

export const validate = Kontonummer.validate

Format

Format key:

The class should include a public format method that returns the sortingCode and accountNumber in one string. Some different formats should be available. If no argument is provided it should default to numeric.

type Format = 'numeric' | 'pretty'
type Part = 'full' | 'sortingCode' | 'accountNumber'

class Kontonummer {
  format (format?: Format = 'numeric', part?: Part = 'full'): string
}
Name Format
numeric
(default)
S[C]AC
where the account number is padded to the appropriate max length depending on account type
pretty Depends on type and bank, see research below

[] brackets marks optional

Research

Moved to swantzter.se where I plan to publish research on other topics as well