Skip to main content

Crate uk_bank_details_validator

Crate uk_bank_details_validator 

Source
Expand description

§uk-bank-details-validator

crates.io docs.rs CI License: MIT

Validate UK bank details (a sort code and account number) using the Vocalink modulus checking specification.

Modulus checking tells you whether an account number is possible for a sort code. It catches typos and swapped digits before a payment is made, which makes it useful on payment forms, Direct Debit sign-ups and payout flows.

use uk_bank_details_validator::{BankDetails, Validation};

let details = BankDetails::new("08-99-99", "66374958")?;

match details.validate() {
    Validation::Valid => println!("details look correct"),
    Validation::Invalid => println!("please check the sort code and account number"),
    Validation::Unchecked => println!("these details can't be checked"),
}

§What it does, and doesn’t

Does

  • Implements all three methods: MOD10, MOD11 and DBLAL
  • Handles the specification’s exceptions 1 to 14, including sort code substitution
  • Standardises 6, 7 and 9-digit account numbers
  • Accepts 089999, 08-99-99 and 08 99 99
  • Bundles the Vocalink tables: no network access, no setup
  • Works without std and without an allocator

Doesn’t

  • Confirm that an account exists or is open
  • Check the account holder’s name (that’s Confirmation of Payee)
  • Guarantee that a payment will succeed

Valid means “these details could be real”, not “these details are real”.

§Installation

cargo add uk-bank-details-validator

§Usage

§Parse, then validate

Creating BankDetails checks the format and returns a ParseError for bad input. validate() then runs the modulus check and always returns a Validation:

use uk_bank_details_validator::{BankDetails, ParseError};

assert!(matches!(BankDetails::new("08-99", "66374958"), Err(ParseError::SortCode(_))));
assert!(matches!(BankDetails::new("089999", "12AB"), Err(ParseError::AccountNumber(_))));

§The three results

ResultMeaningWhat to do
ValidPassed every required checkAccept
InvalidFailed the modulus checkAsk the customer to check their details
UncheckedThe sort code isn’t covered, or it’s a foreign currency account (exception 6)Usually accept: Vocalink says to treat these as valid

Unchecked is separate from Valid so you can decide how strict to be. If you don’t need the difference, use is_acceptable():

use uk_bank_details_validator::BankDetails;

let details = BankDetails::new("08-99-99", "66374958")?;
assert!(details.validate().is_acceptable()); // Valid or Unchecked

§Account numbers that aren’t 8 digits

DigitsRuleExample
6Prefix with 00123456 → 00123456
7Prefix with 01234567 → 01234567
9Santander: the first digit replaces the last sort code digit, then the last 8 digits are used12-34-56 + 123456789 → 12-34-51 + 23456789
10Not supported yet: returns ParseError::AmbiguousTenDigitAccount

Ten-digit numbers use the last 8 digits at NatWest but the first 8 at Co-op Bank and Leeds Building Society, and the specification doesn’t say which sort codes belong to which bank. Rather than guess, and risk calling a wrong number Valid, the crate rejects them.

§Working with the parts

SortCode and AccountNumber are types of their own, so a value can only exist if its format is correct:

use uk_bank_details_validator::{AccountNumber, BankDetails, SortCode};

let sort_code: SortCode = "08-99-99".parse()?;
let account: AccountNumber = "66374958".parse()?;
let details = BankDetails::from_parts(sort_code, account)?;

§Feature flags

FeatureDefaultDescription
stdyesImplements std::error::Error for the error types

Without std, the crate is no_std and needs no allocator:

uk-bank-details-validator = { version = "0.1", default-features = false }

§Data version

Crate versionVocalink specification
0.1.x9.10

Also available in code as uk_bank_details_validator::VOCALINK_DATA_VERSION. Vocalink updates its tables several times a year; data-only updates are released as patch versions.

§Minimum supported Rust version

Rust 1.85. Raising it is treated as a minor version change.

§Disclaimer

Not affiliated with, endorsed by or supported by Vocalink, Mastercard or Pay.UK. “Vocalink” and “Bacs” are trademarks of their respective owners. The weight and substitution tables are published by Vocalink.

§License

MIT

Structs§

AccountNumber
A bank account number exactly as the customer gave it: 6 to 10 digits.
BankDetails
UK bank details: a sort code and an eight-digit account number.
SortCode
A UK sort code: six digits, usually written 08-99-99.

Enums§

AccountNumberError
Why a string couldn’t be parsed as an AccountNumber.
ParseError
Why a sort code and account number couldn’t be turned into bank details.
SortCodeError
Why a string couldn’t be parsed as a SortCode.
Validation
The result of validating bank details.

Constants§

VOCALINK_DATA_VERSION
The Vocalink specification version the bundled tables come from.