# `BankAccount`

A bank account to pay into or take from, in any country.

Use it when an account could be from anywhere. It holds a UK sort code and account number, an IBAN, or another country's details, and you choose which you were given.

## Shape

One of these, each with its own fields:

| Variant | Field | Type | Required? |
|---|---|---|---|
| `BankAccount.uk` | `sort_code` | [`UkSortCode`](/docs/types/type-uk-sort-code.md) | Yes |
|  | `account_number` | [`UkAccountNumber`](/docs/types/type-uk-account-number.md) | Yes |
| `BankAccount.iban` | `iban` | [`Iban`](/docs/types/type-iban.md) | Yes |
| `BankAccount.other` | `country` | [`Country`](/docs/types/type-country.md) | Yes |
|  | `bank_code` | `Text` | No |
|  | `account_number` | `Text` | Yes |

## Example

```jadpo
BankAccount.uk { sort_code: "089999" account_number: "66374958" }
```

## Good to know

- The others are `BankAccount.iban { iban: ... }` and `BankAccount.other { country: ... bank_code: ... account_number: ... }`.
- `text.format` shows only the last four digits, `•••• 4958`.
- `==` tells you whether two are the same. `<` and `list.sort_by` don't take it.
- A field that holds one counts as personal data (`financial`), so you don't mark it yourself.

## In JSON

An object of its variant's name, as `tag`, and the variant's fields, in a request, a response or `json.write`:

```json
{"tag":"uk","sort_code":"089999","account_number":"66374958"}
{"tag":"iban","iban":"GB29NWBK60161331926819"}
{"tag":"other","country":"US","bank_code":"021000021","account_number":"123456789"}
```

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`BankAccount` is in the group Money and banking, and has been in Jadpo since 0.1.0.

### Text form

It has no single text form, so `text.from` and `text.parse` don't take it.

### How it is stored

It is stored in 6 columns, each named for the field, `_` and its suffix. This is `{"tag":"other","country":"US","bank_code":"021000021","account_number":"123456789"}`:

| Column | Type | Holds |
|---|---|---|
| `<field>_kind` | `text` | `'other'` |
| `<field>_sort_code` | `text` | `NULL::text` |
| `<field>_account_number` | `text` | `'123456789'` |
| `<field>_iban` | `text` | `NULL::text` |
| `<field>_country` | `text` | `'US'` |
| `<field>_bank_code` | `text` | `'021000021'` |

### How it is shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `{"tag":"uk","sort_code":"089999","account_number":"66374958"}` | `•••• 4958` |
| `{"tag":"iban","iban":"GB29NWBK60161331926819"}` | `•••• 6819` |
| `{"tag":"other","country":"US","bank_code":"021000021","account_number":"123456789"}` | `•••• 6789` |
| `{"tag":"other","country":"NZ","bank_code":null,"account_number":"010202012345600"}` | `•••• 5600` |

### Operations

It also takes `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
