---
title: "Validating financial identifiers with finid"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Validating financial identifiers with finid}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(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

```{r}
finid_normalize("gb82 west 1234 5698 7654 32")
finid_normalize("4532-0151-1283-0366")
finid_normalize(c(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.

```{r}
finid_check(c("4532015112830366", "4532015112830367"), type = "luhn")

luhn_check_digit("453201511283036")
```

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.

```{r}
iban_valid(c(
  "GB82WEST12345698765432",
  "DE89370400440532013000",
  "FR1420041010050500013M02606",
  "GB82WEST12345698765431"
))

iban_format("GB82 WEST 1234 5698 7654 32")
iban_country("DE89370400440532013000")
iban_bban("DE89370400440532013000")
```

## Securities: ISIN, CUSIP and SEDOL

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

```{r}
isin_valid(c("US0378331005", "GB0002634946", "US0378331004"))
isin_country("US0378331005")
isin_check_digit("US037833100")
```

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

```{r}
cusip_valid(c("037833100", "594918104", "17275R102"))
cusip_check_digit("17275R10")

sedol_valid(c("0263494", "B0YQ5W0", "0263498"))
sedol_check_digit("026349")
```

## Legal entities and instruments

```{r}
lei_valid(c("529900T8BM49AURSDO55", "529900T8BM49AURSDO54"))
lei_check_digits("529900T8BM49AURSDO")

figi_valid(c("BBG000BLNNH6", "BBG000BBDZG3", "BBG000BLNNH5"))
figi_check_digit("BBG000BLNNH")
```

## SWIFT/BIC codes

```{r}
swift_valid(c("DEUTDEFF", "DEUTDEFF500", "DEUTZZFF"))
swift_country("DEUTDEFF500")
swift_bic8("DEUTDEFF500")
swift_branch("DEUTDEFF500")
```

## Chinese unified social credit codes

```{r}
uscc_valid(c("91350100M000100Y43", "91440300708461136T", "91350100M000100Y44"))
uscc_check_digit("91350100M000100Y4")
```

## 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.

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

finid_detect(ids)
finid_check(ids)
```

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.
