hledger icon indicating copy to clipboard operation
hledger copied to clipboard

CSV import: Add rule to warn/error out on weird transactions

Open hseg opened this issue 4 months ago • 1 comments

It'd be helpful to be able to vocally assert that a .rules file doesn't know how to parse some transactions yet. In particular, I'm thinking of something like

if ! %txnType debit
  error "Unknown transaction type: " %txnType

with a similar warn rule for completeness.

Usecase: I'm writing up a .rules file for my credit card processor, and only have debit transactions to process at the moment, so I don't know what the other transaction types look like. I'd like to defensively insert assertions that the transactions are of the expected format, so that when one day in the future I'll make those different transactions, hledger will warn me before blithely importing them.

Similarly, it'd be useful if I could assert the header line of the CSV indeed contains those headers I expect, but that might need more of a change to support. For my case, I have two metadata lines first, so I'd like something like:

skip 2
if %line 1 && ! ^"שם כרטיס","תאריך לחיוב",...$
  error "Header has changed, check the file to make sure the schema hasn't changed!"
fields cardNo, billDate, ....

or some similar way of controlling that if so it only is tested against the header line. An alternative design I considered before rejecting it as probably too invasive is to have top-level skip directives make parsing subsequent lines start from the line following the skip, so eg

skip 2
if ! ^...$
  error
skip 1
fields

would only run the if block on the first line, and then would only run fields on subsequent lines.

hseg avatar Jul 28 '25 12:07 hseg