Validating financial identifiers with finid

Why validate identifiers?

Financial data rarely arrives clean. Card numbers come from payment terminals, IBANs from customer onboarding forms, ISINs from market data vendors, LEIs from counterparty reference data, and SWIFT codes from settlement instructions. A single mistyped character can route a payment to the wrong account, reject a trade, or break a reconciliation.

finid performs the checksum and structure checks that can be done offline. Every validator is vectorized, accepts identifiers with spaces or hyphens, and returns NA for NA input, so the functions compose with dplyr, data.table or base R pipelines.

Normalization

finid_normalize("gb82 west 1234 5698 7654 32")
#> [1] "GB82WEST12345698765432"
finid_normalize("4532-0151-1283-0366")
#> [1] "4532015112830366"
finid_normalize(c(NA, "us0378331005"))
#> [1] NA             "US0378331005"

Payment card numbers

pan_valid() checks the 12 to 19 digit length and the Luhn checksum. luhn_valid() applies the checksum to any length, which is useful for other Luhn-protected identifiers.

finid_check(c("4532015112830366", "4532015112830367"), type = "luhn")
#>              input       normalized type valid
#> 1 4532015112830366 4532015112830366 luhn  TRUE
#> 2 4532015112830367 4532015112830367 luhn FALSE

luhn_check_digit("453201511283036")
#> [1] 6

A Luhn checksum only shows that the digits are internally consistent. It does not show that an account exists or that the card is active; those checks require a payment processor.

Bank accounts: IBAN

iban_valid() verifies the country code, the registered total length, the national BBAN structure and the modulus 97 check digits. The registry table covers the countries listed by the ISO 13616 registry.

iban_valid(c(
  "GB82WEST12345698765432",
  "DE89370400440532013000",
  "FR1420041010050500013M02606",
  "GB82WEST12345698765431"
))
#> [1]  TRUE  TRUE  TRUE FALSE

iban_format("GB82 WEST 1234 5698 7654 32")
#> [1] "GB82 WEST 1234 5698 7654 32"
iban_country("DE89370400440532013000")
#> [1] "DE"
iban_bban("DE89370400440532013000")
#> [1] "370400440532013000"

Securities: ISIN, CUSIP and SEDOL

ISINs combine a two letter ISO 3166 country prefix, a nine character national number and a Luhn check digit.

isin_valid(c("US0378331005", "GB0002634946", "US0378331004"))
#> [1]  TRUE  TRUE FALSE
isin_country("US0378331005")
#> [1] "US"
isin_check_digit("US037833100")
#> [1] 5

CUSIPs identify North American securities and SEDOLs identify securities listed in London. Both validators also expose a *_check_digit() helper.

cusip_valid(c("037833100", "594918104", "17275R102"))
#> [1] TRUE TRUE TRUE
cusip_check_digit("17275R10")
#> [1] 2

sedol_valid(c("0263494", "B0YQ5W0", "0263498"))
#> [1]  TRUE  TRUE FALSE
sedol_check_digit("026349")
#> [1] 4

SWIFT/BIC codes

swift_valid(c("DEUTDEFF", "DEUTDEFF500", "DEUTZZFF"))
#> [1]  TRUE  TRUE FALSE
swift_country("DEUTDEFF500")
#> [1] "DE"
swift_bic8("DEUTDEFF500")
#> [1] "DEUTDEFF"
swift_branch("DEUTDEFF500")
#> [1] "500"

Chinese unified social credit codes

uscc_valid(c("91350100M000100Y43", "91440300708461136T", "91350100M000100Y44"))
#> [1]  TRUE  TRUE FALSE
uscc_check_digit("91350100M000100Y4")
#> [1] "3"

Profiling an identifier column

finid_detect() infers the identifier type. finid_check() returns a data frame with the original value, the normalized value, the detected type and the validation result, which makes it easy to triage a column before loading it into a warehouse.

ids <- c(
  "US0378331005",
  "GB82 WEST 1234 5698 7654 32",
  "037833100",
  "4532015112830366",
  "529900T8BM49AURSDO55",
  "not-an-id",
  NA
)

finid_detect(ids)
#> [1] "isin"    "iban"    "cusip"   "pan"     "lei"     "unknown" NA
finid_check(ids)
#>                         input             normalized    type valid
#> 1                US0378331005           US0378331005    isin  TRUE
#> 2 GB82 WEST 1234 5698 7654 32 GB82WEST12345698765432    iban  TRUE
#> 3                   037833100              037833100   cusip  TRUE
#> 4            4532015112830366       4532015112830366     pan  TRUE
#> 5        529900T8BM49AURSDO55   529900T8BM49AURSDO55     lei  TRUE
#> 6                   not-an-id                NOTANID unknown FALSE
#> 7                        <NA>                   <NA>    <NA>    NA

Rows detected as "unknown" are structurally inconsistent with every supported specification. A row whose valid value is FALSE (for example a 12 character ISIN-like string with a bad check digit) matched a structure but failed its checksum.

Scope and limitations

  • All checks run offline; no registry or network lookup is performed.
  • The IBAN registry table reflects the ISO 13616 registry at package release time. Countries that join the scheme later can be added in a future version.
  • Identifier structure and check digits are validated, not existence, ownership, status or regulatory permissions.
  • Card numbers, IBANs and similar data are sensitive; finid never logs or stores values and only returns logical or character results.