Functional Weave
Code in Rust

finance.ledger.journal-validate@1.0.0

README.md

1,922 bytes · view raw

# finance.ledger.journal-validate

The checks a ledger makes before it posts a journal. It reports every problem
rather than stopping at the first, because the person fixing a 40-line journal
wants the whole list, and it returns the totals it checked, so "unbalanced by
how much" is answered without a second pass.

A journal is valid when it has no problems:

| code | problem |
|---|---|
| `empty` | the journal has no lines |
| `invalid-account` | the account code is not well-formed (below) |
| `currency-mismatch` | a debit or credit is not in the journal currency; the line is left out of the totals |
| `negative-amount` | a debit or credit is negative (a negative debit is a credit: post it as one); left out of the totals |
| `zero-line` | debit and credit are both zero |
| `both-sides` | a line has both a debit and a credit; split it into two lines |
| `unbalanced` | total debits do not equal total credits |

Line numbers are 1-based, as a journal screen shows them. One line can have
more than one problem (a malformed account on a zero line), and each is listed.
Messages are fixed text and identical in every language; they do not format
amounts, because the totals are in the result.

**Account codes.** This checks the shape of a code, not that the account
exists in a chart of accounts, which is the caller's data. A code is 1 to 20
ASCII letters and digits, optionally in groups separated by a single `-`, `.`
or `/`: `4000`, `1200-01`, `SALES.UK`, `6100/2` are well-formed; `4 000`,
`-4000`, `4000-`, `40--00` and the empty string are not.

An invalid account code does not exclude the line from the totals, because its
amount is still a real amount. A wrong currency or a negative amount does,
because adding it would produce a total that means nothing.

Only the currency argument itself can make this throw (it must be an ISO 4217
code); everything wrong with the journal is reported, not thrown.