Being a small Swift library to check the validity of IBAN codes. It's based on the the ISO 13616:2007 standard, which defines a checksum process described here.
SPM:
- Add
.package(url: "https://github.com/louis-prudhomme/Ray.git"),
to yourPackage.swift
- Run
swift package update
- That should be it.
Carthage:
Not yet. Maybe one day. You may want to fall back to the original library this repository is a fork of: timd/IBANValidator.
- Import the framework wherever you want to use it:
import Ray
- Test the IBAN as such:
let validIbanNumber = Iban(from: "AL90208110080000001039531801")
let invalidIbanNumber = Iban(from: "dqzdqpzodzkqpdozqdu_ehçàçéi")
let anotherInvalidIbanNumber = Iban(from: "AL90208110080000001039531801921902118972918273")
print("\(validIbanNumber.isValid)") // true
print("\(invalidIbanNumber.isValid)") // false
print("\(anotherInvalidIbanNumber.isValid)") // false
// OR
do {
IbanValidator.validateOrThrow(for: "FR7710293109283091830989776654")
} catch {
(error as? IbanValidatorError)?.violations.map { print("Iban invalid because \($0)")}
}
Errors are returned as an IbanValidatorError
. It bears all the violations of the IBAN which allow you to provide feedback to your users.
invalidChecksum
: IBAN has an invalid checksum.unknownCountryCode
: could not parse the country code (first two letters)exceedsMaximumLength
: IBAN is over 34 charactersexceedsCountryLengthSpecification
: IBAN exceeds what length is specified for the countrycontainsForbiddenCharacters
: IBAN contains forbidden characters. Essentially, any non-alphanumeric character.
As a convenience for populating things like picker lists, Ray
exposes the SupportedCountry
enum
. It is CaseIterable
in order to build picker lists.
Use the Iban#formatted
method to format your IBAN. Formatting depends on the country code of your IBAN.
- This library uses BigInt, as validating an IBAN requires computing the mod(97) of 34-digit integers.
- This library is based off timd/IBANValidator ; many thanks for their excellent work !
This library should only be used to check for an IBAN base conformity. It must not be the only check your program performs on user-given IBANs, but rather a passage before tapping onto IBAN.com validator.
Use at your own risk.